package fsir.tqqe

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.supercleanermax.R
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string

class Jxdi : RecyclerView.Adapter<ViewHolder> {

    private val mListener: Vtzf<Rdrk>

    private var mHastMap: HashMap<Int, Boolean> = HashMap()

    private val HEAD = 10081

    private val CONTENT = 10082

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

    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)
                R.string.ޕދހޓޠ_D.string() else R.string.ޏޔގޓޣ_D.string()
        }

    }

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

    constructor(mListener: Vtzf<Rdrk>) : super() {
        this.mListener = mListener
    }

    override fun getItemCount(): Int = 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.jwyqmbk)
            mImageView = itemView.findViewById(R.id.numbvea)
            mSize = itemView.findViewById(R.id.vnqlpnf)
            mRatio = itemView.findViewById(R.id.yytoxhu)
            val layoutParams = mImageView.layoutParams
            layoutParams.height = Snjq.calculateImageWidth(itemView.context)
            mImageView.layoutParams = layoutParams
            itemView.setTrackedOnClickListener {
                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)
                }

            }
        }
    }

    inner class HeadViewHolder : ViewHolder {

        val mSelectAll: TextView
        val mTitle: TextView

        constructor(itemView: View) : super(itemView) {
            mSelectAll = itemView.findViewById(R.id.ysixnvj)
            mTitle = itemView.findViewById(R.id.gsdtmyk)

            mSelectAll.setTrackedOnClickListener {
                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(R.string.ޕދހޓޠ_D.string(), text)
                            mHastMap[index] = boolean
                            if (photoBean.type == 0) {
                                mListener.onUpdateBtn(boolean, photoBean)
                            }
                        }
                    }

                    mSelectAll.text = when (text) {
                        R.string.ޕދހޓޠ_D.string() -> R.string.ޏޔގޓޣ_D.string()
                        else -> R.string.ޕދހޓޠ_D.string()
                    }
                    notifyItemRangeChanged(0, mList?.size ?: 0)
                }

            }
        }
    }

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

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

    private var mList: MutableList<Rdrk> = ArrayList()

    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.ldobqn, parent, false)
            )
        } else {
            return ContentViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.duwkag, parent, false)
            )
        }
    }
}