package com.zxhl.main.page.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseRecyclerAdapter
import com.zxhl.cms.net.model.box.DepotEntity
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecycleImageView
import com.zxhl.main.R
import com.zxhl.main.page.activity.GoodsReceiveActivity
import com.zxhl.main.page.activity.OrderDetailActivity
import com.zxhl.main.page.activity.ReceiveAddressActivity

class PickUpAdapter : BaseRecyclerAdapter<DepotEntity, RecyclerView.ViewHolder> {
    private var mContext: Context? = null
    private val listener: OnRecycleItemClickListener<Boolean>
    private val CONTENT = 1000
    private val LOADMORE = 1001
    private val checkStatus: MutableMap<Int, Boolean> = HashMap()
    private var type: Int? = 0

    //更改集合内部存储的状态
    fun initCheck(flag: Boolean) {
        for (i in 0 until mList.size) {
            //更改指定位置的数据
            checkStatus.put(i, flag)
        }
    }

    constructor(
        content: Context?,
        listener: OnRecycleItemClickListener<Boolean>
    ) : super() {
        mContext = content
        this.listener = listener
        initCheck(false)

    }

    fun setTihuo(type: Int?) {
        this.type = type
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        val size = mList.size
        if (size > 0) {
            return if (isLoadMore) {
                mList.size + 1
            } else {
                mList.size
            }
        } else {
            return size
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (isLoadMore) {
            if (position == mList.size) {
                return LOADMORE
            } else {
                return CONTENT
            }
        } else {
            return CONTENT
        }
    }

    private var isAllSelect = false

    fun setAllSelect(isAllSelect: Boolean) {
        this.isAllSelect = isAllSelect
    }

    private var isCancelAllSelect = false

    fun setCancelAllSelect(isCancelAllSelect: Boolean) {
        this.isCancelAllSelect = isCancelAllSelect
        if (isCancelAllSelect) {
            initCheck(false)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ViewHolder) {
            var bean = mList[position]
            if (type == 0) {
                holder.item_tv_xuni_good_tihuo?.visibility = View.GONE
            } else {
                holder.item_tv_xuni_good_tihuo?.visibility = View.VISIBLE
            }
            holder.item_img_choose?.isChecked = checkStatus[position] ?: false
            holder.item_tv_xuni_good_tihuo?.setOnClickListener {
                val intent = Intent(mContext, GoodsReceiveActivity::class.java)
                intent.putExtra("goodsbean", bean)
                mContext?.startActivity(intent)

            }
            if (bean.boxId.equals("-2")) {
                holder.item_img_xinren?.visibility = View.VISIBLE
            } else {
                holder.item_img_xinren?.visibility = View.GONE
            }
            if (isAllSelect) {
                holder.item_img_choose?.isChecked = true
            }

            if (isCancelAllSelect) {
                holder.item_img_choose?.isChecked = false
                if (position == (mList.size - 1)) {
                    isCancelAllSelect = false
                }
            }


            holder.item_img_goods?.setLoadImageUrl(bean.products?.mainImage, false)
            holder.item_tv_good_name?.text = bean.products?.productName
            holder.item_tv_good_type?.text = bean.products?.cateName
            holder.item_tv_good_price?.text = bean.amountSpent
        } else if (holder is LoadMoreHolder) {
            Glide.with(AppContext.get()).asGif().load(R.drawable.load_more).into(holder.mImg)
        }

    }

    private var isLoadMore = true
    fun setLoadMore(isBoolean: Boolean) {
        isLoadMore = isBoolean
    }

    fun getLoadMore(): Boolean = isLoadMore

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (isLoadMore) {
            if (viewType == LOADMORE) {
                return LoadMoreHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_load_more, parent, false)
                )
            } else {
                return ViewHolder(
                    LayoutInflater.from(mContext)
                        .inflate(R.layout.item_layout_pick_up, parent, false)
                )
            }
        } else {
            return ViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_layout_pick_up, parent, false)
            )
        }


    }

    inner class ViewHolder : RecyclerView.ViewHolder {


        var item_rl: RelativeLayout?
        var item_img_choose: CheckBox?
        var item_img_goods: RecycleImageView?
        var item_tv_good_type: TextView?
        var item_tv_good_name: TextView?
        var item_tv_good_price: TextView?
        var item_tv_xuni_good_tihuo: Button?
        var item_img_xinren: ImageView?


        constructor(itemView: View) : super(itemView) {

            item_tv_good_type = itemView.findViewById<TextView>(R.id.id_goods_type)
            item_tv_good_name = itemView.findViewById<TextView>(R.id.id_goods_name)
            item_tv_good_price = itemView.findViewById<TextView>(R.id.id_tv_good_price)
            item_img_choose = itemView.findViewById<CheckBox>(R.id.id_img_choose)
            item_rl = itemView.findViewById<RelativeLayout>(R.id.id_rl_item)
            item_img_goods = itemView.findViewById<RecycleImageView>(R.id.id_goods_img)
            item_tv_xuni_good_tihuo = itemView.findViewById<Button>(R.id.id_tv_tihuo)
            item_img_xinren = itemView.findViewById<ImageView>(R.id.id_img_xiren)

            item_img_choose?.setOnCheckedChangeListener { compoundButton, b ->
                val pos = adapterPosition
                if (pos >= 0 && pos < mList.size) {
                    if (b) {
                        item_img_choose?.setBackgroundResource(R.drawable.icon_xuanzhong)
                    } else {
                        item_img_choose?.setBackgroundResource(R.drawable.icon_xuanze)
                    }
                    checkStatus.put(position, b)
                    listener.onItemClick(item_rl, position, b)
                }
            }

            item_rl?.setOnClickListener {
                item_img_choose?.isChecked = !(item_img_choose?.isChecked ?: false)
//                notifyItemChanged(adapterPosition)
            }
        }
    }

    class LoadMoreHolder : RecyclerView.ViewHolder {

        val mImg: ImageView

        constructor(itemView: View) : super(itemView) {
            mImg = itemView.findViewById(R.id.id_item_more_img)
        }
    }
}