package waific.Teflonic.therapistic

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import androidx.lifecycle.lifecycleScope
import tooic.fatalityic.jittersic.Peteric
import tooic.fatalityic.jittersic.jumpAction
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 tooic.fatalityic.corroborateic.Protegeic.Companion.VIDEO_CLEAN
import com.easy.clean.databinding.DynamiteicBinding
import rememberic.permissionStorageJump
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Rotundaic : Peteric<DynamiteicBinding>(DynamiteicBinding::inflate) {

    override fun initView() {
        super.initView()
//        Unwindic.showNative(binding.protectic, R.layout.explanatoryic)
        binding.fantasticallyic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
        }
        getData()
    }

    override fun initListener() {
        binding.wrestic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
        }
        binding.spaceic.setOnClickListener {
            (requireActivity() as Hauntingic).jumpAction(BATTERY_INFO)
        }
        binding.meantimeic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(VIDEO_CLEAN)
        }
        binding.interestic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(LARGE_FILE_CLEAN)
        }
        binding.kiddoic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(PHOTO_COMPRESSION)
        }
        binding.anachronismic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(SCREENSHOT_CLEAN)
        }
        binding.rhododendronic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(SIMILAR_PHOTOS)
        }
    }

    fun getData() {
        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) {
                val spanned = SpannableStringBuilder(usedStorage + " / $totalStorage")
                spanned.setSpan(
                    ForegroundColorSpan(Color.parseColor("#F22525")),
                    0,
                    usedStorage.length,
                    Spanned.SPAN_EXCLUSIVE_INCLUSIVE
                )
                binding.brutalic.text = spanned
                binding.fantasticallyic.setProgressWithAnimation(percent.toFloat(), 1000)
            }
        }
    }
}