package com.grkj.iscs_mc.view.dialog

import android.content.Context
import android.content.res.ColorStateList
import android.view.View
import android.widget.RelativeLayout
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.DialogInspectionBinding
import com.grkj.iscs_mc.view.base.BaseDialog

/**
 * 一键检查弹框
 */
class InspectionDialog(val ctx: Context) : BaseDialog<DialogInspectionBinding>(ctx) {

    private var mBtnList: MutableList<RelativeLayout?> ? = null
    private var confirmListener: ((Int) -> Unit)? = null
    private var mSelected = -1

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

    override fun initView() {
        mBtnList = mutableListOf(mBinding?.rlNormal, mBinding?.rlExpired, mBinding?.rlDamaged)

        mBinding?.btnConfirm?.setOnClickListener {
            confirmListener?.invoke(mSelected)
            dismiss()
        }
        mBinding?.btnCancel?.setOnClickListener {
            dismiss()
        }

        mBinding?.rlNormal?.setOnClickListener {
            setSelect(it)
        }

        mBinding?.rlExpired?.setOnClickListener {
            setSelect(it)
        }

        mBinding?.rlDamaged?.setOnClickListener {
            setSelect(it)
        }
    }

    fun showCount(count: Int) {
        super.show()
        mBinding?.tvTip?.text = ctx.getString(R.string.inspection_dialog_tip, count)
    }

    private fun setSelect(btn: View?) {
        when(btn) {
            mBinding?.rlNormal -> {
                mBinding?.rlNormal?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_green_bg))
                mBinding?.rlExpired?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_yellow_bg_40))
                mBinding?.rlDamaged?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_red_bg_40))
                mSelected = 0
            }
            mBinding?.rlExpired -> {
                mBinding?.rlNormal?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_green_bg_40))
                mBinding?.rlExpired?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_yellow_bg))
                mBinding?.rlDamaged?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_red_bg_40))
                mSelected = 1
            }
            mBinding?.rlDamaged -> {
                mBinding?.rlNormal?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_green_bg_40))
                mBinding?.rlExpired?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_yellow_bg_40))
                mBinding?.rlDamaged?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_red_bg))
                mSelected = 2
            }
            null -> {
                mBinding?.rlNormal?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_green_bg_40))
                mBinding?.rlExpired?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_yellow_bg_40))
                mBinding?.rlDamaged?.backgroundTintList =
                    ColorStateList.valueOf(ctx.getColor(R.color.common_btn_red_bg_40))
                mSelected = 3
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        setSelect(null)
    }

    fun setConfirmListener(listener: (Int) -> Unit) {
        confirmListener = listener
    }
}