package com.abbidot.tracker.adapter

import android.content.Context
import androidx.appcompat.widget.AppCompatCheckBox
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.ui.activity.device.set.LiveDurationSetActivity
import com.abbidot.tracker.ui.activity.device.set.TrackingDurationSetActivity

/**
 *Created  by .yzq on 2024/6/7/007.
 * @link
 * @description:
 */
class DurationSetAdapter(
    ctx: Context, list: MutableList<DataBean>?
) : BaseRecyclerAdapter<DataBean>(ctx, list) {

    private var mTrackingDurationSetActivity: TrackingDurationSetActivity? = null
    private var mLiveDurationSetActivity: LiveDurationSetActivity? = null

    init {
        if (ctx is TrackingDurationSetActivity) {
            mTrackingDurationSetActivity = ctx
        } else if (ctx is LiveDurationSetActivity) {
            mLiveDurationSetActivity = ctx
        }
    }

    override fun getEmptyLayoutId(viewType: Int) = 0

    override fun getItemLayoutId(viewType: Int) = R.layout.item_duration_set_layout

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: DataBean) {
        holder!!.setText(R.id.tv_duration_name_item, item.name)
        val checkBox = holder.getView(R.id.cb_duration_set_check) as AppCompatCheckBox
        checkBox.apply {
            isChecked = item.selected
            setOnCheckedChangeListener(null)
            setOnClickListener {
                isChecked = item.selected
                if (mContext is TrackingDurationSetActivity) {
                    mTrackingDurationSetActivity?.setTrackingDuration(position)
                } else if (mContext is LiveDurationSetActivity) {
                    mLiveDurationSetActivity?.setLiveDuration(position)
                }
            }
        }
    }
}