package com.zj365.dc.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.bracelet.checkBlueConnectPermission
import com.xty.common.bracelet.getBluePermission
import com.xty.common.event.BindDevEvent
import com.xty.network.model.DeviceBean
import com.zj365.dc.R
import com.zj365.dc.adapter.DeviceChoiceAdapter
import com.zj365.dc.databinding.ActDeviceChoiceBinding
import com.zj365.dc.vm.DeviceChoiceVm
import com.zj365.mime.weight.FactorySettingDialog
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 选择手表型号
 */
@Route(path = ARouterUrl.DEVICE_CHOICE)
class DeviceChoiceAct : BaseVmAct<DeviceChoiceVm>() {

    val binding by lazy { ActDeviceChoiceBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private val deviceAdapter by lazy { DeviceChoiceAdapter() }
    private var isLogin = false


    override fun liveObserver() {
        mViewModel.deviceTypeListLiveData.observe(this){
            deviceAdapter.setNewInstance(it.data)
        }

    }

    override fun initData() {
        super.initData()
        isLogin = intent.getBooleanExtra("isLogin", false)

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
//        binding.title.titleBg.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
        binding.title.mTvTitle.text = getString(R.string.activity_chocie_device)
        binding.title.mIvBack.setOnClickListener {
            if (isLogin) { //如果是走注册流程过来的，退出该界面，则进入主界面
                RouteManager.goAct(ARouterUrl.MAIN_ACT)
                RxActivityTool.finishAllActivity()
            } else {
                finish()
            }
        }
        binding.rlvData.apply {
            layoutManager = LinearLayoutManager(this@DeviceChoiceAct)
            adapter = deviceAdapter
        }


        deviceAdapter.setOnItemClickListener { adapter, _, position ->
            val device = adapter.data[position] as DeviceBean
            val type = device.watchType
            if (type != 0) {
                if(device.bindType == 1){
                    bundle.clear()
                    bundle.putInt("type", type)
                    bundle.putBoolean("isLogin", isLogin)
                    RouteManager.goAct(ARouterUrl.ONE_KEY_DEVICE_ACTIVE, bundle)
                }else{
                    bundle.clear()
                    bundle.putInt("type", type)
                    bundle.putBoolean("isLogin", isLogin)
                    RouteManager.goAct(ARouterUrl.DEVICE_ACTIVE, bundle)

                }

            } else {
                if (!checkBlueConnectPermission(this)) {
                    FactorySettingDialog(this, "蓝牙手表需要访问位置、蓝牙权限。") {
                        getBluePermission(baseContext) {
                            if (it) {
                                bundle.clear()
                                bundle.putInt("type", type)
                                bundle.putBoolean("isLogin", isLogin)
                                RouteManager.goAct(ARouterUrl.DEVICE_ACTIVE, bundle)
                            }
                        }
                    }.show()
                } else {
                    bundle.clear()
                    bundle.putInt("type", type)
                    bundle.putBoolean("isLogin", isLogin)
                    RouteManager.goAct(ARouterUrl.DEVICE_ACTIVE, bundle)
                }
            }
        }
        mViewModel.getDeviceType()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (isLogin) { //如果是走注册流程过来的，退出该界面，则进入主界面
            RouteManager.goAct(ARouterUrl.MAIN_ACT)
            RxActivityTool.finishAllActivity()
        } else {
            super.onBackPressed()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBindDev(event : BindDevEvent){
        finish()
    }
}