package com.zj365.mime.act

import android.app.Service
import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxActivityTool
import com.xty.base.act.BaseVmAct
import com.xty.common.Const
import com.xty.common.bracelet.YCBBlueConnectHelper
import com.xty.common.MMkvHelper
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.BlueToothConnectEvent
import com.xty.common.util.CommonToastUtils
import com.xty.common.util.CommonUtils
import com.xty.common.work.MyWorkManger
import com.zj365.mime.R
import com.zj365.mime.databinding.ActSettingBinding
import com.zj365.mime.vm.SettingVm
import com.zj365.mime.weight.FactorySettingDialog
import com.xty.network.MyRetrofit
import com.xty.network.model.SettingBean
import org.greenrobot.eventbus.EventBus

/**
 * 我的设置
 */
class SettingAct : BaseVmAct<SettingVm>() {

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

    private var isDemonstrate: Boolean = false
    var user: SettingBean.User?=null

    private val logOutDialog by lazy {
        FactorySettingDialog(this, getString(R.string.dialog_logout)) {
            logoOut()
        }
    }

    override fun initData() {
        super.initData()
        isDarkBar = true
        var intentData = intent.extras!!.getSerializable("user")
        isDemonstrate = intent.extras!!.getBoolean("isDemonstrate",false)

        if (null != intentData) user = intentData as SettingBean.User
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = getString(R.string.my_setting)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.mVersion.text = "版本号  ${packageManager.getPackageInfo(this.packageName,0).versionName}"
        binding.mTvSafe.setOnClickListener {
            RouteManager.goAct(ARouterUrl.ACCOUNT_SAFE)
        }

        binding.mTvDevice.setOnClickListener {
            if (isDemonstrate){
                CommonToastUtils.showToast("请先绑定设备才能操作设备设置")
                return@setOnClickListener
            }

            if (CommonUtils.isBlueTooth(MMkvHelper.getInt(Const.Watch_Device_Type))){
                if (YCBBlueConnectHelper.getBlueStatus())
                    RouteManager.goAct(ARouterUrl.DEVICE_SETTING)
                else
                    CommonToastUtils.showToast("设备未连接")
            }else{
                RouteManager.goAct(ARouterUrl.WEAR_MANAGER)
            }
        }
        binding.mTvNotice.setOnClickListener {
            RouteManager.goAct(ARouterUrl.NOTICE_SETTING)
        }
        binding.mFeedBack.setOnClickListener {
            user?.let {
                bundle.clear()
                bundle.putString("name",it.name)
                bundle.putString("id",it.id.toString())
                RouteManager.goAct(ARouterUrl.FEED_BACK,bundle)
            }


        }
        binding.mLogOut.setOnClickListener {
            logOutDialog.show()
        }

        binding.mTvAbout.setOnClickListener {
            RouteManager.goAct(ARouterUrl.ABOUT)
        }
        binding.mVersionList.setOnClickListener{
            CommonToastUtils.showToast( "当前版本：${packageManager.getPackageInfo(this.packageName,0).versionName}")
//            RouteManager.goAct(ARouterUrl.VERSION_LIST)
        }

        binding.mTvPrev.setOnClickListener {

        RouteManager.goAct( ARouterUrl.AGREEMENT_LIST)
        }
    }

    override fun liveObserver() {

    }

    private fun logoOut() {
        //退出音乐播放器
        var service=RouteManager.getAny(ARouterUrl.MUSIC_SERVICE) as Service
        var intent= Intent(this,service::class.java)
        intent.putExtra("close","")
        stopService(intent)
        //结束mqtt服务
//        var mqttService=RouteManager.getAny(ARouterUrl.MQTT_SERVICE) as Service
//        var mqttIntent= Intent(this,mqttService::class.java)
//        mqttIntent.putExtra("close","")

//        stopService(mqttIntent)
//        //结束前台服务
//         val noneService = RouteManager.getAny(ARouterUrl.NONE_SERVICE) as Service
//        var noneIntent=Intent(this,noneService::class.java)
//        stopService(noneIntent)

        MyWorkManger.cancelWork()
        Const.mainLiveData?.value?.apply {
            user?.bluetooth = ""//将蓝牙信息清空
        }

        MMkvHelper.remove(Const.Watch_Device_Type)//清除手表型号
        Const.watchType = -1//将手表型号恢复初始值
        Const.battery = ""//电池信息恢复初始值

        MyRetrofit.get().BLUETOOTH_MAC = ""//清除蓝牙信息
        MMkvHelper.remove(Const.Blue_Tooth)//清除蓝牙信息
        MMkvHelper.clearUserInfo()//清除用户登录信息
        YCBBlueConnectHelper.disconnect()//断开蓝牙连接
        YCBBlueConnectHelper.cancelTimer()//断开重连定时器
        EventBus.getDefault().post(BlueToothConnectEvent())//刷新蓝牙连接信息
        RouteManager.goAct(ARouterUrl.LOGIN_ACT)
        RxActivityTool.finishAllActivity()
    }
}