package com.zj365.mime.act

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.gson.Gson
import com.xty.base.act.BaseVmAct
import com.xty.base.weight.TipDialog
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
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.mime.R
import com.zj365.mime.databinding.ActMyOrganBinding
import com.zj365.mime.vm.MyOrganVm
import com.xuexiang.xqrcode.XQRCode

/**
 * 我的机构
 */
@Route(path = ARouterUrl.MY_ORGAN)
class MyOrganAct: BaseVmAct<MyOrganVm>() {

    private var isUpdate: Boolean = false
    private var isChangeOrgan: Boolean = false
    val binding by lazy { ActMyOrganBinding.inflate(layoutInflater) }

    private val dialogInfoUn by lazy {
        TipDialog(this ,  "确定退出该机构，重新加入新机构吗？",true ,"确认","取消") {
            isChangeOrgan = true
            binding.mTvActCode.text.clear()
            binding.lle1.visibility = View.GONE
            binding.lle2.visibility = View.VISIBLE
        }
    }
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
    }

    override fun onBackPressed() {

        if (isChangeOrgan){
            binding.lle1.visibility = View.VISIBLE
            binding.lle2.visibility = View.GONE

        }else{
            finish()
            super.onBackPressed()
        }
        isChangeOrgan = false
    }
    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            if (isChangeOrgan){
                binding.lle1.visibility = View.VISIBLE
                binding.lle2.visibility = View.GONE
            }else{
                finish()
            }
            isChangeOrgan = false
        }
        binding.title.mTvTitle.text = getString(R.string.my_organ)

        if ( Const.DeviceIdNum.isNullOrEmpty()){
            binding.mScanBtn.visibility = View.VISIBLE
            binding.mScanBtn.setOnClickListener {
                dialogInfoUn.show()
            }
        }else{
            binding.mScanBtn.visibility = View.GONE
        }

        binding.mSubmit.setOnClickListener {
           val code = binding.mTvActCode.text.toString()
            if (code.isNullOrEmpty()) {
                CommonToastUtils.showToast("请输入机构授权码")
                return@setOnClickListener
            }
            mViewModel.bindAuthCode(code)
            isUpdate = true
        }
        binding.llScan.setOnClickListener {
            startScan()
        }

    updateOrgan()
    }

    private fun updateOrgan() {
        mViewModel.getMyOrgan()
    }

    private fun startScan() {
       /* PermissionUtils.requestPermission(this,
            { XQRCodeAct.start(
                this@MyOrganAct,
                QRCODE_AGREEMENT,
                R.style.XQRCodeTheme_Custom
            ) }, Permission.CAMERA)*/

    }
    override fun liveObserver() {

        mViewModel.BindingResponeLive.observeForever {
            if (it.code==0){
                CommonToastUtils.showSucceedToast("关联机构成功！")
                binding.title.mIvBack.performClick()
                updateOrgan()
            }else{
                CommonToastUtils.showToast(it.msg)
            }
        }
        mViewModel.myOrganLive.observe(this){


            if (it.data.authCode.isNullOrEmpty()){//没有加入机构
                binding.lle1.visibility = View.GONE
                binding.lle2.visibility = View.VISIBLE

            }else{
//                binding.ivQrCode.
                binding.lle1.visibility = View.VISIBLE
                binding.lle2.visibility = View.GONE
                binding.organCodeTv.text = it.data.authCode
                binding.mOrganNameTv.text = it.data.name
                val bitmap = XQRCode.createQRCodeWithLogo(it.data.authCode ,null )
                binding.ivQrCode.setImageBitmap(bitmap)
            }


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