package hairnetic

import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.easy.clean.R
import tooic.fatalityic.jittersic.Reinstateic
import tooic.fatalityic.jittersic.LottieEnum
import tooic.fatalityic.corroborateic.Voluminousic
import autonomyic.polemicalic.congestionic.Unwindic
import com.easy.clean.databinding.GreensicBinding
import tooic.fatalityic.redic.speckledic.hankeringic.Lilacic
import tooic.fatalityic.redic.speckledic.hankeringic.Townhouseic.TYPE_IMAGE
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class Slipperyic : Reinstateic<GreensicBinding>(GreensicBinding::inflate) {

    private lateinit var adapter: Includingic
    private var total: Long = 0
    private var isSelectAll = false

    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()
        adapter = Includingic(TYPE_IMAGE) {
            if (it.isSelected) total += it.length
            else total -= it.length
            isSelectAll = adapter.list.stream().allMatch(Voluminousic::isSelected)
            updateView(total > 0)
        }
        binding.squaloric.jobic.text=getString(R.string.boundaryic)
        binding.junctionic.adapter = adapter
        binding.junctionic.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
        initData()

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

    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val images = async { Lilacic.findImageFiles(this@Slipperyic) }.await()
            withContext(Dispatchers.Main) {
                adapter.submitList(images)
                binding.herselfic.visibility = if (images.isEmpty()) View.VISIBLE else View.GONE
                binding.counteric.visibility = if (images.isEmpty()) View.GONE else View.VISIBLE
            }
        }
    }

    private fun updateView(isSelection: Boolean) {
        binding.vestibuleic.isSelected = isSelectAll
        binding.burntic.isEnabled = isSelection
        if (total > 0) {
            binding.burntic.setTextColor(getColor(R.color.roadsideic))
            binding.burntic.text =
                "${getString(R.string.raspic)} (${Devastatingic.getSizeFormat(total)})"
            binding.burntic.background = getDrawable(R.drawable.scrutinyic)
        } else {
            binding.burntic.setTextColor(getColor(R.color.manuallyic))
            binding.burntic.text = getString(R.string.raspic)
            binding.burntic.background = getDrawable(R.drawable.billowic)
        }
    }

    override fun initListener() {
        super.initListener()

        binding.squaloric.telecommutingic.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
        binding.thereafteric.setOnClickListener {
            isSelectAll = !isSelectAll
            val list = adapter.list
            list.forEach { it.isSelected = isSelectAll }
            adapter.submitList(list)
            total = list.filter { it.isSelected }.sumOf { it.length }
            updateView(isSelectAll)
        }
        binding.burntic.setOnClickListener {
            val list = adapter.list.filter { it.isSelected }.toMutableList()
            if (list.isEmpty()) return@setOnClickListener
            Camerawomanic.list = list
            goToAc(Camerawomanic::class.java)
        }
    }

}