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.MMkvHelper
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.bracelet.YCBBlueConnectHelper
import com.xty.common.event.BlueToothConnectEvent
import com.xty.common.event.LoginOutEvent
import com.xty.common.work.MyWorkManger
import com.xty.network.MyRetrofit
import com.xty.network.model.SettingBean
import com.zj365.mime.R
import com.zj365.mime.databinding.ActSettingNewBinding
import com.zj365.mime.vm.SettingVm
import com.zj365.mime.weight.FactorySettingDialog
import org.greenrobot.eventbus.EventBus

@Route(path = ARouterUrl.SETTING)
class SettingNewAct: BaseVmAct<SettingVm>()  {
    val binding by lazy { ActSettingNewBinding.inflate(layoutInflater) }

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

    var user: SettingBean.User? = null

    private var destStr = """
        一旦您注销中健三六五健康账号后，您的中健三六五健康APP将会被清除账号信息，请知悉并谨慎操作！
        注销中健三六五健康账号前，请确保您的账号符合以下注销条件：
        1、账号处于安全状态
        您的账号没有被盗、被封等风险（最近15天账号未在其他设备登录）
        2、其他关联用户解绑
        您的账号已经解除对其他中健三六五手表佩戴用户的关注与绑定
        3、中健三六五手表解绑
        您的账号已经解除对中健三六五手表的绑定
        4、账号不可找回
        注销后，账号绑定的信息将被释放，账号无法登录中健三六五健康APP
    """.trimIndent()

    private val destAccount by lazy {
        FactorySettingDialog(this, destStr) {
            //确认解除绑定
            user?.let {
                mViewModel.destAccount(it.id.toString())
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text ="设置"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

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

        binding.mTvModifyPwd.setOnClickListener {
            RouteManager.goAct(ARouterUrl.CHANGE_PWD)

        }

        binding.mTvPrivacy.setOnClickListener {

            bundle.clear()
            bundle.putString("title", "隐私政策")
            bundle.putBoolean("isAgree", false)
            bundle.putString("url","https://dc.zhongjian365.com/privacy.html")
            RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)

        }

        binding.mTvUserAgree.setOnClickListener {
            bundle.clear()
            bundle.putString("title", "用户协议")
            bundle.putBoolean("isAgree", false)
            bundle.putString("url","https://dc.zhongjian365.com/agreement.html")
            RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
           /* bundle.clear()
            bundle.putString("title", "用户协议")
            bundle.putInt("type", Const.AGREEMENT_1)
            bundle.putBoolean("isAgree", false)
            RouteManager.goAct(ARouterUrl.RICH_TEXT, bundle)*/

        }

        binding.mTvSignOut.setOnClickListener {
            destAccount.changeTextSetting()
            destAccount.show()
        }
    }

    override fun initData() {
        super.initData()
        var intentData = intent.extras!!.getSerializable("user")

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

    override fun liveObserver() {

        mViewModel.destLive.observe(this) {
            logoOut()
        }
    }

    override fun setLayout() = binding.root


    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(LoginOutEvent())
       // EventBus.getDefault().post(BlueToothConnectEvent())//刷新蓝牙连接信息
        RouteManager.goAct(ARouterUrl.LOGIN_ACT)
        finish()
    }
}