package com.miya.fastcashier.ui

import android.os.Bundle
import android.view.KeyEvent
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.miya.fastcashier.databinding.ActivityRefundBinding
import com.miya.fastcashier.ui.dialog.MessageDialog
import com.miya.fastcashier.ui.dialog.NumberManualDialog
import com.miya.fastcashier.ui.dialog.NumberManualDialog.NumberInputListener
import com.miya.fastcashier.ui.dialog.RefundInfoDialog
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.ScanGunKeyEventHelper
import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.viewmodel.PayViewModel
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse


class RefundActivity : BaseActivity() {
    private var alertDialog: MessageDialog? = null
    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityRefundBinding

    private  var refundInfoDialog: RefundInfoDialog? = null
    private  var numberManualDialog: NumberManualDialog? = null

    private var scanGunKeyEventHelper: ScanGunKeyEventHelper = ScanGunKeyEventHelper()

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

        binding = ActivityRefundBinding.inflate(layoutInflater)
        setContentView(binding.root)
        viewModel = ViewModelProvider(this).get(PayViewModel::class.java)
        viewModel.payResultLiveData.observe(this,  { payResult ->
            payResult.onFailure {
                dismissProgressDialog()
                it.message?.let { it1 -> CenterToasty.error(this, it1,Toast.LENGTH_LONG).show() }
            }
            payResult.onSuccess { payServiceResponse ->
                dismissProgressDialog()
                //金额确认弹窗
                if(null == refundInfoDialog){
                    refundInfoDialog = RefundInfoDialog(this)
                }
                refundInfoDialog?.setData(payServiceResponse) {
                    doubleRefundConfirm(it)
                };
                refundInfoDialog?.show()
            }
        })


        viewModel.refundLiveData.observe(this,  { payResult ->
            payResult.onFailure {
                dismissProgressDialog()
                it.message?.let { it1 -> CenterToasty.error(this, it1,Toast.LENGTH_LONG).show() }
            }
            payResult.onSuccess {
                dismissProgressDialog()
                CenterToasty.success(this,"退款成功!!",Toast.LENGTH_LONG).show()
                finish()
            }
        })
        val llBack = binding.llBack

        llBack.clickWithTrigger { finish() }

        val ivRefundManualInput = binding.ivRefundManualInput
        ivRefundManualInput.clickWithTrigger {
            if (numberManualDialog == null) {
                numberManualDialog = NumberManualDialog(this);
                numberManualDialog!!.setNumberInputListener(object :
                    NumberInputListener {
                    override fun numberInput(number: String) {
                        showProgressDialog("正在为您退款..")
                        viewModel.payResultQuery(number)
                    }
                })
            }
            numberManualDialog!!.show()
        }

        scanGunKeyEventHelper.setOnBarCodeCatchListener {
            showProgressDialog("正在为您退款..")
            viewModel.payResultQuery(it)
        }
    }


    /**
     * 二次退款确认
     */
    private fun doubleRefundConfirm(payServiceResponse: PayServiceResponse) {
        if (null == alertDialog) {
            alertDialog =  MessageDialog(this)
                .setCustomMessage("是否确认退款")
                .setPositiveButton("确认退款"
                ) { _, _ ->
                    run {
                        showProgressDialog("退款中")
                        viewModel.refund(payServiceResponse)
                    }
                }
        }
        alertDialog?.show()
    }

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        scanGunKeyEventHelper.analysisKeyEvent(event)
        return super.dispatchKeyEvent(event)
    }

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

}