package intellectuallyic.jitteryic.mosaicic

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.easy.clean.R
import tooic.fatalityic.Runawayic
import tooic.fatalityic.jittersic.Reinstateic
import tooic.fatalityic.jittersic.jumpAction
import tooic.fatalityic.corroborateic.Protegeic
import tooic.fatalityic.corroborateic.Protegeic.Companion.BATTERY_INFO
import tooic.fatalityic.corroborateic.Protegeic.Companion.JUNK_CLEAN
import tooic.fatalityic.corroborateic.Protegeic.Companion.LARGE_FILE_CLEAN
import tooic.fatalityic.corroborateic.Protegeic.Companion.PHOTO_COMPRESSION
import tooic.fatalityic.corroborateic.Protegeic.Companion.SCREENSHOT_CLEAN
import tooic.fatalityic.corroborateic.Protegeic.Companion.SIMILAR_PHOTOS
import autonomyic.polemicalic.congestionic.Unwindic
import autonomyic.polemicalic.congestionic.NASAic
import com.easy.clean.databinding.DispensableicBinding
import com.easy.clean.databinding.TimesicBinding
import tooic.fatalityic.redic.speckledic.hankeringic.Carpentryic
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic

class Tailspinic :
    Reinstateic<DispensableicBinding>(DispensableicBinding::inflate) {

    companion object {
        var total: Long = 0
        var functionKey = ""
        var titleName = ""
    }

    private var list = mutableListOf<Protegeic>()
    private val adapter by lazy {
        class ViewHolder(val binding: TimesicBinding) :
            RecyclerView.ViewHolder(binding.root)

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val binding = TimesicBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return ViewHolder(binding)
            }

            override fun getItemCount(): Int = list.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = list[position]
                holder.binding.weaponryic.setImageResource(item.icon)
                holder.binding.consequentic.text = getString(item.titleRes)
                holder.binding.spadesic.setText(item.content)
                holder.binding.professionalismic.setOnClickListener {
                    jumpAction(item.key)
                }

                val translationX = resources.displayMetrics.widthPixels.toFloat()
                holder.itemView.translationX = -translationX
                Carpentryic.translationX(
                    holder.itemView,
                    -translationX,
                    0f,
                    1000,
                    (position + 1) * 300L
                )

                if (position == 0) {
                    Unwindic.showNative(holder.binding.protectic, R.layout.explanatoryic)
                }
            }
        }
    }

    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
        }
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun initView() {
        super.initView()
        binding.epicenteric.adapter = adapter
        binding.epicenteric.layoutManager = LinearLayoutManager(this)

        if (titleName.isEmpty()) titleName = getString(R.string.sleeperic)
        binding.squaloric.jobic.text = titleName
        binding.somebodyic.text = "${getString(R.string.inoffensiveic)} ${Devastatingic.getSizeFormat(total)}"
        Runawayic.cleanedSize += total

        list = getData()
        adapter.notifyDataSetChanged()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            Unwindic.showInsert(this@Tailspinic, showCallBack = object : NASAic() {
                override fun next() {
                    finishToMainTop()
                }
            })
        }
        binding.squaloric.telecommutingic.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

    }

    private fun getData(): MutableList<Protegeic> {
        val list = mutableListOf<Protegeic>().apply {
            add(
                Protegeic(
                    JUNK_CLEAN,
                    R.string.woolenic,
                    R.drawable.libic,
                    R.string.convoyic
                )
            )
            add(
                Protegeic(
                    BATTERY_INFO,
                    R.string.innateic,
                    R.drawable.fireplaceic,
                    R.string.anatomyic
                )
            )
            add(
                Protegeic(
                    LARGE_FILE_CLEAN,
                    R.string.sanitaryic,
                    R.drawable.catapultic,
                    R.string.maternalic
                )
            )
            add(
                Protegeic(
                    PHOTO_COMPRESSION,
                    R.string.forceic,
                    R.drawable.penalizeic,
                    R.string.challengeic
                )
            )
            add(
                Protegeic(
                    SCREENSHOT_CLEAN,
                    R.string.tautic,
                    R.drawable.pneumoniaic,
                    R.string.touchdownic
                )
            )
            add(
                Protegeic(
                    SIMILAR_PHOTOS,
                    R.string.conjectureic,
                    R.drawable.dignitaryic,
                    R.string.impreciseic
                )
            )
        }

        val filteredList = list.filter { it.key != functionKey }
        val randomIndices = filteredList.indices.shuffled().take(4).toList()
        val data = randomIndices.map { filteredList[it] }
        return data.toMutableList()
    }
}