package com.abbidot.tracker.dialog

import android.content.Context
import android.view.View
import android.widget.CompoundButton
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.databinding.DialogSelectMapTypeLayoutBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil

/**
 *Created  by .yzq on 2022/1/7/007.
 * @link
 * @description:选择地图类型弹窗
 */
class SelectMapTypeDialog(
    context: Context,
    adapter: BaseRecyclerAdapter<*>,
    fenceCheckedListener: CompoundButton.OnCheckedChangeListener? = null,
    dashedCheckedListener: CompoundButton.OnCheckedChangeListener? = null,
    showAllPetCheckedListener: CompoundButton.OnCheckedChangeListener? = null
) : BaseDialog<DialogSelectMapTypeLayoutBinding>(
    DialogSelectMapTypeLayoutBinding::inflate, context
) {
    private var mAdapter = adapter
    private val mFenceCheckedListener = fenceCheckedListener
    private val mDashedCheckedListener = dashedCheckedListener
    private val mShowAllPetCheckedListener = showAllPetCheckedListener

    override fun initView() {
        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewFlexboxLayout(
                context, rvShowMapTypeList, mAdapter, right = AppUtils.dpToPx(58)
            )
            cbDialogMapFencesSwitch.isChecked = Util.getShowFenceSp()
            cbDialogMapDashedLineSwitch.isChecked = MMKVUtil.getBoolean(MMKVKey.ShowDashedLine)
            cbDialogMapAllPetSwitch.isChecked = MMKVUtil.getBoolean(MMKVKey.ShowAllPet)
            cbDialogMapFencesSwitch.setOnCheckedChangeListener(mFenceCheckedListener)
            cbDialogMapDashedLineSwitch.setOnCheckedChangeListener(mDashedCheckedListener)
            cbDialogMapAllPetSwitch.setOnCheckedChangeListener(mShowAllPetCheckedListener)
        }
    }


    /**
     * 检测地图类型有没有改变
     */
    fun mapTypeSpToUpdate() {
        val mapType = Util.getMapTypeSp()
        val list = mAdapter.getData()
        for (i in 0 until list.size) {
            val l = list[i]
            if (l is DataBean) {
                if (i == mapType && !l.selected) {
                    l.selected = true
                    mAdapter.notifyItemChanged(i)
                } else if (i != mapType && l.selected) {
                    l.selected = false
                    mAdapter.notifyItemChanged(i)
                }
            }
        }
    }

    fun setFencesSwitch(checked: Boolean) {
        mViewBinding.cbDialogMapFencesSwitch.isChecked = checked
    }

    fun setDashedSwitch(checked: Boolean) {
        mViewBinding.cbDialogMapDashedLineSwitch.isChecked = checked
    }

    fun setShowAllPetSwitch(checked: Boolean) {
        mViewBinding.cbDialogMapAllPetSwitch.isChecked = checked
    }

    /**
     * 是否显示按钮
     */
    fun showAllPetSwitch(isShow: Boolean) {
        mViewBinding.llMapTypeShowAllPetSwitch.visibility = if (isShow) View.VISIBLE else View.GONE
    }
}