package com.grkj.iscs.view.fragment

import android.content.Intent
import com.grkj.iscs.BusinessManager
import com.grkj.iscs.databinding.FragmentSettingBinding
import com.grkj.iscs.modbus.ModBusController.controlKeyBuckle
import com.grkj.iscs.modbus.ModBusController.controlLockBuckle
import com.grkj.iscs.modbus.ModBusController.dockList
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
import com.grkj.iscs.util.NetApi
import com.grkj.iscs.view.activity.LoginActivity
import com.grkj.iscs.view.base.BaseFragment

/**
 * 设置页
 */
class SettingFragment : BaseFragment<FragmentSettingBinding>() {

    override val viewBinding: FragmentSettingBinding
        get() = FragmentSettingBinding.inflate(layoutInflater)

    override fun initView() {
        mBinding?.cbLogout?.setOnClickListener {
            NetApi.logout()
            // 关所有有设备的卡扣
            dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
                val hasLockIdxList = dockBean.getLockList().filter { it.isExist }.map { it.idx } as MutableList<Int>
                controlLockBuckle(false, dockBean.addr, hasLockIdxList)
            }
            dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
                dockBean.getKeyList().forEach { key ->
                    if (key.isExist) {
                        controlKeyBuckle(false, key.isLeft, dockBean.addr)
                    }
                }
            }
            // 不拿的设备不归你，下次登录重新按需分配
            BusinessManager.mDeviceTakeList.clear()
            startActivity(Intent(context, LoginActivity::class.java))
        }
    }
}