package importantedto.appliededto.surgeonedto.promiseedto.enteredto.eatenedto.ridingedto

import android.annotation.SuppressLint
import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.EarlieredtoBinding
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.afteredto.constitutionaledto.Memberedto.getAllMedia
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
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_ALL
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_AUDIO
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_DOCUMENT
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_OTHER
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_PICTURE
import importantedto.concreteedto.cameedto.Hateedto.SELECT_FILE_VIDEO
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.deserveedto.manufactureredto.viableedto.Mazeedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showDeletePermanentlyDialog
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showExitFunctionDialog
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showGerPermission
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showLargeFileTypeDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File

class Premiumedto : Murderedto<EarlieredtoBinding>() {

    private lateinit var largeFileAdapter: Mazeedto
    private var largeData = listOf<Vigorousedto>()

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

    var selectType = SELECT_FILE_ALL


    @SuppressLint("SetTextI18n")
    override fun initView() {
        largeFileAdapter = Mazeedto(itemClick = {}, selectClick = {
            if (it.isNotEmpty()) {
                binding.tooledto.isEnabled = true
                binding.tooledto.text = "Delete(${it.sumOf { bean -> bean.size }.toFormatSize()})"
            } else {
                binding.tooledto.isEnabled = false
            }
        })
        binding.questedto.adapter = largeFileAdapter

        if (checkStorePermission()) {
            initData()
        } else {
            showGerPermission(
                desc = "This feature requires access to your storage to scan your large files and clean up large files." +
                        "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) initData() else finishToMain()
                    }
                })
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showExitFunctionDialog { show ->
                if (show) {
                    Differenceedto.admobMaxShowInterstitialAd(this@Premiumedto, false) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        }
        binding.personaledto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tooledto.setOnClickListener {
            showDeletePermanentlyDialog {
                deleteFiles()
            }
        }
        binding.gunedto.setOnClickListener {
            showLargeFileTypeDialog(selectType) { type ->
                selectType = type
                binding.erroredto.text = selectType
                largeFileAdapter.setData(largeData.filter { selectFilter(it) })
            }
        }
    }

    private fun selectFilter(mediaBean: Vigorousedto): Boolean {

        when (selectType) {
            SELECT_FILE_ALL -> return true
            SELECT_FILE_PICTURE -> {
                return mediaBean.mimeType.contains("image")
            }

            SELECT_FILE_VIDEO -> {
                return mediaBean.mimeType.contains("video")
            }

            SELECT_FILE_AUDIO -> {
                return mediaBean.mimeType.contains("audio")
            }

            SELECT_FILE_DOCUMENT -> {
                return mediaBean.mimeType.contains("pdf")
                        || mediaBean.mimeType.contains("msword")
                        || mediaBean.mimeType.contains("document")
                        || mediaBean.mimeType.contains("excel")
                        || mediaBean.mimeType.contains("sheet")
                        || mediaBean.mimeType.contains("powerpoint")
                        || mediaBean.mimeType.contains("presentation")
            }

            SELECT_FILE_OTHER -> {
                return mediaBean.mimeType.contains("zip")
                        || mediaBean.mimeType.contains("rar")
                        || mediaBean.mimeType.contains("7z")
                        || mediaBean.mimeType.contains("tar")
                        || mediaBean.mimeType.contains("archive")
                        || mediaBean.mimeType.contains("stream")
            }
        }

        return false
    }

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

        val list = largeFileAdapter.getSelectData()
        runCatching {
            list.forEach { bean ->
                File(bean.path).delete()
            }
        }
        launch(Dispatchers.Main) {
            largeFileAdapter.removeData(list)
            delay(1000)
            startActivity(Intent(this@Premiumedto, Halcyonedto::class.java).apply {
                putExtra("from", Hateedto.LARGE_FILE)
            })
            finish()
        }
    }

    private fun initData() = lifecycleScope.launch(Dispatchers.IO) {
        val beanList = getAllMedia { size -> size > 1024 * 1024 * 10 }
        launch(Dispatchers.Main) {
            largeData = beanList
            binding.bushedto.isVisible = beanList.isEmpty()
            largeFileAdapter.setData(beanList)
        }
    }

}