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.databinding.ActivityWxPayCodeBinding
import com.miya.fastcashier.ui.dialog.PayChooseDialog
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.DateUtils
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 com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import java.util.*
import kotlin.concurrent.timerTask

/**
 * 微信付款码
 */
class WxPayCodeActivity : AppCompatActivity() {
    private lateinit var viewModel: PayViewModel
    private lateinit var binding: ActivityWxPayCodeBinding

    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

        val kv = MMKV.defaultMMKV()

        llBack.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))
            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 ->

                    //自增流水
                    val today = DateUtils.format8(Date())
                    var no = kv.getInt(today,0);
                    kv.putInt(DateUtils.format8(Date()),++no)

                    val intent = Intent()
                    intent.setClass(this@WxPayCodeActivity, PayResultActivity::class.java)
                    intent.putExtra("payServiceResponse",it1)
                    startActivity(intent)
                }
            }
        })

    }

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

}