package rallyedto.finishedto.pollutionedto

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.base.datarecovery.R
import com.base.datarecovery.databinding.AcceptableedtoBinding
import com.bumptech.glide.Glide
import rallyedto.battleedto.acresedto.Criticismedto
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.battleedto.acresedto.Napedto.requestStorePermission
import rallyedto.farewelledto.Technologyedto.showDeletePermanentlyDialog
import rallyedto.farewelledto.Technologyedto.showExitFunctionDialog
import rallyedto.farewelledto.Technologyedto.showGerPermission
import rallyedto.policeedto.huntedto.Squeezeedto
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Lampedto
import rallyedto.talledto.blewedto.Localedto
import rallyedto.talledto.blewedto.Sketchedto


class Breatheedto : Lemonedto<AcceptableedtoBinding>() {
    private var isSelectAll = false

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

    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
       // Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        if (checkStorePermission()) {
            initData()
        } else {
            showGerPermission("Storage Permission Required",
                "This feature requires access to your storage to scan your files and clean up repeat photos. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
                deny = { finishToMain() },
                allow = {
                    requestStorePermission(launcher, result = {
                        if (it) {
                            initData()
                        } else {
                            finishToMain()
                        }
                    })
                })
        }
        // binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        binding.basketedto.isEnabled = false

    }

    private fun initData() {
        binding.navyedto.isVisible = true
        fileList = Lampedto.similarImageList
        binding.biscuitedto.isVisible = fileList.size == 0
        binding.basketedto.isVisible = fileList.size > 0
        binding.dieedto.isVisible = fileList.size > 0
        binding.childrenedto.isVisible = fileList.size > 0
        initAdapter()
        if (fileList.size > 6) {
            Togetheredto.showNativeAd(this, binding.disappointedto)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initListener() {
        binding.candidateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            showExitFunctionDialog {
                admobMaxShowInterstitialAd(isShow = Togetheredto.isShowAdBackInter()) {
                    finishToMain()
                }
            }
        }

        binding.childrenedto.setOnClickListener {
            if (isSelectAll) {
                isSelectAll = false
                binding.dieedto.text = "All"
                binding.childrenedto.setImageResource(R.mipmap.walkedto)
            } else {
                isSelectAll = true
                binding.dieedto.text = "Cancel"
                binding.childrenedto.setImageResource(R.mipmap.junioredto)
            }
            fileList.forEach { it.isSelect = isSelectAll }
            // mAdapter.notifyItemRangeChanged(0, fileList.size, 1)
            binding.acuteedto.adapter?.notifyItemRangeChanged(0, fileList.size, 1)
            setTvStatus()
        }
        binding.basketedto.setOnClickListener {
            showDeletePermanentlyDialog {
                admobMaxShowInterstitialAd(true) {
                    cleanFile()
                }
            }
        }
    }

    private fun cleanFile() {
        val list = fileList.toList().filter { it.isSelect }
        if (list.isNotEmpty() && list.all { Sketchedto.delete(it.path) }) {
            fileList.removeAll(list)
            Lampedto.similarImageList.removeAll(list)
            //    mAdapter.notifyDataSetChanged()
            binding.acuteedto.adapter?.notifyDataSetChanged()
            setTvStatus()
            Toast.makeText(this, "Delete Successful", Toast.LENGTH_SHORT).show()
            val array = list.map { it.path }.toTypedArray()
            Localedto.updateMediaStore(this, array)
        } else {
            Toast.makeText(this, "Delete failed.", Toast.LENGTH_SHORT).show()
        }
    }

    private var fileList = mutableListOf<Squeezeedto>()


    private fun initAdapter() {
        binding.acuteedto.run {
            layoutManager = GridLayoutManager(this@Breatheedto, 3)
            adapter = object : RecyclerView.Adapter<mViewHolder>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): mViewHolder {
                    val view = LayoutInflater.from(context)
                        .inflate(R.layout.cherryedto, parent, false)
                    return mViewHolder(view)
                }

                @SuppressLint("SetTextI18n")
                override fun onBindViewHolder(holder: mViewHolder, position: Int) {
                    val item = fileList[position]
                    Glide.with(Criticismedto.context).load(item.path).into(holder.ivImage)
                    holder.ivSelect.isVisible = true
                    holder.itemView.setOnClickListener {

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

                        if (fileList.all { it.isSelect }) {
                            isSelectAll = true
                            binding.dieedto.text = "Cancel"
                            binding.childrenedto.setImageResource(R.mipmap.junioredto)
                        } else {
                            isSelectAll = false
                            binding.dieedto.text = "All"
                            binding.childrenedto.setImageResource(R.mipmap.walkedto)
                        }
                        setTvStatus()
                    }
                }

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

                override fun getItemCount() = fileList.size
            }
        }

    }

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

    class mViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivImage: ImageView
        val ivSelect: ImageView

        init {
            ivImage = view.findViewById(R.id.loweredto)
            ivSelect = view.findViewById(R.id.ironyedto)
        }
    }
}