package com.miya.fastcashier.ui

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.LogUtils
import com.miya.fastcashier.config.IntentExtra
import com.miya.fastcashier.databinding.ActivityWxPayCodeBinding
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.StringPriceFormat
import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.utils.manage.OrderRecordManageKit
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 WxPayCodeActivity : AppCompatActivity() {
    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityWxPayCodeBinding

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

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

        binding = ActivityWxPayCodeBinding.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(JSON.toJSONString(payServiceResponse))
            tvPrice.text = "¥".plus(StringPriceFormat.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@WxPayCodeActivity,it1)
                }
            }
        })

    }

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

}