package waific.Teflonic.therapistic

import android.annotation.SuppressLint
import androidx.lifecycle.lifecycleScope
import com.easy.clean.R
import tooic.fatalityic.jittersic.Peteric
import tooic.fatalityic.jittersic.goToAc
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.SCREENSHOT_CLEAN
import tooic.fatalityic.corroborateic.Protegeic.Companion.SIMILAR_PHOTOS
import autonomyic.polemicalic.congestionic.Unwindic
import com.easy.clean.databinding.SorticBinding
import rememberic.permissionStorageJump
import overthrewic.dirtyic.coneic.Aerodynamicsic
import tooic.fatalityic.redic.speckledic.hankeringic.Devastatingic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Mussic : Peteric<SorticBinding>(SorticBinding::inflate) {

    override fun initView() {
        super.initView()
        Unwindic.showNative(binding.protectic, R.layout.explanatoryic)

        initData()
    }

    override fun initListener() {
        super.initListener()
        binding.brokenic.setOnClickListener {
            goToAc(Aerodynamicsic::class.java)
        }
        binding.ratificationic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
        }
        binding.drinkeric.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
        }
        binding.overshotic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(JUNK_CLEAN)
        }
        binding.ravageic.setOnClickListener {
            (requireActivity() as Hauntingic).jumpAction(BATTERY_INFO)
        }
        binding.pulleyic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(LARGE_FILE_CLEAN)
        }
        binding.refurbishmentic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(SIMILAR_PHOTOS)
        }
        binding.clotheslineic.setOnClickListener {
            (requireActivity() as Hauntingic).permissionStorageJump(SCREENSHOT_CLEAN)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        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) {
                binding.vulnerabilityic.text = usedStorage
                binding.alarmic.text = " / $totalStorage"
                binding.fantasticallyic.setProgressWithAnimation(percent.toFloat(), 1000)
            }
        }
    }
}