package pxds.etmo

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.FuoxsdBinding
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Lwfm
import pxds.lmmd.zmwf.Oejm
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc
import pxds.vazp.Xwft

class Fjda: Bpfw<FuoxsdBinding>() {

    override val binding: FuoxsdBinding by lazy {
        FuoxsdBinding.inflate(layoutInflater)
    }

    private var fileList = mutableListOf<Xwft>()

    private var isSelectAll = false

    private val mAdapter by lazy {
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val ivImage: ImageView
            val ivSelect: ImageView

            init {
                ivImage = view.findViewById(R.id.ojkbsfv)
                ivSelect = view.findViewById(R.id.raznyaa)
            }
        }

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val view = LayoutInflater.from(this@Fjda).inflate(
                    R.layout.faawag, parent, false
                )
                return ViewHolder(view)
            }

            override fun getItemCount() = fileList.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = fileList[position]
                Glide.with(Lwfm.context).load(item.path).into(holder.ivImage)
                holder.ivSelect.isVisible = true
                holder.itemView.setTrackedOnClickListener {

                    item.isSelect = !item.isSelect
                    notifyItemChanged(position, 1)

                    if (fileList.all { it.isSelect }) {
                        isSelectAll = true
                        binding.frzabrj.text = R.string.ބދޛޠލ_D.string()
                        binding.rsvaqrt.setImageResource(R.mipmap.kteryjkw)
                    } else {
                        isSelectAll = false
                        binding.frzabrj.text = R.string.ޣމޒއހ_D.string()
                        binding.rsvaqrt.setImageResource(R.mipmap.dwwkrqym)
                    }
                    setTvStatus()
                }
            }

            override fun onBindViewHolder(
                holder: ViewHolder, position: Int, payloads: MutableList<Any>
            ) {
                super.onBindViewHolder(holder, position, payloads)
                val item = fileList[position]
                holder.ivSelect.setImageResource(
                    if (item.isSelect) {
                        R.mipmap.kteryjkw
                    } else {
                        R.mipmap.dwwkrqym
                    }
                )
            }
        }
    }

    override fun initListener(){
        binding.hirpzcs.setTrackedOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                Utdc.showInterstitialAd(this@Fjda){
                    finishToMain()
                }

            }
        })

        binding.rsvaqrt.setTrackedOnClickListener {
            if (isSelectAll) {
                isSelectAll = false
                binding.frzabrj.text = R.string.ޣމޒއހ_D.string()
                binding.rsvaqrt.setImageResource(R.mipmap.dwwkrqym)
            } else {
                isSelectAll = true
                binding.frzabrj.text = R.string.ބދޛޠލ_D.string()
                binding.rsvaqrt.setImageResource(R.mipmap.kteryjkw)
            }
            fileList.forEach { it.isSelect = isSelectAll }
            mAdapter.notifyItemRangeChanged(0, fileList.size, 1)
            setTvStatus()
        }
        binding.lxvvmos.setTrackedOnClickListener {
            Utdc.showInterstitialAd(this){
                val list = fileList.toList().filter { it.isSelect }
                if (list.isNotEmpty() && list.all { FileUtils.delete(it.path) }) {
                    fileList.removeAll(list)
                    Oejm.similarImageList.removeAll(list)
                    mAdapter.notifyDataSetChanged()
                    setTvStatus()
                    ToastUtils.showShort(R.string.ލޏތޘޚ_D.string())
                } else {
                    ToastUtils.showShort(R.string.ބޠޏއޢ_D.string())
                }
            }

        }
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.lxvvmos.isEnabled = false
        initListener()
    }

    override fun onPermissionsResult(isGranted: Boolean) {
        if(isGranted){
            Oejm.getImageFiles {
                Utdc.showInterstitialAd(this)
                binding.yomuden.isVisible = false
                binding.ahekezy.isVisible = true
                fileList = Oejm.similarImageList
                binding.vezjukb.isVisible = fileList.size == 0
                binding.lxvvmos.isVisible = fileList.size > 0
                binding.frzabrj.isVisible = fileList.size > 0
                binding.rsvaqrt.isVisible = fileList.size > 0
                binding.meefcha.layoutManager = GridLayoutManager(this, 3)
                binding.meefcha.adapter = mAdapter
            }
        }else{
            finishToMain()
        }
    }

    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    private fun setTvStatus() {
        val it = fileList.filter { it.isSelect }
        binding.lxvvmos.isEnabled = it.isNotEmpty()
    }
}