package rallyedto.finishedto.exampleedto

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Environment
import android.view.View
import android.widget.Toast
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.FilledtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Affinityedto.updateMediaStore
import rallyedto.battleedto.acresedto.Circulationedto.loadFileByFilter
import rallyedto.battleedto.acresedto.Lemonedto
import rallyedto.dumpedto.editionedto.Bootedto
import rallyedto.farewelledto.Technologyedto.showDeletePermanentlyDialog
import rallyedto.policeedto.huntedto.Outletedto
import rallyedto.talledto.blewedto.Islandedto
import rallyedto.talledto.blewedto.Musicaledto
import java.io.File

class Personedto : Lemonedto<FilledtoBinding>() {

    private val TAG = "PrivacyManageActivity"
    private var mediaType: String = ""
    private lateinit var mediaSubAdapter: Bootedto

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


    override fun initView() {
        Islandedto.setStatusBarLightMode(this, true)
        Islandedto.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = Islandedto.getStatusBarHeight())
        mediaType = intent?.extras?.getString("mediaType") ?: ""


        mediaSubAdapter = Bootedto()
        binding.autonomyedto.adapter = mediaSubAdapter
        initData()
    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {

        binding.moreedto.setOnClickListener {
            downloadCopy()
        }
        binding.weaveedto.setOnClickListener {
            deleteFilesDirMedia()
        }
        binding.brownedto.setOnClickListener {
            binding.brownedto.isSelected = !binding.brownedto.isSelected
            mediaSubAdapter.toggleSelect(binding.brownedto.isSelected)
            if (binding.brownedto.isSelected) {
                binding.brownedto.text = "Unselect All"
            } else {
                binding.brownedto.text = "Select All"
            }
        }
        binding.tycoonedto.setOnClickListener {
            finish()
        }
    }

    private fun deleteFilesDirMedia() {
        showDeletePermanentlyDialog {
            admobMaxShowInterstitialAd(true){
                lifecycleScope.launch(Dispatchers.IO) {
                    val list = mediaSubAdapter.getSelectData()
                    list.forEach {
                        runCatching { File(it.path).delete() }
                    }
                    launch(Dispatchers.Main) {
                        Toast.makeText(this@Personedto, "success", Toast.LENGTH_SHORT).show()
                        mediaSubAdapter.removeData(list)
                    }
                }
            }
        }
    }

    private fun downloadCopy() = lifecycleScope.launch(Dispatchers.IO) {
        mediaSubAdapter.getSelectData().forEach { bean ->
            runCatching {
                val file = File(bean.path)
                val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                file.copyTo(File(dir, file.name))
            }
        }
        this@Personedto.updateMediaStore()
        launch(Dispatchers.Main) {
            Toast.makeText(this@Personedto, "success", Toast.LENGTH_SHORT).show()
            mediaSubAdapter.toggleSelect(false)
        }
    }

    private fun initData() {
        val dir = getFilesMediaDir()
        if (!dir.exists()) {
            dir.mkdirs()
        }
        val list = arrayListOf<Outletedto>()

        lifecycleScope.loadFileByFilter(folder = dir, filter = { true }, onDo = {
            Musicaledto.logDebug(TAG, "$it")
            list.add(Outletedto(it.absolutePath))
        }, onFinish = {
            lifecycleScope.launch(Dispatchers.Main) {
                if (list.isNotEmpty()) {
                    binding.autonomyedto.visibility = View.VISIBLE
                    mediaSubAdapter.setData(list)
                }
            }
        })
    }

    private fun getFilesMediaDir(): File {
        return if (mediaType == "Photos") {
            File(filesDir, "photos")
        } else {
            File(filesDir, "videos")
        }
    }

}