package com.grkj.iscs.view.presenter

import android.content.Context
import com.grkj.iscs.BusinessManager
import com.grkj.iscs.R
import com.grkj.iscs.extentions.removeLeadingZeros
import com.grkj.iscs.extentions.toHexStrings
import com.grkj.iscs.modbus.ModBusController
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
import com.grkj.iscs.model.vo.card.CardInfoRespVO
import com.grkj.iscs.model.vo.user.UserInfoRespVO
import com.grkj.iscs.util.Executor
import com.grkj.iscs.util.NetApi
import com.grkj.iscs.util.SPUtils
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.view.base.BasePresenter
import com.grkj.iscs.view.iview.ILoginView

class LoginPresenter : BasePresenter<ILoginView>() {

    fun login(context: Context, account: String, pwd: String, callBack: (Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit) {
        if (account.isEmpty()) {
            ToastUtils.tip(context.getString(R.string.please_input_account))
            return
        } else if (account.isEmpty()) {
            ToastUtils.tip(context.getString(R.string.please_input_password))
            return
        }
        NetApi.login(account, pwd) {
            if (it == true) {
                NetApi.getUserInfo { userInfo ->
                    NetApi.getCardInfoByLoginUser { itInfo ->
                        itInfo?.let { info ->
                            SPUtils.setLoginUser(mContext!!, info)
                        }
                        callBack.invoke(it, itInfo, userInfo)
                    }
                }
            } else {
                callBack.invoke(false, null, null)
            }
        }
    }

    fun cardLogin(card: String, callBack: (Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit) {
        NetApi.cardLogin(card) {
            if (it == true) {
                NetApi.getUserInfo { userInfo ->
                    NetApi.getCardInfoByLoginUser { itInfo ->
                        Executor.runOnMain {
                            itInfo?.let { info ->
                                SPUtils.setLoginUser(mContext!!, info)
                            }
                            callBack.invoke(it, itInfo, userInfo)
                        }
                    }
                }
            } else {
                Executor.runOnMain {
                    callBack.invoke(false, null, null)
                }
            }
        }
    }

    fun registerListener() {
        BusinessManager.registerStatusListener(this) { dockBean ->
            dockBean.deviceList.forEach { deviceBean ->
                if (!deviceBean.isExist) {
                    return@forEach
                }
                when (deviceBean.type) {
                    DEVICE_TYPE_CARD -> {
                        ModBusController.readPortalCaseCardRfid(dockBean.addr) { res ->
                            if (res.size >= 11) {
                                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                                LogUtil.i("卡片RFID ： $rfid")
                                // TODO 跳转页面处理
                            }
                        }
                    }
                }
            }
        }
    }

    fun unregisterListener() {
        BusinessManager.unregisterListener(this)
    }
}