package com.miya.fastcashier.ui

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.miya.fastcashier.config.IntentExtra
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.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 lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityRefundBinding

    private var numberManualDialog: NumberManualDialog? = null

    private var scanGunKeyEventHelper: ScanGunKeyEventHelper = ScanGunKeyEventHelper()

    companion object {
        fun start(context: BaseActivity) {
            val intent = Intent(context, RefundActivity::class.java)
            context.startActivity(intent)
        }
    }

    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()
                PriceInputActivity.start(
                    this@RefundActivity,
                    PriceInputActivity.Companion.PRICEINPUT_TYPE_CREFUND,
                    payServiceResponse
                )
            }
        })

        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)
        }
    }

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