package rowedto.jockeyedto.sheetedto.atticedto.jobedto.basinedto.civilianedto

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
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.base.datarecovery.R
import com.base.datarecovery.databinding.PenedtoBinding
import com.bumptech.glide.Glide
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showDeletePermanentlyDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showExitFunctionDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Claimedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Driededto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.checkStorePermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.requestStorePermission
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Ecstasyedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Flameedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Responsibilityedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto


class Tossedto : Crossingedto<PenedtoBinding>() {
    private var isSelectAll = false

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

    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.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 = Spareedto.getStatusBarHeight())
        binding.mathematicsedto.isEnabled = false

    }
    private fun initData(){
        binding.cultivateedto.isVisible = true
        fileList = Ecstasyedto.similarImageList
        binding.onionedto.isVisible = fileList.size == 0
        binding.mathematicsedto.isVisible = fileList.size > 0
        binding.firstedto.isVisible = fileList.size > 0
        binding.clusteredto.isVisible = fileList.size > 0
        initAdapter()
        if (fileList.size > 6) {
            Scandaledto.admobMaxShowNativeAd(this, binding.breatheedto, 1)
        }
    }
    @SuppressLint("NotifyDataSetChanged")
    override fun initListener() {
        binding.groundedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                showExitFunctionDialog {
                    if (it) {
                        Scandaledto.admobMaxShowInterstitialAd(this@Tossedto, false) {
                            finishToMain()
                        }
                    } else {
                        finishToMain()
                    }

                }
            }
        })

        binding.clusteredto.setOnClickListener {
            if (isSelectAll) {
                isSelectAll = false
                binding.firstedto.text = "All"
                binding.clusteredto.setImageResource(R.mipmap.excessedto)
            } else {
                isSelectAll = true
                binding.firstedto.text = "Cancel"
                binding.clusteredto.setImageResource(R.mipmap.nearestedto)
            }
            fileList.forEach { it.isSelect = isSelectAll }
            // mAdapter.notifyItemRangeChanged(0, fileList.size, 1)
            binding.soupedto.adapter?.notifyItemRangeChanged(0, fileList.size, 1)
            setTvStatus()
        }
        binding.mathematicsedto.setOnClickListener {
            showDeletePermanentlyDialog {
                Scandaledto.admobMaxShowInterstitialAd(this, false) {
                    cleanFile()
                }
            }
        }
    }

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

    private var fileList = mutableListOf<Claimedto>()




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

                @SuppressLint("SetTextI18n")
                override fun onBindViewHolder(holder: mViewHolder, position: Int) {
                    val item = fileList[position]
                    Glide.with(Driededto.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.firstedto.text = "Cancel"
                            binding.clusteredto.setImageResource(R.mipmap.nearestedto)
                        } else {
                            isSelectAll = false
                            binding.firstedto.text = "All"
                            binding.clusteredto.setImageResource(R.mipmap.excessedto)
                        }
                        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.nearestedto
                        } else {
                            R.mipmap.excessedto
                        }
                    )
                }

                override fun getItemCount() = fileList.size
            }
        }

    }

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

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

        init {
            ivImage = view.findViewById(R.id.plaintiffedto)
            ivSelect = view.findViewById(R.id.crownedto)
        }
    }
}