package com.test.basd.onecleanmaster.activity.photocompress.photo

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
import com.test.basd.onecleanmaster.R

class CompressionPhotoListAdapter : RecyclerView.Adapter<ViewHolder> {

    private val HEAD = 10081
    private val CONTENT = 10082

    private var mList: MutableList<PhotoBean> = ArrayList()
    private var mHastMap: HashMap<Int, Boolean> = HashMap()

    private val mListener: OnItemClickListener<PhotoBean>

    constructor(mListener: OnItemClickListener<PhotoBean>) : super() {
        this.mListener = mListener
    }

    fun updateData(mList: List<PhotoBean>) {
        this.mList?.clear()
        this.mList?.addAll(mList)
        this.mHastMap.clear()
        notifyDataSetChanged()
    }

    fun getData(): List<PhotoBean>? {
        return mList
    }

    fun isSelectAll(): Boolean {
        if (mHastMap.count() == itemCount) {
            return mHastMap.all { it.value }
        }
        return false
    }

    fun setNoOrYesselectAll(boolean: Boolean) {
        for (i in 0 until itemCount) {
            mHastMap[i] = boolean
            val entity = mList[i]
            if (entity.type == 0) {
                mListener.onUpdateBtn(boolean, entity)
            }
        }
        notifyItemRangeChanged(0, itemCount)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (viewType == HEAD) {
            return HeadViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_head, parent, false)
            )
        } else {
            return ContentViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_content, parent, false)
            )
        }
    }

    override fun getItemViewType(position: Int): Int {
        val data = mList[position]
        return if (data.type == 0) {
            CONTENT
        } else {
            HEAD
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = mList[position]
        if (holder is ContentViewHolder) {
            holder.mCheck.isChecked = mHastMap[position] ?: false
            Glide.with(holder.mImageView.context).load(entity.src)
                .into(holder.mImageView)

            holder.mRatio.text = entity.whRatio
            holder.mSize.text = entity.sizeText
        } else if (holder is HeadViewHolder) {
            holder.mTitle.text = entity.date
            holder.mSelectAll.text = if (mHastMap[position] == true)
                "DESELECT ALL" else "SELECT ALL"
        }

    }

    override fun getItemCount(): Int = mList?.size ?: 0


    inner class HeadViewHolder : ViewHolder {

        val mSelectAll: TextView
        val mTitle: TextView


        constructor(itemView: View) : super(itemView) {
            mSelectAll = itemView.findViewById(R.id.select_all)
            mTitle = itemView.findViewById(R.id.title)

            mSelectAll.setOnClickListener {
                val text = mSelectAll.text
                if(position>=0&&position<mList.size){
                    val data = mList[position]
                    val date = data?.date
                    mList?.forEachIndexed { index, photoBean ->
                        if (TextUtils.equals(photoBean.date, date)) {
                            val boolean = !TextUtils.equals("DESELECT ALL", text)
                            mHastMap[index] = boolean
                            if (photoBean.type == 0) {
                                mListener.onUpdateBtn(boolean, photoBean)
                            }
                        }
                    }

                    mSelectAll.text = when (text) {
                        "DESELECT ALL" -> "SELECT ALL"
                        else -> "DESELECT ALL"
                    }
                    notifyItemRangeChanged(0, mList?.size ?: 0)
                }

            }
        }
    }

    inner class ContentViewHolder : ViewHolder {

        val mCheck: CheckBox
        val mImageView: RoundedImageView

        val mSize: TextView
        val mRatio: TextView

        constructor(itemView: View) : super(itemView) {
            mCheck = itemView.findViewById(R.id.checkbox)
            mImageView = itemView.findViewById(R.id.image)
            mSize = itemView.findViewById(R.id.size)
            mRatio = itemView.findViewById(R.id.ratio)
            val layoutParams = mImageView.layoutParams
            layoutParams.height = Utils.calculateImageWidth(itemView.context)
            mImageView.layoutParams = layoutParams
            itemView.setOnClickListener {
                mCheck.performClick()
                if(position>=0&&position<mList.size){
                    val entity = mList[position]
                    val index =
                        mList.indexOfFirst { it.type == 1 && TextUtils.equals(entity.date, it.date) }
                    notifyItemChanged(index)
                }

            }
            mCheck.setOnCheckedChangeListener { buttonView, isChecked ->
                if(position>=0&&position<mList.size){
                    val entity = mList[position]
                    mHastMap.put(position, isChecked)
                    mListener.onUpdateBtn(isChecked, entity)

                    val indexList: MutableList<Int> = ArrayList()
                    val date = entity?.date ?: ""
                    mList?.forEachIndexed { index, photoBean ->
                        if (photoBean.type == 0 && TextUtils.equals(photoBean.date, date)) {
                            indexList.add(index)
                        }
                    }
                    val allTrue = (indexList.first()..indexList.last()).all { i ->
                        mHastMap.getOrDefault(
                            i,
                            false
                        )
                    }
                    mHastMap.put(indexList.first() - 1, allTrue)
                }



            }
        }
    }
}