package fsir.tqqe

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.view.View
import android.view.View.OnClickListener
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.test.basd.supercleanermax.R
import pxds.lmmd.zmwf.Khko.string

class Gqbi(context: Activity) : Vtzf<Rdrk>, SpanSizeLookup(), OnClickListener {

    private var mCompressionPhotoListAdapter: Jxdi? = null

    private val mContext: Context = context

    private var mBtnContinue: TextView? = null

    private var mRecycleView: RecyclerView? = null

    private val mSelectAll: TextView

    init {
        mRecycleView = context.findViewById(R.id.angrvgf)
        mBtnContinue = context.findViewById(R.id.lpxnxlb)
        mSelectAll = context.findViewById(R.id.ysixnvj)
        init()
    }

    private fun init() {
        (mRecycleView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        mCompressionPhotoListAdapter = Jxdi(this)
        val layoutManager = GridLayoutManager(mContext, 3)
        layoutManager.spanSizeLookup = (this)
        mRecycleView?.layoutManager = layoutManager
        mRecycleView?.adapter = mCompressionPhotoListAdapter
        mBtnContinue?.setOnClickListener(this)
        mSelectAll.setOnClickListener(this)
    }

    override fun onItemClick(position: Int, entity: Rdrk) {
    }

    override fun onUpdateBtn(isChecked: Boolean, entity: Rdrk) {
        Gtor.getInstance().let {
            if (isChecked) it.addPhoto(entity) else it.removePhoto(entity)
        }
        updateBtnContinue()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.lpxnxlb -> {
                val intent = Intent(mContext, Nxko::class.java)
                intent.putExtra(R.string.ޔޒޓޑތ_D.string(), Gtor.getInstance().photoList.size)
                mContext.startActivity(intent)
            }

            R.id.ysixnvj -> {
                mCompressionPhotoListAdapter?.setNoOrYesselectAll((mCompressionPhotoListAdapter?.isSelectAll() != true))
            }
        }

    }

    override fun getSpanSize(position: Int): Int {
        val entity = mCompressionPhotoListAdapter?.getData()?.get(position)
        return if ((entity?.type ?: 0) != 0) {
            3
        } else {
            1
        }
    }

    private fun updateBtnContinue() {
        mBtnContinue?.isEnabled = Gtor.getInstance().photoList.size > 0
        val int = Gtor.getInstance().photoList.size
        mBtnContinue?.text = if (mBtnContinue?.isEnabled == true) R.string.ޅޒޕޠމ_D.string((int).toString()) else R.string.ބނޢޛޗ_D.string()

        mCompressionPhotoListAdapter?.isSelectAll()
        val drawable: Drawable? = if (mCompressionPhotoListAdapter?.isSelectAll() == true) {
            ContextCompat.getDrawable(mContext, R.mipmap.gqtxvcys)
        } else {
            ContextCompat.getDrawable(mContext, R.mipmap.adksokcq)
        }
        mSelectAll.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable, null, null)
    }

    fun onSelectAlbum(album: Kikc) {
        val list = Lpgx.getPhotosForFolder(album.name, mContext)
        if (list.isNotEmpty()) {
            Gtor.getInstance().clearCache()
            mCompressionPhotoListAdapter?.updateData(list)
            updateBtnContinue()
        }
    }
}