package com.abbidot.tracker.ui.activity.device.set

import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.DurationSetAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityLiveDurationSetBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.TrackerSetViewModel

class LiveDurationSetActivity :
    BaseActivity<ActivityLiveDurationSetBinding>(ActivityLiveDurationSetBinding::inflate) {

    private val mTrackerSetViewModel: TrackerSetViewModel by viewModels()

    private lateinit var mDurationSetAdapter: DurationSetAdapter

    //选中的下标
    private var mSelectIndex = -1
    private var mSelectTempIndex = -1

    private var mMapDeviceBean: MapDeviceBean? = null

    override fun getTopBar() = mViewBinding.ilLiveDurationSetTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_live_duration)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        intent.extras?.apply {
            mMapDeviceBean = Util.getParcelableAdaptive(
                intent, ConstantString.MapDeviceBean, MapDeviceBean::class.java
            )
        }

        setListData()

        mViewBinding.apply {
            ViewUtil.instance.setRecyclerViewVerticalLinearLayout(
                mContext, rvLiveDurationSetList, mDurationSetAdapter, top = AppUtils.dpToPx(10)
            )
        }
    }

    override fun liveDataObserve() {
        mTrackerSetViewModel.mSetLiveTimeLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    showToast(R.string.txt_change_successful)
                    mSelectIndex = mSelectTempIndex
                    ViewUtil.instance.updateDataBeanOnlySelectedItem(
                        mDurationSetAdapter, mDurationSetAdapter.getData(), mSelectIndex
                    )
                }
            })
        }
    }

    private fun setListData() {
        val durationList = mutableListOf<DataBean>()
        val timeList = mutableListOf(5, 10, 15, 30, 60)
        for (time in timeList) {
            DataBean().apply {
                imageId = time
                name = String.format(getString(R.string.txt_duration_min_unit), "$time")
                durationList.add(this)
            }
        }

        mMapDeviceBean?.apply {
            for (i in 0 until durationList.size) {
                val data = durationList[i]
                if (liveTime / 60 == data.imageId) {
                    mSelectIndex = i
                    data.selected = true
                    break
                }
            }
        }
        mDurationSetAdapter = DurationSetAdapter(this, durationList).apply {
            setOnItemClickListener(object : BaseRecyclerAdapter.OnItemClickListener {
                override fun onItemClick(itemView: View?, pos: Int) {
                    setLiveDuration(pos)
                }
            })
        }
    }

    fun setLiveDuration(pos: Int) {
        if (mSelectIndex == pos) return
        mMapDeviceBean?.apply {
            mSelectTempIndex = pos
            val dataBean = mDurationSetAdapter.getData()[pos]
            mTrackerSetViewModel.setLiveTime(
                this@LiveDurationSetActivity, deviceId, dataBean.imageId
            )
        }
    }
}