package com.grkj.iscs.presentation

import androidx.appcompat.app.AlertDialog
import com.grkj.iscs.view.base.BaseMvpActivity
import com.grkj.iscs.databinding.ActivityLockerTogetherBinding
import com.grkj.iscs.util.Executor
import com.grkj.iscs.util.ToastUtils

/**
 * 共锁人
 */
class LockerTogetherActivity :
    BaseMvpActivity<ILockerTogetherView, LockerTogetherPresenter, ActivityLockerTogetherBinding>() {

    private var cardRfid: String? = null

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

    override fun initView() {
        cardRfid = intent.getStringExtra("card")
    }

    fun showDlg(rfid: String) {
        Executor.runOnMain {
            val togetherStatus = PresentationManager.checkTogetherLocker(rfid)
            if (togetherStatus != null) {
                val str = when (togetherStatus) {
                    0 -> "确定要共锁吗？"
                    1 -> "确定要解除共锁吗？"
                    else -> ""
                }
                if (togetherStatus == 100) {
                    ToastUtils.tip("请等待上锁人上锁")
                    return@runOnMain
                }
                if (togetherStatus == 2) {
                    ToastUtils.tip("共锁已完成")
                    return@runOnMain
                }
                AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage(str)
                    .setPositiveButton("确定") { _, _ ->
                        PresentationManager.updateTogetherLockerStatus(cardRfid!!, togetherStatus)
                    }
                    .setNegativeButton("取消") { dialog, _ ->
                        dialog.dismiss()
                    }
                    .create().show()
            }
        }
    }

    override fun initPresenter(): LockerTogetherPresenter {
        return LockerTogetherPresenter()
    }
}