package importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto

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.CarryedtoBinding
import importantedto.admireedto.sweptedto.Differenceedto
import importantedto.afteredto.constitutionaledto.Concessionedto.loadFileByFilter
import importantedto.afteredto.constitutionaledto.Memberedto.updateMediaStore
import importantedto.afteredto.constitutionaledto.Murderedto
import importantedto.concreteedto.cameedto.Vigorousedto
import importantedto.deserveedto.manufactureredto.viableedto.Characteredto
import importantedto.gallonedto.Transcendedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showDeletePermanentlyDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class Shirtedto : Murderedto<CarryedtoBinding>() {

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

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


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


        mediaSubAdapter = Characteredto()
        binding.questedto.adapter = mediaSubAdapter
        initData()
    }

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

        binding.spitedto.setOnClickListener {
            downloadCopy()
        }
        binding.earthwaxedto.setOnClickListener {
            deleteFilesDirMedia()
        }
        binding.bazaaredto.setOnClickListener {
            binding.bazaaredto.isSelected = !binding.bazaaredto.isSelected
            mediaSubAdapter.toggleSelect(binding.bazaaredto.isSelected)
            if (binding.bazaaredto.isSelected) {
                binding.bazaaredto.text = "Unselect All"
            } else {
                binding.bazaaredto.text = "Select All"
            }
        }
        binding.formationedto.setOnClickListener {
            finish()
        }
    }

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

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

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

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

}