package rallyedto.finishedto.illnessedto

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.MatteredtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Affinityedto.getAllMedia
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.battleedto.acresedto.Napedto.requestStorePermission
import rallyedto.dumpedto.editionedto.Versionedto
import rallyedto.farewelledto.Technologyedto.showDeletePermanentlyDialog
import rallyedto.farewelledto.Technologyedto.showExitFunctionDialog
import rallyedto.farewelledto.Technologyedto.showGerPermission
import rallyedto.farewelledto.Technologyedto.showLargeFileTypeDialog
import rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto.Drugedto
import rallyedto.policeedto.huntedto.Outletedto
import rallyedto.policeedto.huntedto.Snuggleedto
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_ALL
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_AUDIO
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_DOCUMENT
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_OTHER
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_PICTURE
import rallyedto.policeedto.huntedto.Snuggleedto.SELECT_FILE_VIDEO
import rallyedto.sunedto.tumouredto.majorityedto.loseedto.Togetheredto

class Bounceedto : Lemonedto<MatteredtoBinding>() {

    private lateinit var largeFileAdapter: Versionedto
    private var largeData = listOf<Outletedto>()

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

    var selectType = SELECT_FILE_ALL


    @SuppressLint("SetTextI18n")
    override fun initView() {
        Togetheredto.showNativeAd(this,binding.jeweledto)
        largeFileAdapter = Versionedto(itemClick = {}, selectClick = {
            if (it.isNotEmpty()) {
                binding.censorshipedto.isEnabled = true
                binding.censorshipedto.text = "Delete(${it.sumOf { bean -> bean.size }.toFormatSize()})"
            } else {
                binding.censorshipedto.isEnabled = false
            }
        })
        binding.autonomyedto.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 {
                admobMaxShowInterstitialAd(isShow = Togetheredto.isShowAdBackInter()) {
                    finishToMain()
                }
            }
        }
        binding.gooseedto.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.censorshipedto.setOnClickListener {
            showDeletePermanentlyDialog {
                deleteFiles()
            }
        }
        binding.themselvesedto.setOnClickListener {
            showLargeFileTypeDialog(selectType) { type ->
                selectType = type
                binding.noedto.text = selectType
                largeFileAdapter.setData(largeData.filter { selectFilter(it) })
            }
        }
    }

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

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

}