package com.abbidot.tracker.dialog

import android.content.Context
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<*>,
    fenceCheckedChangeListener: CompoundButton.OnCheckedChangeListener? = null,
    dashedCheckedChangeListener: CompoundButton.OnCheckedChangeListener? = null
) : BaseDialog<DialogSelectMapTypeLayoutBinding>(
    DialogSelectMapTypeLayoutBinding::inflate, context
) {
    private var mAdapter = adapter
    private val mFenceCheckedChangeListener = fenceCheckedChangeListener
    private val mDashedCheckedChangeListener = dashedCheckedChangeListener

    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, false)
            cbDialogMapFencesSwitch.setOnCheckedChangeListener(mFenceCheckedChangeListener)
            cbDialogMapDashedLineSwitch.setOnCheckedChangeListener(mDashedCheckedChangeListener)
        }
    }

    /**
     * 检测地图类型有没有改变
     */
    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
    }
}