package com.abbidot.tracker.adapter

import android.content.Context
import android.view.View
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.constant.ConstantInt
import com.abbidot.tracker.ui.activity.set.FirstPermissionsActivity
import com.abbidot.tracker.widget.TypefaceButton

/**
 *Created  by .yzq on 2024/3/25/025.
 * @link
 * @description:
 */
class FirstPermissionsAdapter(
    ctx: Context, list: MutableList<DataBean>?
) : BaseRecyclerAdapter<DataBean>(ctx, list) {

    private val mPermissionsActivity = ctx as FirstPermissionsActivity

    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: DataBean) {
        val stateView = holder!!.getImageView(R.id.cb_permission_state_item)
        val typefaceTextView = holder.getTextView(R.id.tv_permission_tip_item)
        val btnLayout = holder.getView(R.id.ll_permission_set_layout)
        val btnSkip = holder.getView(R.id.btn_permission_skip_set) as TypefaceButton
        val btnEnable = holder.getView(R.id.btn_permission_enable_set) as TypefaceButton
        btnSkip.setChangeAlphaWhenPress(true)
        btnEnable.setChangeAlphaWhenPress(true)

        if (item.selected) {
            typefaceTextView.visibility = View.VISIBLE
            holder.itemView.setBackgroundResource(R.drawable.shape16_white_color_bg)

            if (item.state == ConstantInt.Type1) {
                btnLayout.visibility = View.GONE
            } else {
                btnLayout.visibility = View.VISIBLE
            }
        } else {
            btnLayout.visibility = View.GONE
            typefaceTextView.visibility = View.GONE
            holder.itemView.setBackgroundResource(R.drawable.shape10_white_bg)
        }

        when (item.state) {
            ConstantInt.SpecialType -> stateView.setImageResource(R.drawable.icon_permission_error_check_svg)
            ConstantInt.Type0 -> stateView.setImageResource(R.drawable.icon_permission_no_check_svg)
            ConstantInt.Type1 -> stateView.setImageResource(R.drawable.icon_permission_check_svg)
        }

        holder.setText(R.id.tv_permission_name_item, item.name)
        typefaceTextView.text = item.value

        btnSkip.setOnClickListener {
            item.state = ConstantInt.SpecialType
            item.selected = false
            notifyItemChanged(position)
//            if (position + 1 < getData().size) {
//                getData()[position + 1].
//            }
        }
        btnEnable.setOnClickListener {
            when (position) {
                0 -> mPermissionsActivity.mPermissionsCommon.operateBluetooth()
                1 -> mPermissionsActivity.mPermissionsCommon.operateGPSLocation()
                2 -> mPermissionsActivity.mPermissionsCommon.operateNotifications(
                    this, ConstantInt.SpecialType
                )
            }
        }

        mPermissionsActivity.updateNextBtnState()
    }
}