package rowedto.jockeyedto.sheetedto.slideedto

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.NationalismedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.atticedto.harboredto.Ambiguityedto
import rowedto.jockeyedto.sheetedto.atticedto.harboredto.Princeedto
import rowedto.jockeyedto.sheetedto.atticedto.harboredto.Reignedto
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showCameraSettingDialog
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showGerPermission
import rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto.Patedto.showImageImportDialog
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Aloofedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Everythingedto.loadFileByFilter
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.checkStorePermission
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Studiededto.requestStorePermission
import rowedto.jockeyedto.sheetedto.meadowedto.naturaledto.idealedto.Rightedto
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Dishedto
import java.io.File
import kotlin.random.Random

class Offsetedto(
    val mediaType: String
) : Aloofedto<NationalismedtoBinding>() {

    private val TAG = "PrivacyPageFragment"

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

    private lateinit var adapter: Rightedto

    override fun setView() {
        adapter = Rightedto()
        binding.funeraledto.adapter = adapter

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

            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.marineedto, binding.ejectedto).forEach {
            it.setOnClickListener {
                requireContext().showImageImportDialog(import = {
                    val launcher = (requireActivity() as Reignedto).launcher
                    launcher.launch(
                        Intent(requireContext(), Princeedto::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.stubbornedto.setOnRefreshListener {
            binding.stubbornedto.isRefreshing = true
            initData {
                binding.root.postDelayed({
                    binding.stubbornedto.isRefreshing = false
                }, Random.nextLong(1500))
            }
        }
        binding.reformedto.setOnClickListener {
            startActivity(Intent(requireContext(), Ambiguityedto::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 = {
                Dishedto.logDebug(TAG, "$it")
                list.add(it.absolutePath)
            },
            onFinish = {
                lifecycleScope.launch(Dispatchers.Main) {
                    if (list.isNotEmpty()) {
                        binding.strokeedto.visibility = View.GONE
                        binding.funeraledto.visibility = View.VISIBLE
                        binding.marineedto.visibility = View.VISIBLE
                        binding.reformedto.visibility = View.VISIBLE
                    }
                    adapter.setData(list)
                    callback?.invoke()
                }
            })
    }
}