package rowedto.jockeyedto.sheetedto.atticedto.harboredto

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.ChildedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showDeletePermanentlyDialog
import rowedto.jockeyedto.sheetedto.debutedto.revolutionaryedto.Cunningedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Affairedto.updateMediaStore
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Crossingedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Everythingedto.loadFileByFilter
import rowedto.jockeyedto.sheetedto.meadowedto.naturaledto.idealedto.Exerciseedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Spareedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import java.io.File

class Ambiguityedto : Crossingedto<ChildedtoBinding>() {

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

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


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


        mediaSubAdapter = Exerciseedto()
        binding.funeraledto.adapter = mediaSubAdapter
        initData()
    }

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

        binding.marbleedto.setOnClickListener {
            downloadCopy()
        }
        binding.plagiarizeedto.setOnClickListener {
            deleteFilesDirMedia()
        }
        binding.glassedto.setOnClickListener {
            binding.glassedto.isSelected = !binding.glassedto.isSelected
            mediaSubAdapter.toggleSelect(binding.glassedto.isSelected)
            if (binding.glassedto.isSelected) {
                binding.glassedto.text = "Unselect All"
            } else {
                binding.glassedto.text = "Select All"
            }
        }
        binding.timeedto.setOnClickListener {
            finish()
        }
    }

    private fun deleteFilesDirMedia() {
        showDeletePermanentlyDialog {
            Scandaledto.admobMaxShowInterstitialAd(this,false){
                lifecycleScope.launch(Dispatchers.IO) {
                    val list = mediaSubAdapter.getSelectData()
                    list.forEach {
                        runCatching { File(it.path).delete() }
                    }
                    launch(Dispatchers.Main) {
                        Toast.makeText(this@Ambiguityedto, "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@Ambiguityedto.updateMediaStore()
        launch(Dispatchers.Main) {
            Toast.makeText(this@Ambiguityedto, "success", Toast.LENGTH_SHORT).show()
            mediaSubAdapter.toggleSelect(false)
        }
    }

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

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

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

}