package importantedto.candidateedto

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.databinding.StilledtoBinding
import importantedto.afteredto.constitutionaledto.Concessionedto.loadFileByFilter
import importantedto.afteredto.constitutionaledto.Jumpedto
import importantedto.afteredto.constitutionaledto.Rackedto.checkStorePermission
import importantedto.afteredto.constitutionaledto.Rackedto.requestStorePermission
import importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto.Exclaimededto
import importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto.Hopeedto
import importantedto.appliededto.surgeonedto.promiseedto.respectableedto.prisonedto.comfortableedto.Shirtedto
import importantedto.deserveedto.manufactureredto.viableedto.Testifyedto
import importantedto.gallonedto.Tumbleedto
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showCameraSettingDialog
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showGerPermission
import importantedto.maidedto.witnessedto.nationalismedto.Photocopyedto.showImageImportDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import kotlin.random.Random

class Stepedto(
    val mediaType: String
) : Jumpedto<StilledtoBinding>() {

    private val TAG = "PrivacyPageFragment"

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

    private lateinit var adapter: Testifyedto

    override fun setView() {
        adapter = Testifyedto()
        binding.questedto.adapter = adapter

        if (requireContext().checkStorePermission()) {
            initData()
        } else {
            val activity = (requireActivity() as Exclaimededto)

            requireContext().showGerPermission("Privacy Space Access",
                "To help you protect your privacy, our app needs to scan your device for photos and videos. We will use this information to create a secure private space that only you can access. We do not collect any personal information or data. Please click \"Agree\" to grant us permission to scan your device or \"Cancel\" to exit.",
                denyText = "Exit",
                allowText = "Agree",
                deny = {
                    activity.finishToMain()
                },
                allow = {
                    requireContext().requestStorePermission(activity.launcher, result = {
                        if (it) {
                            initData()
                        } else {
                            activity.finishToMain()
                        }
                    })
                })

        }
    }

    override fun setListener() {
        arrayOf(binding.termsedto, binding.relationedto).forEach {
            it.setOnClickListener {
                requireContext().showImageImportDialog(import = {
                    val launcher = (requireActivity() as Exclaimededto).launcher
                    launcher.launch(
                        Intent(requireContext(), Hopeedto::class.java)
                    ) { activityResult ->
                        val result = activityResult.data?.extras?.getString("Result")
                        if(!TextUtils.isEmpty(result)){
                            Toast.makeText(requireContext(), result, Toast.LENGTH_SHORT).show()
                        }
                        initData()
                    }
                }, camera = {
                    requireContext().showCameraSettingDialog()
                })
            }
        }
        binding.rubedto.setOnRefreshListener {
            binding.rubedto.isRefreshing = true
            initData {
                binding.root.postDelayed({
                    binding.rubedto.isRefreshing = false
                }, Random.nextLong(1500))
            }
        }
        binding.loyaltyedto.setOnClickListener {
            startActivity(Intent(requireContext(), Shirtedto::class.java).apply {
                putExtra("mediaType", mediaType)
            })
        }

    }


    private fun initData(callback: (() -> Unit)? = null) {
        val dir = if (mediaType == "Photos") {
            File(requireContext().filesDir, "photos")
        } else {
            File(requireContext().filesDir, "videos")
        }
        if (!dir.exists()) {
            dir.mkdirs()
        }
        val list = arrayListOf<String>()

        lifecycleScope.loadFileByFilter(folder = dir,
            filter = { true },
            onDo = {
                Tumbleedto.logDebug(TAG, "$it")
                list.add(it.absolutePath)
            },
            onFinish = {
                lifecycleScope.launch(Dispatchers.Main) {
                    if (list.isNotEmpty()) {
                        binding.bushedto.visibility = View.GONE
                        binding.questedto.visibility = View.VISIBLE
                        binding.termsedto.visibility = View.VISIBLE
                        binding.loyaltyedto.visibility = View.VISIBLE
                    }
                    adapter.setData(list)
                    callback?.invoke()
                }
            })
    }
}