package com.zj365.health.act

import android.Manifest
import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.xty.base.act.BaseVmAct
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.picture.PictureUtils
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.PermissionUtils
import com.xty.common.xqr_code.XQRCodeAct
import com.xty.common.xqr_code.XQRCodeAct.Companion.QRCODE_AGREEMENT
import com.zj365.health.R
import com.zj365.health.databinding.ActDuihuanmaBinding
import com.zj365.health.vm.PayCancerVm
import com.xuexiang.xqrcode.XQRCode
import pub.devrel.easypermissions.EasyPermissions

/**
 * 已填问卷列表
 */
@Route(path = ARouterUrl.DUIHUAN)
class DuihuanmaAct : BaseVmAct<PayCancerVm>() {

    val binding by lazy { ActDuihuanmaBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root
    var goodId: String? = ""
    var orderNo: String? = ""
    override fun initData() {
        super.initData()
        goodId = intent.getStringExtra("id")
        orderNo = intent.getStringExtra("orderNo")
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "开通AI防癌预警"
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.mPrivate.setOnClickListener {
            binding.mPrivate.isSelected = !binding.mPrivate.isSelected
        }
        binding.mConfirm.setOnClickListener {
            confirm()
        }
        binding.mConfirm.isSelected = true
        binding.llScan.setOnClickListener {
            startScan()
        }
    }

    /**
     * 扫码
     */
    private fun startScan() {
        if(EasyPermissions.hasPermissions(this,
                Manifest.permission.CAMERA)){
            XQRCodeAct.start(
                this@DuihuanmaAct,
                QRCODE_AGREEMENT,
                R.style.XQRCodeTheme_Custom
            )
        }else{
            EasyPermissions.requestPermissions(this,"",
                0x00011,
                Manifest.permission.CAMERA)
        }
    }
    private fun confirm() {

        if (binding.etCode.text.toString().isNullOrEmpty()) {
            CommonToastUtils.showToast("请输入兑换码")
            return
        }

        if (!binding.mPrivate.isSelected) {
            CommonToastUtils.showToast("请阅读开通须知")
            return
        }

        mViewModel.payUrl(goodId, "EXCHANGE_CODE", binding.etCode.text.toString())
    }

    override fun liveObserver() {
        mViewModel.payObservable.observe(this) {
            val data = it.data
            //知情同意书
            //跳转兑换
            bundle.clear()
            bundle.putString("orderNo", data.orderNo)
            RouteManager.goAct(ARouterUrl.AGREE, bundle)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode === RESULT_OK) {
            when (requestCode) {
                QRCODE_AGREEMENT -> {
                    var type = data!!.getIntExtra(XQRCode.RESULT_TYPE, 0)
                    when (type) {
                        XQRCode.RESULT_SUCCESS -> {
                            try {
                                var result = data!!.extras!!.getString(XQRCode.RESULT_DATA)
                                LogUtils.d("扫描结果:$result")
                                if (!result.isNullOrEmpty()) {
                                    val gson = Gson()
                                    //设置扫描结果
                                    binding.etCode.setText(result)
                                } else {
                                    CommonToastUtils.showToast("扫码失败,请重试...")
                                }
                            } catch (e: Exception) {
                                LogUtils.e("二维码扫描：$e")
                                CommonToastUtils.showToast("请确认二维码是否正确！")
                            }
                        }
                        XQRCode.RESULT_FAILED -> {
                            LogUtils.d("扫描失败")

                        }
                    }
                }
            }
        }
    }

    override fun onSuccess(requestCode: Int) {
        super.onSuccess(requestCode)
        XQRCodeAct.start(
            this@DuihuanmaAct,
            QRCODE_AGREEMENT,
            R.style.XQRCodeTheme_Custom
        )
    }
}