package com.zj365.health.act.binddevice

import android.Manifest
import android.content.Intent
import com.google.gson.Gson
import com.xty.base.act.BaseVmAct
import com.xty.common.LogUtils
import com.xty.common.event.BindDevEvent
import com.xty.common.event.WorkManagerEvent
import com.xty.common.util.CommonToastUtils
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.ActBindDeviceBinding
import com.zj365.health.vm.BindDeviceVm
import com.xuexiang.xqrcode.XQRCode
import org.greenrobot.eventbus.EventBus
import pub.devrel.easypermissions.EasyPermissions

open class BaseDeviceBindAct<V: BindDeviceVm> : BaseVmAct<V>() {

    val binding  by lazy{ ActBindDeviceBinding.inflate(layoutInflater)}

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.tvBindDevice.setOnClickListener {
            bindDevice()
        }
        binding.tvScan.setOnClickListener {
            startScan()
        }
    }

    private fun bindDevice() {
        mViewModel.bindDev(binding.tvSnNum.text.toString())
    }

    override fun liveObserver() {
        mViewModel.bindDev.observe(this){
            EventBus.getDefault().post(BindDevEvent())
            EventBus.getDefault().post(WorkManagerEvent())  //首页更新
            finish()
        }
    }
    /**
     * 扫码
     */
    private fun startScan() {
        if(EasyPermissions.hasPermissions(this,
                Manifest.permission.CAMERA)){
            XQRCodeAct.start(
                this@BaseDeviceBindAct,
                QRCODE_AGREEMENT,
                R.style.XQRCodeTheme_Custom
            )
        }else{
            EasyPermissions.requestPermissions(this,"",
                0x00011,
                Manifest.permission.CAMERA)
        }
    }

    override fun onSuccess(requestCode: Int) {
        super.onSuccess(requestCode)
        XQRCodeAct.start(
            this@BaseDeviceBindAct,
            QRCODE_AGREEMENT,
            R.style.XQRCodeTheme_Custom
        )
    }
    override fun setLayout()= binding.root

    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.tvSnNum.setText(result)
                                } else {
                                    CommonToastUtils.showToast("扫码失败,请重试...")
                                }
                            }catch (e: Exception){
                                LogUtils.e("二维码扫描：$e")
                                CommonToastUtils.showToast("请确认二维码是否正确！")
                            }
                        }
                        XQRCode.RESULT_FAILED -> {
                            LogUtils.d("扫描失败")
                        }
                    }
                }
            }
        }
    }
}