package com.miya.fastcashier.ui

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.miya.fastcashier.databinding.ActivityPriceInputBinding
import com.miya.fastcashier.ui.dialog.PayChooseDialog
import com.miya.fastcashier.ui.dialog.PayChooseDialog.GeneratePayCodeClickListener
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.viewmodel.PayViewModel
import com.miya.fastcashier.widget.DecimalKeyboard.DELETE
import com.miya.fastcashier.widget.DecimalKeyboard.DOT
import java.math.BigDecimal


class PriceInputActivity : BaseActivity() {
    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityPriceInputBinding

    private var payChooseDialog: PayChooseDialog? = null;


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityPriceInputBinding.inflate(layoutInflater)
        setContentView(binding.root)
        viewModel = ViewModelProvider(this).get(PayViewModel::class.java)

        val tvGeneratePayCode = binding.tvGeneratePayCode
        val tvScanPayCode = binding.tvScanPayCode
        val llBack = binding.llBack
        val etPrice = binding.etPrice
        val numKeyboard = binding.numKeyboard

        tvScanPayCode.clickWithTrigger {

            val price = etPrice.text
            if(TextUtils.isEmpty(price)){
                CenterToasty.error(this,"请输入金额!",Toast.LENGTH_LONG).show()
                return@clickWithTrigger
            }

            val intent = Intent()
            intent.setClass(this@PriceInputActivity, PayActivity::class.java)
            intent.putExtra("price", price)
            startActivity(intent)

        }

        tvGeneratePayCode.clickWithTrigger {
            if (null == payChooseDialog) {
                payChooseDialog = PayChooseDialog(this)
                payChooseDialog!!.setGeneratePayCodeClickListener(object :
                    GeneratePayCodeClickListener {
                    override fun choose(payType: String) {
                        showProgressDialog("生成付款码")
                        viewModel.generatePayCode(etPrice.text.toString(),payType)
                    }
                })
            }
            payChooseDialog!!.show()
        }

        llBack.clickWithTrigger { finish() }

        numKeyboard.setKeyboardClickListener {
            when (it) {
                DELETE -> {
                    if (TextUtils.isEmpty(etPrice.text.toString())) {
                        return@setKeyboardClickListener
                    }
                    var currentCode: String = etPrice.text.toString()
                    currentCode = if (currentCode.length <= 1) {
                        ""
                    } else {
                        currentCode.substring(0, currentCode.length - 1)
                    }
                    etPrice.setText(currentCode)
                }
                DOT -> {
                    var currentCode: String = etPrice.text.toString()
                    if (!currentCode.contains(".") && !TextUtils.isEmpty(currentCode)) {
                        currentCode += "."
                        etPrice.setText(currentCode)
                    } else {
                        CenterToasty.error(this, "金额格式有误！", Toast.LENGTH_SHORT).show()
                    }
                }
                else -> {
                    var currentCode: String = etPrice.text.toString()
                    if (currentCode.contains(".")) {
                        val tmp =
                            currentCode.substring(currentCode.indexOf("."), currentCode.length - 1)
                        if (tmp.length < 2) {
                            if (BigDecimal(currentCode + it).compareTo(BigDecimal.ZERO) == 0 && tmp.length == 1) {
                                CenterToasty.error(this, "金额不能为0！", Toast.LENGTH_SHORT).show()
                            } else {
                                currentCode += it
                                etPrice.text = currentCode
                            }
                        } else {
                            CenterToasty.error(this, "金额最多设置2位小数！", Toast.LENGTH_SHORT).show()
                        }
                    } else {
                        if (currentCode.startsWith("0") && currentCode.length == 1 && it == 0) {
                            CenterToasty.error(this, "第二位不能输入0！", Toast.LENGTH_SHORT).show()
                        } else {
                            if (BigDecimal(currentCode + it).compareTo(BigDecimal("99999")) == 1) {
                                CenterToasty.error(this, "金额不能超过99999！", Toast.LENGTH_SHORT).show()
                            } else {
                                currentCode += it
                                etPrice.text = currentCode
                            }
                        }
                    }
                }
            }
        }

        viewModel.payServiceResponseLiveData.observe(this, { result ->
            dismissProgressDialog()

            result.getOrElse {
                it.message?.let { it1 -> CenterToasty.error(this, it1, Toast.LENGTH_LONG).show() }
                return@observe
            }

            result.getOrNull().also {
                it?.let { it1 ->
                    val intent = Intent()
                    if ("1" == it1.chanelTag){
                        intent.setClass(this@PriceInputActivity, WxPayCodeActivity::class.java)
                    }else{
                        intent.setClass(this@PriceInputActivity, AliPayCodeActivity::class.java)
                    }
                    intent.putExtra("payServiceResponse", it1)
                    startActivity(intent)
                }
            }
        })
    }

    override fun onStop() {
        super.onStop()
        payChooseDialog?.dismiss()
    }
}