package com.zj365.dc.activity

import android.Manifest
import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.act.BaseVmAct
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.event.BindDevEvent
import com.xty.common.util.CommonToastUtils
import com.xty.common.xqr_code.XQRCodeAct
import com.xuexiang.xqrcode.XQRCode
import com.zj365.dc.databinding.ActBinderDeviceOneKeyBinding
import com.zj365.dc.model.ScanInfoBean
import com.zj365.dc.vm.OneKeyBinderDeviceVm
import com.zj365.health.R
import org.greenrobot.eventbus.EventBus
import pub.devrel.easypermissions.EasyPermissions

@Route(path = ARouterUrl.ONE_KEY_DEVICE_ACTIVE)
class OneKeyBinderDeviceAct : BaseVmAct<OneKeyBinderDeviceVm>() {

    val binding by lazy { ActBinderDeviceOneKeyBinding.inflate(layoutInflater) }
    override fun liveObserver() {
        mViewModel.bindResult.observe(this){
            EventBus.getDefault().post(BindDevEvent())
            CommonToastUtils.showToast("绑定成功")
            finish()
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text ="一键绑定"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.tvScan.setOnClickListener {
            startScan()
        }
        binding.tvBackHome.setOnClickListener {
            RxActivityTool.finishActivity(DeviceChoiceAct::class.java)
            finish()
        }
    }

    override fun initData() {
        super.initData()
    }

    private fun startScan() {
        if(EasyPermissions.hasPermissions(this,
                Manifest.permission.CAMERA)){
            XQRCodeAct.start(
                this@OneKeyBinderDeviceAct,
                XQRCodeAct.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@OneKeyBinderDeviceAct,
            XQRCodeAct.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) {
                XQRCodeAct.QRCODE_AGREEMENT -> {
                    when (data!!.getIntExtra(XQRCode.RESULT_TYPE, 0)) {
                        XQRCode.RESULT_SUCCESS -> {
                            try {
                                val result = data.extras!!.getString(XQRCode.RESULT_DATA)
                                LogUtils.d("扫描结果:$result")
                                binding.tvErrorMsg.visibility = View.GONE
                                if (!result.isNullOrEmpty()) {
                                    mViewModel.binderDevice(result)
                                } else {
                                    binding.tvErrorMsg.visibility = View.VISIBLE
                                    binding.tvScan.text = "继续绑定"
                                }
                            } catch (e: Exception) {
                                binding.tvErrorMsg.visibility = View.VISIBLE
                                binding.tvScan.text = "继续绑定"
                            }
                        }

                        XQRCode.RESULT_FAILED -> {
                            binding.tvErrorMsg.visibility = View.VISIBLE
                            binding.tvScan.text = "继续绑定"
                        }
                    }
                }
            }
        }
    }
}