package waific.Teflonic.therapistic

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import com.easy.clean.R
import tooic.fatalityic.jittersic.Peteric
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 com.easy.clean.databinding.ComradeshipicBinding
import rememberic.permissionStorageJump
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch


class Irrationalic : Peteric<ComradeshipicBinding>(ComradeshipicBinding::inflate) {

    private val concatAdapter = ConcatAdapter()
    private val featureList = listOf(
        Protegeic(JUNK_CLEAN, R.string.sheathic, R.drawable.keynoteic).apply { span = 1 },
        Protegeic(BATTERY_INFO, R.string.innateic, R.drawable.orbitic).apply { span = 1 },
        Protegeic(LARGE_FILE_CLEAN, R.string.sanitaryic, R.drawable.franknessic).apply { span = 1 },
        Protegeic(SCREENSHOT_CLEAN, R.string.tautic, R.drawable.scienceic).apply { span = 3 },
        Protegeic(PHOTO_COMPRESSION, R.string.boundaryic, R.drawable.polaric).apply { span = 3 },
        Protegeic(SIMILAR_PHOTOS, R.string.conjectureic, R.drawable.miniatureic).apply { span = 3 },
    )

    override fun initView() {
        super.initView()
        binding.callingic.adapter = concatAdapter
        concatAdapter.addAdapter(Quotaic().apply {
            submitList(listOf(arrayOf("", "")))
            cleanClick = {
                (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
            }
            initData(this)
        })

        concatAdapter.addAdapter(Offenseic().apply {
            submitList(listOf(featureList))
            itemClick = { bean ->
                if (bean.key == BATTERY_INFO) {
                    requireActivity().jumpAction(bean.key)
                } else {
                    requireActivity().permissionStorageJump(bean.key)
                }
            }
        })

    }

    override fun initListener() {
        super.initListener()
    }

    private fun initData(adapter: Quotaic) {
        lifecycleScope.launch(Dispatchers.IO) {
            val totalStorageSize = async { Devastatingic.getTotalStorageSize() }.await()
            val availableStorageSize = async { Devastatingic.getAvailableStorageSize() }.await()
            val usedStorageSize = totalStorageSize - availableStorageSize
            val totalStorage = Devastatingic.getSizeFormat(totalStorageSize)
            val usedStorage = Devastatingic.getSizeFormat(usedStorageSize)
            val percent = (usedStorageSize * 100 / totalStorageSize).toInt()
            launch(Dispatchers.Main) {
                adapter.submitList(
                    listOf(arrayOf(usedStorage, totalStorage, "$percent"))
                )
            }
        }
    }

}