package com.miya.fastcashier.ui

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.fastcashier.lib_common.base.BaseActivity
import com.fastcashier.lib_common.config.IntentExtra
import com.fastcashier.lib_common.function.pay.OrderRecordManageKit
import com.fastcashier.lib_common.util.StringPriceFormatUtils
import com.fastcashier.lib_common.util.clickWithTrigger
import com.miya.fastcashier.databinding.ActivityAliPayCodeBinding
import com.miya.fastcashier.util.CenterToasty
import com.miya.fastcashier.viewmodel.PayViewModel
import com.miya.print.utils.BitmapUtils
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import java.util.*

/***
 * 支付宝付款码
 */
class AliPayCodeActivity : BaseActivity() {

    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityAliPayCodeBinding

    companion object {
        fun start(context: BaseActivity, payServiceResponse: PayServiceResponse?) {
            val intent = Intent()
            intent.setClass(context, AliPayCodeActivity::class.java)
            intent.putExtra(IntentExtra.KEY_EXTRA_PAYSERVICERESPONSE, payServiceResponse)
            context.startActivity(intent)
        }
    }

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

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

        val ivPayCode = binding.ivPayCode
        val llBack = binding.llBack
        val tvPrice = binding.tvPrice

        llBack.clickWithTrigger { finish() }

        val extra = intent.getSerializableExtra(IntentExtra.KEY_EXTRA_PAYSERVICERESPONSE)
        extra?.let {
            val payServiceResponse = it as PayServiceResponse
            LogUtils.e(GsonUtils.toJson(payServiceResponse))
            tvPrice.text = "¥".plus(StringPriceFormatUtils.transStringPriceToDecimalString( payServiceResponse.tradPrice))
            runBlocking(Dispatchers.IO) {
                val qrcode = BitmapUtils.createBarcodeBitmap(payServiceResponse.payQrCode, 9, 453, 453)
                runOnUiThread {
                    ivPayCode.setImageBitmap(qrcode)
                    //支付结果查询
                    viewModel.payResultQuery(payServiceResponse)
                }
            }
        }

        viewModel.payResultLiveData.observe(this,{ result ->
            result.getOrElse {
                it.message?.let { it1 -> {
                    if(!it1.contains("交易不存在")){
                        CenterToasty.error(this, it1,Toast.LENGTH_LONG).show()
                    }
                }  }
                return@observe
            }

            result.getOrNull().also {
                it?.let { it1 ->
                    OrderRecordManageKit.setSerialNumber()
                    PayResultActivity.start(this@AliPayCodeActivity,it1)
                }
            }
        })
    }

    override fun onStop() {
        super.onStop()
        viewModel.payResultJob.cancel()
    }

}