package rowedto.jockeyedto.sheetedto.atticedto.rededto.knifeedto.economyedto

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.GraveledtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.atticedto.directionedto.noxiousedto.batedto.Negativeedto
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showDeletePermanentlyDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showExitFunctionDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showLargeFileTypeDialog
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_ALL
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_AUDIO
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_DOCUMENT
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_OTHER
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_PICTURE
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Dynamicedto.SELECT_FILE_VIDEO
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Affairedto.getAllMedia
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
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.Bulletedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto

class Restrictedto : Crossingedto<GraveledtoBinding>() {

    private lateinit var largeFileAdapter: Bulletedto
    private var largeData = listOf<Cunningedto>()

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

    var selectType = SELECT_FILE_ALL


    @SuppressLint("SetTextI18n")
    override fun initView() {
        Scandaledto.admobMaxShowNativeAd(this,binding.tooledto,1)
        largeFileAdapter = Bulletedto(itemClick = {}, selectClick = {
            if (it.isNotEmpty()) {
                binding.kineticedto.isEnabled = true
                binding.kineticedto.text = "Delete(${it.sumOf { bean -> bean.size }.toFormatSize()})"
            } else {
                binding.kineticedto.isEnabled = false
            }
        })
        binding.funeraledto.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) {
                    Scandaledto.admobMaxShowInterstitialAd(this@Restrictedto, false) {
                        finishToMain()
                    }
                } else {
                    finishToMain()
                }
            }
        }
        binding.concentrateedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.kineticedto.setOnClickListener {
            showDeletePermanentlyDialog {
                deleteFiles()
            }
        }
        binding.doctoredto.setOnClickListener {
            showLargeFileTypeDialog(selectType) { type ->
                selectType = type
                binding.dramaedto.text = selectType
                largeFileAdapter.setData(largeData.filter { selectFilter(it) })
            }
        }
    }

    private fun selectFilter(mediaBean: Cunningedto): 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() {
        val list = largeFileAdapter.getSelectData()
//        runCatching {
//            list.forEach { bean ->
//                File(bean.path).delete()
//            }
//        }
//        launch(Dispatchers.Main) {
//            largeFileAdapter.removeData(list)
//            delay(1000)
//            startActivity(Intent(this@Restrictedto, Softedto::class.java).apply {
//                putExtra("from", Dynamicedto.LARGE_FILE)
//            })
//            finish()
//        }
        startActivity(Intent(this, Negativeedto::class.java).apply {
                putExtra("list",list.map { it.path }.toTypedArray())
                putExtra("type", Dynamicedto.LARGE_FILE)
            }
        )
        finish()
    }

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

}