package com.ray650128.floatingwindow.view

import android.content.Context
import android.graphics.Color
import android.text.InputType
import android.util.AttributeSet
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import com.ray650128.floatingwindow.R
import com.ray650128.floatingwindow.dp

class ValueEditor: LinearLayout {

    constructor(context: Context): super(context) {
        this.context = context
        initContentView()
    }

    constructor(context: Context, attrs: AttributeSet): super(context, attrs) {
        this.context = context
        initContentView()
    }

    constructor(context: Context, attrs: AttributeSet, intRes: Int): super(context, attrs, intRes) {
        this.context = context
        initContentView()
    }

    private lateinit var context: Context

    private var onValueChangeListener: OnValueChangeListener? = null

    private val btnAdd by lazy {
        ImageButton(context).apply {
            setImageResource(R.drawable.ic_value_add)
            setBackgroundColor(Color.TRANSPARENT)
            layoutParams = LayoutParams(48.dp, 48.dp)
        }
    }

    private val btnValue by lazy {
        Button(context).apply {
            text = "$value"
            setBackgroundColor(Color.TRANSPARENT)
            layoutParams = LayoutParams(0, 48.dp).apply {
                weight = 1.0f
            }
        }
    }

    private val btnMinus by lazy {
        ImageButton(context).apply {
            setImageResource(R.drawable.ic_value_minus)
            setBackgroundColor(Color.TRANSPARENT)
            layoutParams = LayoutParams(48.dp, 48.dp)
        }
    }

    var value: Int = 0
        set(value) {
            field = value
            btnValue.text = "$value"
        }

    private fun initContentView() {
        this.orientation = HORIZONTAL

        this.addView(btnAdd)
        this.addView(btnValue)
        this.addView(btnMinus)

        btnAdd.setOnClickListener {
            onValueChangeListener?.onValueAdd()
        }

        btnValue.setOnClickListener {
            AlertDialog.Builder(context).apply {
                val editText = EditText(context).apply {
                    inputType = InputType.TYPE_CLASS_NUMBER
                }
                setTitle("請輸入想要的值")
                setView(editText)
                setPositiveButton("確定") { dialog, _ ->
                    val inputText = editText.text?.toString()?.toIntOrNull()
                    onValueChangeListener?.onValueClick(inputText)
                    dialog.dismiss()
                }
                setNegativeButton("取消") { dialog, _ ->
                    dialog.dismiss()
                }
            }.show()
        }

        btnMinus.setOnClickListener {
            onValueChangeListener?.onValueMinus()
        }
    }

    fun setOnValueChangeListener(listener: OnValueChangeListener) {
        this.onValueChangeListener = listener
    }

    interface OnValueChangeListener {
        fun onValueAdd()

        fun onValueMinus()

        fun onValueClick(value: Int?)
    }
}