package rowedto.jockeyedto.sheetedto.atticedto.expandedto.colonyedto.dietedto

import android.content.Intent
import android.graphics.Color
import android.os.Environment
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.CoerceedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.atticedto.jobedto.basinedto.civilianedto.Quarteredto
import rowedto.jockeyedto.sheetedto.atticedto.jobedto.basinedto.civilianedto.Searchedto
import rowedto.jockeyedto.sheetedto.atticedto.teacheredto.characteristicedto.Cattleedto
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Everythingedto.getDirFiles
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.checkStorePermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.requestStorePermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.toFormatSize
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stressedto.isImage
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import java.io.File

class Beautifuledto : Crossingedto<CoerceedtoBinding>() {

    override val binding: CoerceedtoBinding by lazy {
        CoerceedtoBinding.inflate(layoutInflater)
    }

    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = Spareedto.getStatusBarHeight())
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        val tittle = intent.extras?.getString("tittle")
        tittle?.let { binding.spontaneousedto.text = it }

        if (checkStorePermission()) {
            initDataSize()
        } else {
            showGerPermission(desc = "This feature requires access to your storage to scan your files and clean up screenshots. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
                deny = { finishToMain() }, allow = {
                    requestStorePermission(launcher) {
                        if (it) {
                            initDataSize()
                        } else {
                            finishToMain()
                        }
                    }
                })
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.concentrateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.developmentedto.setOnClickListener {
            startActivity(Intent(this@Beautifuledto, Cattleedto::class.java))
        }
        binding.calorieedto.setOnClickListener {
            startActivity(Intent(this@Beautifuledto, Searchedto::class.java))
        }
    }

    private fun initDataSize() = lifecycleScope.launch(Dispatchers.IO) {
        val dcimSize =
            getDirFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)).filter { isImage(it) }
                .sumOf { it.length() }.toFormatSize()

        Quarteredto.beanList.addAll(Quarteredto.getSimilarList(this@Beautifuledto))
        val photoSize = Quarteredto.beanList.sumOf { it.beans.sumOf { s -> File(s.path).length() } }.toFormatSize()

        launch(Dispatchers.Main) {
            binding.funedto.text = dcimSize
            binding.indulgeedto.text = photoSize
        }

    }


}