package com.abbidot.tracker.adapter

import android.content.Context
import android.content.Intent
import android.text.TextUtils
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.DeviceBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.ui.activity.device.AddNewTracker1Activity
import com.abbidot.tracker.ui.activity.device.MyTrackerV2Activity
import com.abbidot.tracker.ui.activity.pet.PetProfileActivity
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.qmuiteam.qmui.widget.QMUIRadiusImageView

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

    private val mMyTrackerActivity = mContext as MyTrackerV2Activity

    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: DeviceBean) {
        val batteryTextView = holder!!.getTextView(R.id.iv_item_my_tracker_v2_battery)
        val trackerImageView = holder.getImageView(R.id.iv_item_my_tracker_v2_device)
        val addTrackerLayout = holder.getView(R.id.ll_my_tracker_v2_add_tracker_layout)
        val petAddLayout = holder.getView(R.id.rl_my_tracker_v2_pet)
        val petHead = holder.getView(R.id.il_my_tracker_v2_pet_head) as QMUIRadiusImageView
        val addPetLayout = holder.getView(R.id.ll_my_tracker_v2_add_pet_layout)
        val trackerName = holder.getTextView(R.id.tv_my_tracker_v2_device_name)
        val linkImage = holder.getImageView(R.id.iv_my_tracker_v2_link_image)
        val petName = holder.getTextView(R.id.tv_my_tracker_v2_pet_name)

        holder.getImageView(R.id.iv_my_tracker_v2_share).apply {
            visibility = if (item.shared == ConstantInt.NoShare) View.GONE else View.VISIBLE
        }

        addTrackerLayout.setOnClickListener {
            if (itemCount - 1 == position) {
                mMyTrackerActivity.startActivity(
                    Intent(
                        mContext, AddNewTracker1Activity::class.java
                    )
                )
            } else {
                mMyTrackerActivity.getAssignData(position)
            }
        }
        addPetLayout.setOnClickListener {
            if (itemCount - 1 == position) {
                mMyTrackerActivity.startActivity(Intent(mContext, PetProfileActivity::class.java))
            } else {
                mMyTrackerActivity.getAssignData(position)
            }
        }

        trackerImageView.setOnClickListener {
            if (item.shared == ConstantInt.NoShare) {
                mMyTrackerActivity.showDeviceBottomSheetMenu(position)
            }
        }
        petHead.setOnClickListener {
            if (item.shared == ConstantInt.NoShare) {
                mMyTrackerActivity.showPetBottomSheetMenu(position)
            }
        }

        if (itemCount - 1 == position) {
            batteryTextView.visibility = View.GONE
            trackerImageView.visibility = View.GONE
            trackerName.visibility = View.GONE
            addTrackerLayout.visibility = View.VISIBLE

            petAddLayout.visibility = View.GONE
            addPetLayout.visibility = View.GONE
            linkImage.visibility = View.GONE
            petHead.visibility = View.GONE
            petName.visibility = View.GONE
        } else {
            petAddLayout.visibility = View.VISIBLE
            linkImage.visibility = View.VISIBLE
            if (TextUtils.isEmpty(item.deviceOutId)) {
                addTrackerLayout.visibility = View.VISIBLE
                batteryTextView.visibility = View.GONE
                trackerImageView.visibility = View.GONE
                trackerName.visibility = View.GONE
            } else {
                addTrackerLayout.visibility = View.GONE
                batteryTextView.visibility = View.VISIBLE
                trackerImageView.visibility = View.VISIBLE
                trackerName.visibility = View.VISIBLE

                trackerImageView.setImageResource(Util.getDeviceTypeImage(item.deviceType))
                trackerName.text = item.deviceOutId
                val battery = "${item.batteryCapacity} "
                batteryTextView.text = battery
            }
            if (null == item.pet) {
                addPetLayout.visibility = View.VISIBLE
                petHead.visibility = View.GONE
                petName.visibility = View.GONE
            } else {
                addPetLayout.visibility = View.GONE
                petHead.visibility = View.VISIBLE
                petName.visibility = View.VISIBLE
                item.pet?.let {
                    petName.text = it.petName
                    ViewUtil.instance.setPetTypeHead(petHead, it.imgurl, it.petType)
                }
            }
        }
    }
}