package importantedto.appliededto.surgeonedto.promiseedto.wonderedto

import android.content.Intent
import android.graphics.Color
import android.os.Environment
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.R
import com.base.datarecovery.databinding.SecretaryedtoBinding
import com.google.gson.Gson
import importantedto.admireedto.sweptedto.Differenceedto
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.Halcyonedto
import importantedto.concreteedto.cameedto.Hateedto.WHATSAPP_CLEANER
import importantedto.concreteedto.cameedto.Highlightedto
import importantedto.deserveedto.manufactureredto.viableedto.Volumeedto
import importantedto.gallonedto.Buttocksedto.isImage
import importantedto.gallonedto.Buttocksedto.isVideo
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showExitFunctionDialog
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showGerPermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class Contraryedto : Murderedto<SecretaryedtoBinding>() {

    private val TAG = "WhatsAppCleanerActivity"
    private lateinit var adapter: Volumeedto

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


    override fun initView() {
        Transcendedto.setStatusBarLightMode(this, true)
        Transcendedto.setStatusBarColor(this, Color.TRANSPARENT)

        adapter = Volumeedto {
            startActivity(Intent(this, Jovialedto::class.java).apply {
                putExtra("PathList", Gson().toJson(it.subFile.toTypedArray()))
                putExtra("Tittle", it.tittle)
            })
            finish()
        }
        binding.questedto.adapter = adapter

        if (checkStorePermission()) {
            initData()
        } else {
            showGerPermission(null, deny = {
                finishToMain()
            }, allow = {
                requestStorePermission(
                    launcher,
                    result = { flag ->
                        if (flag) {
                            initData()
                        } else {
                            finishToMain()
                        }
                    },
                )
            })
        }
    }

    override fun initListener() {
        binding.personaledto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            showExitFunctionDialog { showAd ->
                if (showAd) {
                    Differenceedto.admobMaxShowInterstitialAd(this@Contraryedto, false) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        }
        binding.northedto.setOnClickListener {
            startActivity(Intent(this, Halcyonedto::class.java).apply {
                putExtra("from", WHATSAPP_CLEANER)
            })
            finish()
        }

    }


    private fun initData() = lifecycleScope.launch(Dispatchers.IO) {

        val whatsAppRoot = File(Environment.getExternalStorageDirectory(), "Android/media/com.whatsapp/WhatsApp/Media/")
        Tumbleedto.logDebug(TAG, whatsAppRoot.absolutePath)
        val list = arrayListOf<Highlightedto>()
        list.add(Highlightedto(R.mipmap.glassedto, "Video Messages"))
        list.add(Highlightedto(R.mipmap.profitedto, "Image Messages"))
        list.add(Highlightedto(R.mipmap.forwardedto, "Audio Messages"))
        var totalSize = 0L
        whatsAppRoot.listFiles()?.forEach { file ->

            if (file.name == "WhatsApp Video") {
                Tumbleedto.logDebug(TAG, "whatsapp file=${file.absolutePath}")
                val bean = list.find { it.tittle == "Video Messages" }
                val subFile = getDirFiles(file).filter { isVideo(it) }
                val size = subFile.sumOf { it.length() }
                bean?.size = size
                totalSize += size
                bean?.subFile = subFile.map { it.absolutePath }
//                subFile.forEach { path ->
//                    Tumbleedto.logDebug(TAG, "path=$path")
//                }
            }
            if (file.name == "WhatsApp Images") {
                val bean = list.find { it.tittle == "Image Messages" }
                val subFile = getDirFiles(file).filter { isImage(it) }
                val size = subFile.sumOf { it.length() }
                bean?.size = size
                totalSize += size
                bean?.subFile = subFile.map { it.absolutePath }
            }
            if (file.name == "WhatsApp Audio") {
                val bean = list.find { it.tittle == "Audio Messages" }
                val subFile = getDirFiles(file).filter { it.name.contains(".mp3") }
                val size = subFile.sumOf { it.length() }
                bean?.size = size
                totalSize += size
                bean?.subFile = subFile.map { it.absolutePath }
            }
        }
        launch(Dispatchers.Main) {
            adapter.setData(list)
            val split = totalSize.toFormatSize().split(" ")
            binding.ignoranceedto.text = split[0]
            binding.cheatedto.text = split[1]
        }
    }

}