package com.miya.fastcashier.ui.dialog

import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.miya.fastcashier.R
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.StringPriceFormat
import com.miya.fastcashier.widget.DecimalKeyboard
import com.miya.fastcashier.widget.NumKeyboard
import com.rengwuxian.materialedittext.MaterialEditText
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
import java.math.BigDecimal

/**
 * 手动输入数字
 */
class NumberManualDialog(context: Context, ) : Dialog(context) {

    private lateinit var ivClose: ImageView
    private lateinit var etNumber: TextView
    private lateinit var tvErrInfo: TextView
    private lateinit var numKeyboard: NumKeyboard
    private lateinit var numInputFinish: Button

    private lateinit var numberInputListener: NumberInputListener

    init {
        initWindow()
        initConfig()
        initView()
    }

    private fun initConfig() {
        setCancelable(false)
    }

    private fun initView() {
        setContentView(R.layout.dialog_number_input)
        ivClose = findViewById(R.id.ivClose)
        ivClose.setOnClickListener {
            dismiss()
        }
        etNumber = findViewById(R.id.etNumber)
        tvErrInfo = findViewById(R.id.tvErrInfo)
        numKeyboard = findViewById(R.id.numKeyboard)
        numInputFinish = numKeyboard.okButton

        numKeyboard.setKeyboardClickListener {
            when (it) {
                NumKeyboard.DELETE -> {
                    if (TextUtils.isEmpty(etNumber.text.toString())) {
                        return@setKeyboardClickListener
                    }
                    var currentCode: String = etNumber.text.toString()
                    currentCode = if (currentCode.length <= 1) {
                        ""
                    } else {
                        currentCode.substring(0, currentCode.length - 1)
                    }
                    etNumber.setText(currentCode)
                    numInputFinish.isEnabled = !TextUtils.isEmpty(currentCode)
                }
                NumKeyboard.SURE -> {
                    val currentCode: String = etNumber.text.toString()
                    if (!TextUtils.isEmpty(currentCode)) {
                        dismiss()
                        tvErrInfo.visibility = View.GONE
                        etNumber.setText(currentCode)
                        numberInputListener.numberInput(currentCode)
                    } else {
                        tvErrInfo.visibility = View.VISIBLE
                        tvErrInfo.text = "内容为空"
                    }
                }
                else -> {
                    tvErrInfo.visibility = View.GONE
                    var currentCode: String = etNumber.text.toString()
                    if(TextUtils.isEmpty(currentCode)){
                        etNumber.setText(it.toString())
                    }else{
                        currentCode += it
                        etNumber.setText(currentCode)
                    }
                    numInputFinish.isEnabled = !TextUtils.isEmpty(currentCode)
                }
            }
        }
    }


     fun setNumberInputListener(numberInputListener: NumberInputListener){
        this.numberInputListener = numberInputListener
    }

    private fun initWindow() {
        val win = this.window
        win!!.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        lp.gravity = Gravity.BOTTOM
        lp.windowAnimations = R.style.BottomInAndOutStyle
        win.attributes = lp
        win.setBackgroundDrawableResource(R.color.transparent)
    }

    interface NumberInputListener {
        fun numberInput(number:String);
    }
}