package com.miya.fastcashier.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.ViewModelProvider
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.miya.fastcashier.R
import com.miya.fastcashier.R.mipmap.ic_alipay
import com.miya.fastcashier.R.mipmap.ic_wxpay
import com.miya.fastcashier.databinding.ActivityPayCodeBinding
import com.miya.fastcashier.databinding.ActivityPriceInputBinding
import com.miya.fastcashier.ui.dialog.PayChooseDialog
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.StringPriceFormat
import com.miya.fastcashier.utils.clickWithTrigger
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.*
import kotlin.concurrent.timerTask

class PayCodeActivity : AppCompatActivity() {
    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityPayCodeBinding

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

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

        val ivPayCode = binding.ivPayCode
        val tvBack = binding.tvBack
        val ivBack = binding.ivBack
        val tvPrice = binding.tvPrice
        val tvPayGuide = binding.tvPayGuide
        val cPayCodeBg = binding.cPayCodeBg

        tvBack.clickWithTrigger { finish() }
        ivBack.clickWithTrigger { finish() }

        val extra = intent.getSerializableExtra("payServiceResponse")
        extra?.let {
            val payServiceResponse = it as PayServiceResponse
            LogUtils.e(JSON.toJSONString(payServiceResponse))
            tvPrice.text = "¥".plus(StringPriceFormat.transStringPriceToDecimalString( payServiceResponse.tradPrice))
            if(payServiceResponse.chanelTag == "1"){
                //初始化微信皮肤
                setTheme(R.style.Theme_MiYaFastCashier_Green)
                tvPayGuide.text = "微信扫描二维码 向我付款";
                tvPayGuide.setCompoundDrawables(AppCompatResources.getDrawable(this, ic_wxpay) ,null,null,null);
                cPayCodeBg.setBackgroundResource(R.color.green)
            }else{
                //初始化支付宝皮肤
                setTheme(R.style.Theme_MiYaFastCashier_Blue)
                tvPayGuide.setCompoundDrawables(AppCompatResources.getDrawable(this, ic_alipay) ,null,null,null);
                tvPayGuide.text = "支付宝扫描二维码 向我付款";
                cPayCodeBg.setBackgroundResource(R.color.blue)

            }
            runBlocking(Dispatchers.IO) {
                val qrcode = BitmapUtils.createBarcodeBitmap(payServiceResponse.payQrCode, 9, 403, 403)
                runOnUiThread {
                    ivPayCode.setImageBitmap(qrcode)
                    //支付结果查询
                    viewModel.payResultQuery(payServiceResponse)
                }
            }
        }

        viewModel.payResultLiveData.observe(this,{ result ->
            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()
                    intent.setClass(this@PayCodeActivity, PayResultActivity::class.java)
                    intent.putExtra("payServiceResponse",it1)
                    startActivity(intent)
                }
            }
        })

    }

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

}