package importantedto.appliededto.surgeonedto.promiseedto.produceredto.motionedto.momentumedto

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.RoutineedtoBinding
import importantedto.afteredto.constitutionaledto.Concessionedto.getDirFiles
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.afteredto.constitutionaledto.Rackedto.checkStorePermission
import importantedto.afteredto.constitutionaledto.Rackedto.requestStorePermission
import importantedto.appliededto.surgeonedto.promiseedto.nobleedto.defendedto.tastyedto.Trafficedto
import importantedto.appliededto.surgeonedto.promiseedto.rapeedto.Boardedto
import importantedto.gallonedto.Buttocksedto.isImage
import importantedto.gallonedto.Transcendedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showGerPermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class Curveedto : Murderedto<RoutineedtoBinding>() {

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

    override fun initView() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = Transcendedto.getStatusBarHeight())

        val tittle = intent.extras?.getString("tittle")
        tittle?.let { binding.meetingedto.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.personaledto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.sweateredto.setOnClickListener {
            startActivity(Intent(this@Curveedto, Boardedto::class.java))
        }
        binding.comedyedto.setOnClickListener {
            startActivity(Intent(this@Curveedto, Trafficedto::class.java))
        }
    }

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

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

        launch(Dispatchers.Main) {
            binding.libertarianedto.text = dcimSize
            binding.frozenedto.text = photoSize
        }

    }


}