package com.abbidot.tracker.adapter

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
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.widget.BatteryView
import com.abbidot.tracker.widget.TypefaceTextView

/**
 * 首页地图宠物警告适配器
 */
class HomeMapDeviceStateAdapter(
    ctx: Context, list: MutableList<DataBean>?
) : BaseRecyclerAdapter<DataBean>(ctx, list) {
    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: DataBean) {
        val typefaceTextView =
            holder!!.getView(R.id.tv_home_map_device_name_item) as TypefaceTextView
        val iv = holder.getView(R.id.iv_home_map_device_item) as AppCompatImageView
        val bv = holder.getView(R.id.bv_home_map_device_power) as BatteryView
        val ivState = holder.getView(R.id.iv_home_map_device_state_item) as AppCompatImageView
        val detailTextView =
            holder.getTextView(R.id.tv_map_device_state_detail_item) as TypefaceTextView

        if (item.menuType == ConstantInt.Open) {
            ivState.setImageResource(R.drawable.icon_map_online)
        } else {
            ivState.setImageResource(R.drawable.icon_map_offline)
        }

        if (item.imageId == 0) {
            iv.visibility = View.GONE
            bv.visibility = View.VISIBLE
            bv.setValue(item.state)
        } else {
            iv.visibility = View.VISIBLE
            bv.visibility = View.GONE
            iv.setImageResource(item.imageId)
        }

        typefaceTextView.text = item.name
        detailTextView.text = item.value

        if (item.selected) {
            detailTextView.visibility = View.VISIBLE
            typefaceTextView.visibility = View.GONE
        } else {
            detailTextView.visibility = View.GONE
            if (TextUtils.isEmpty(item.name)) {
                typefaceTextView.visibility = View.GONE
            } else {
                typefaceTextView.visibility = View.VISIBLE
            }
        }

        holder.itemView.setOnClickListener {
            item.selected = !item.selected
            notifyItemChanged(position)
        }
    }
}