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.util.JumpPermissionManagement
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 com.zj365.health.weight.DialogTip
import org.greenrobot.eventbus.EventBus
import pub.devrel.easypermissions.EasyPermissions

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

    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 {
           // finish()
            RxActivityTool.finishActivity(OneKeyBinderDeviceAct::class.java)
            RxActivityTool.finishActivity(DeviceChoiceAct::class.java)

        }
    }

    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{
            if (EasyPermissions.somePermissionDenied(this,
                    Manifest.permission.CAMERA)){
                DialogTip(
                    this,
                    "中健365需要相机权限,需要打开设置页面么?"
                ) {
                    JumpPermissionManagement.GoToSetting(this@OneKeyBinderDeviceAct)
                }.show()

            }else{
                DialogTip(
                    this,
                    "中健365需要相机权限，以便您使用扫一扫功能"
                ) {
                    EasyPermissions.requestPermissions(this,"中健365需要相机权限，以便您使用扫一扫功能",
                        0x00011,
                        Manifest.permission.CAMERA)
                }.show()
            }

        }
    }

    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 = "继续绑定"
                        }
                    }
                }
            }
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        LogUtils.e(TAG,"onPermissionsGranted")
        XQRCodeAct.start(
            this@OneKeyBinderDeviceAct,
            XQRCodeAct.QRCODE_AGREEMENT,
            R.style.XQRCodeTheme_Custom
        )
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        LogUtils.e(TAG,"onPermissionsDenied")
    }

    override fun onRationaleAccepted(requestCode: Int) {
        LogUtils.e(TAG,"onRationaleAccepted")
        XQRCodeAct.start(
            this@OneKeyBinderDeviceAct,
            XQRCodeAct.QRCODE_AGREEMENT,
            R.style.XQRCodeTheme_Custom
        )
    }

    override fun onRationaleDenied(requestCode: Int) {
        LogUtils.e(TAG,"onRationaleDenied")
    }
}