package com.abbidot.tracker.adapter

import android.content.Context
import android.content.Intent
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.PetBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.ui.activity.pet.PetProfileActivity
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.widget.TypefaceRoundButton

/**
 *Created  by .yzq on 2022/1/8/008.
 * @link
 * @description:切换宠物列表弹窗适配器
 */
class ChangePetListDialogAdapter(ctx: Context, list: MutableList<PetBean>?) :
    BaseRecyclerAdapter<PetBean>(ctx, list) {

    private var mOnChangeClickListener: OnChangeClickListener? = null

    //选中的宠物下标
    private var mSelectPetPos = 0

    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: PetBean) {
        val current =
            holder!!.itemView.findViewById<TypefaceRoundButton>(R.id.dialog_my_pet_current_btn)
        val change =
            holder.itemView.findViewById<TypefaceRoundButton>(R.id.dialog_my_pet_change_btn)

        holder.setText(R.id.dialog_my_pet_name, item.petName)

        holder.getImageView(R.id.dialog_my_pet_head).apply {
            ViewUtil.instance.setPetTypeHead(this, item.imgurl, item.petType)
            setOnClickListener {
                if (null != mOnChangeClickListener) {
                    mOnChangeClickListener!!.onCurrentClick()
                }
                val intent = Intent(mContext, PetProfileActivity::class.java)
                intent.putExtra(ConstantString.Edit, true)
                intent.putExtra(ConstantString.Pet, item)
                mContext.startActivity(intent)
            }
        }

        if (position == mSelectPetPos) {
            current.visibility = View.VISIBLE
            change.visibility = View.GONE
        } else {
            current.visibility = View.GONE
            change.visibility = View.VISIBLE
        }

        current.setChangeAlphaWhenPress(true)
        current.setOnClickListener {
            if (null != mOnChangeClickListener) {
                mOnChangeClickListener!!.onCurrentClick()
            }
        }

        change.setChangeAlphaWhenPress(true)
        change.setOnClickListener {
            mSelectPetPos = position
            notifyItemRangeChanged(0, getData().size)
            if (null != mOnChangeClickListener) {
                mOnChangeClickListener!!.onChangeClick(position)
            }
        }
    }

    fun setSelectPetPos(position: Int) {
        mSelectPetPos = position
    }

    fun setOnChangeClickListener(onChangeClickListener: OnChangeClickListener) {
        mOnChangeClickListener = onChangeClickListener
    }

    interface OnChangeClickListener {
        fun onCurrentClick()
        fun onChangeClick(position: Int)
    }
}