package rowedto.jockeyedto.sheetedto.atticedto.openedto.postedto

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.R
import com.base.datarecovery.databinding.ThemeedtoBinding
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.atticedto.Softedto
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showExitFunctionDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.WHATSAPP_CLEANER
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Tiptoeedto
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.meadowedto.naturaledto.idealedto.Highedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stressedto.isImage
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stressedto.isWhatAppVideo
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import java.io.File

class Chestedto : Crossingedto<ThemeedtoBinding>() {

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

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


    override fun initView() {
        Spareedto.setStatusBarLightMode(this, true)
        Spareedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.graduallyedto.updatePadding(top = Spareedto.getStatusBarHeight())
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        adapter = Highedto {
            startActivity(Intent(this, Stemedto::class.java).apply {
                putExtra("PathList", Gson().toJson(it.subFile.toTypedArray()))
                putExtra("Tittle", it.tittle)
            })
          //  finish()
        }
        binding.funeraledto.adapter = adapter

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

    override fun initListener() {
        binding.concentrateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            showExitFunctionDialog { showAd ->
                if (showAd) {
                    Scandaledto.admobMaxShowInterstitialAd(this@Chestedto, false) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        }
        binding.enlargeedto.setOnClickListener {
            startActivity(Intent(this, Softedto::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/")
        Dishedto.logDebug(TAG, whatsAppRoot.absolutePath)
        val list = arrayListOf<Tiptoeedto>()
        list.add(Tiptoeedto(R.mipmap.wageedto, "Video Messages"))
        list.add(Tiptoeedto(R.mipmap.disasteredto, "Image Messages"))
        list.add(Tiptoeedto(R.mipmap.mealedto, "Audio Messages"))
        var totalSize = 0L
        whatsAppRoot.listFiles()?.forEach { file ->
            if (file.name == "WhatsApp Video") {
                Dishedto.logDebug(TAG, "whatsapp file=${file.absolutePath}")
                val bean = list.find { it.tittle == "Video Messages" }
                val subFile = getDirFiles(file).filter { isWhatAppVideo(it) }
                val size = subFile.sumOf { it.length() }
                bean?.size = size
                totalSize += size
                bean?.subFile = subFile.map { it.absolutePath }
//                subFile.forEach { path ->
//                    Dishedto.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.bulletinedto.text = split[0]
            binding.figureedto.text = split[1]
        }
    }

}