package rallyedto.finishedto.conventionedto

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.DeathedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Circulationedto.getDirFiles
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.battleedto.acresedto.Napedto.requestStorePermission
import rallyedto.farewelledto.Technologyedto.showGerPermission
import rallyedto.finishedto.cableedto.Freckleedto
import rallyedto.finishedto.pollutionedto.Carefullyedto
import rallyedto.finishedto.pollutionedto.Opaledto
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Leafletedto.isImage
import java.io.File

class Biologyedto : Lemonedto<DeathedtoBinding>() {

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

    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        Togetheredto.showNativeAd(this,binding.jeweledto)
        val tittle = intent.extras?.getString("tittle")
        tittle?.let { binding.loveredto.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.gooseedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.haircutedto.setOnClickListener {
            startActivity(Intent(this@Biologyedto, Freckleedto::class.java))
        }
        binding.willedto.setOnClickListener {
            startActivity(Intent(this@Biologyedto, Opaledto::class.java))
        }
    }

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

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

        launch(Dispatchers.Main) {
            binding.untiledto.text = dcimSize
            binding.treatmentedto.text = photoSize
        }

    }


}