package com.abbidot.tracker.adapter

import android.content.Context
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.MenuTxtBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.MultipleEntity
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.widget.BatteryView
import com.abbidot.tracker.widget.MyBluetoothAnimView
import com.abbidot.tracker.widget.TypefaceTextView

/**
 * 首页宠物设备状态
 */
class HomePetTrackStateAdapter(
    ctx: Context, list: MutableList<MenuTxtBean>?
) : BaseRecyclerAdapter<MenuTxtBean>(ctx, list) {
    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: MenuTxtBean) {
        val stateTextView = holder!!.getView(R.id.tv_home_pet_device_state_item) as TypefaceTextView
        stateTextView.text = item.menuName

        val stateName = holder.getTextView(R.id.tv_home_pet_device_state_name_item).apply {
            text = item.menuValue
            val color = if (item.type == ConstantInt.SpecialType) R.color.grey_color
            else item.colorRedId
            setTextColor(ContextCompat.getColor(mContext, color))
        }
        val btnState = holder.getImageView(R.id.iv_home_pet_btn_state)

        val color = if (item.type == ConstantInt.SpecialType) R.color.grey_color
        else R.color.data_black_color
        stateTextView.setTextColor(ContextCompat.getColor(mContext, color))

        val bv = holder.getView(R.id.bv_home_pet_device_power) as BatteryView
        val bluetoothAnimView =
            holder.getView(R.id.mba_home_pet_device_search_anim_item) as MyBluetoothAnimView
        when (item.menuType) {
            MultipleEntity.IMG_TEXT_IMG -> {
                stateName.visibility = View.VISIBLE
                bluetoothAnimView.visibility = View.GONE
                btnState.visibility = View.GONE
                bv.visibility = View.VISIBLE
                bv.setValue(item.imageId)
                bluetoothAnimView.setBluetoothAnimStartAndStop(item.isSwitch)
                stateTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
            }

            MultipleEntity.TEXT_TEXT_IMG -> {
                stateName.visibility = View.VISIBLE
                bluetoothAnimView.visibility = View.VISIBLE
                bv.visibility = View.GONE
                btnState.visibility = View.GONE
                bluetoothAnimView.setBluetoothAnimStartAndStop(item.isSwitch)
                stateTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
            }

            else -> {
                bv.visibility = View.GONE
                bluetoothAnimView.visibility = View.GONE
                val drawable = ContextCompat.getDrawable(mContext, item.imageId)
                if (null == drawable) {
                    stateTextView.setCompoundDrawablesWithIntrinsicBounds(0, item.imageId, 0, 0)
                } else {
                    if (item.type == ConstantInt.SpecialType) {
                        drawable.setTint(ContextCompat.getColor(mContext, R.color.grey_color))
                    } else {
                        drawable.setTint(ContextCompat.getColor(mContext, R.color.data_black_color))
                    }
                    stateTextView.setCompoundDrawablesWithIntrinsicBounds(
                        null, drawable, null, null
                    )
                }
                btnState.apply {
                    ViewUtil.instance.viewRotationAnimator(this, true)
                    visibility = if (item.state == ConstantInt.Type1) {
                        stateName.visibility = View.GONE
                        View.VISIBLE
                    } else {
                        stateName.visibility = View.VISIBLE
                        View.GONE
                    }
                }
            }
        }
    }
}