package shrunkic.typecastic

import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.easy.clean.R
import tooic.fatalityic.jittersic.Reinstateic
import tooic.fatalityic.jittersic.LottieEnum
import tooic.fatalityic.jittersic.cleanFileBeans
import tooic.fatalityic.corroborateic.Protegeic.Companion.SCREENSHOT_CLEAN
import tooic.fatalityic.corroborateic.Voluminousic
import autonomyic.polemicalic.congestionic.Unwindic
import autonomyic.polemicalic.congestionic.NASAic
import com.easy.clean.databinding.MannequinicBinding
import com.easy.clean.databinding.PredispositionicBinding
import intellectuallyic.jitteryic.mosaicic.Tailspinic
import rememberic.Stringsic
import tooic.fatalityic.redic.speckledic.hankeringic.Lilacic
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class Chaseic :
    Reinstateic<MannequinicBinding>(MannequinicBinding::inflate) {


    private var list = mutableListOf<Voluminousic>()
    private var isSelectAll = false
    private var total: Long = 0
    private var selectedFiles = listOf<Voluminousic>()

    private val adapter by lazy {
        class VideoHolder(val item: PredispositionicBinding) : RecyclerView.ViewHolder(item.root)
        object : RecyclerView.Adapter<VideoHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int,
            ): VideoHolder {
                val binding = PredispositionicBinding.inflate(layoutInflater, parent, false)
                return VideoHolder(binding)
            }

            override fun onBindViewHolder(holder: VideoHolder, position: Int) {
                val bean = list[position]
                val round = resources.getDimensionPixelSize(R.dimen.deepenic)
                val options = RequestOptions().transform(CenterCrop(), RoundedCorners(round))
                Glide.with(holder.itemView.context)
                    .asBitmap()
                    .load(File(bean.path))
                    .apply(options)
                    .into(holder.item.bulldozeric)
                holder.item.prospectic.text = Devastatingic.getSizeFormat(bean.length)
                holder.item.salveic.isSelected = bean.isSelected
                holder.item.root.setOnClickListener {
                    val f = !bean.isSelected
                    holder.item.salveic.isSelected = f
                    bean.isSelected = f
                    updateSelection()
                }
            }

            override fun getItemCount() = list.size
        }
    }

    private fun updateSelection() {
        selectedFiles = list.filter {
            it.isSelected
        }
        isSelectAll = selectedFiles.size == list.size
        total = selectedFiles.sumOf { it.length }
        updateView(selectedFiles.isNotEmpty())
    }


    private fun updateView(isSelection: Boolean) {

        binding.vestibuleic.isSelected = isSelectAll
        binding.gownic.isEnabled = isSelection
        if (total > 0) {
            binding.gownic.setTextColor(getColor(R.color.roadsideic))
            binding.gownic.text =
                "${getString(R.string.involvedic)} (${Devastatingic.getSizeFormat(total)})"
            binding.gownic.background = getDrawable(R.drawable.scrutinyic)
        } else {
            binding.gownic.setTextColor(getColor(R.color.manuallyic))
            binding.gownic.text = getString(R.string.involvedic)
            binding.gownic.background = getDrawable(R.drawable.billowic)
        }
    }

    override fun useDefaultImmersive() {
        setStatusBarColor()
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.overseeric.setPadding(0, systemBars.top, 0, 0)
            insets
        }
    }

    override fun initView() {
        super.initView()
        binding.umpireic.adapter = adapter
        val gridLayoutManager = GridLayoutManager(this, 3)
        binding.umpireic.layoutManager = gridLayoutManager
        initData()

        showAdAnimation(LottieEnum.VIDEO_CLEAN_LOTTIE) {
            Unwindic.showNative(binding.protectic, R.layout.explanatoryic)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            list = Lilacic.findVideoFiles(this@Chaseic)
            updateSelection()
            adapter.notifyDataSetChanged()
            var size = 0L
            list.forEach { size += it.length }
            val value = Devastatingic.getSizeArray(size)
            binding.prospectic.text = value[0]
            binding.wharvesic.text = value[1]
            binding.herselfic.visibility = if (list.isEmpty()) View.VISIBLE else View.GONE
            binding.counteric.visibility = if (list.isEmpty()) View.GONE else View.VISIBLE
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initListener() {
        super.initListener()
        binding.squaloric.telecommutingic.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
        binding.thereafteric.setOnClickListener {
            isSelectAll = !isSelectAll
            list.forEach { it.isSelected = isSelectAll }
            adapter.notifyDataSetChanged()
            updateSelection()
        }
        binding.gownic.setOnClickListener {
            if (total <= 0) return@setOnClickListener
            val dialog = Stringsic(this)
            dialog.action = {
                Unwindic.showInsert(this, showCallBack = object : NASAic() {
                    override fun next() {
                        cleanFileBeans(selectedFiles)
                        Tailspinic.total = total
                        Tailspinic.functionKey = SCREENSHOT_CLEAN
                        Tailspinic.titleName = getString(R.string.tautic)
                        goToAc(Tailspinic::class.java)
                    }

                })
            }
            dialog.showDialog("delete")
        }
    }
}