package rallyedto.remarkedto.cooperationedto.agricultureedto

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.WeedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import rallyedto.battleedto.acresedto.Circulationedto.loadFileByFilter
import rallyedto.battleedto.acresedto.Dolledto
import rallyedto.battleedto.acresedto.Napedto.checkStorePermission
import rallyedto.battleedto.acresedto.Napedto.requestStorePermission
import rallyedto.dumpedto.editionedto.Moodedto
import rallyedto.farewelledto.Technologyedto.showCameraSettingDialog
import rallyedto.farewelledto.Technologyedto.showGerPermission
import rallyedto.farewelledto.Technologyedto.showImageImportDialog
import rallyedto.finishedto.exampleedto.Personedto
import rallyedto.finishedto.exampleedto.Speededto
import rallyedto.finishedto.exampleedto.Thoseedto
import rallyedto.talledto.blewedto.Musicaledto
import java.io.File
import kotlin.random.Random

class Swimmingedto(
    val mediaType: String
) : Dolledto<WeedtoBinding>() {

    private val TAG = "PrivacyPageFragment"

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

    private lateinit var adapter: Moodedto

    override fun setView() {
        adapter = Moodedto()
        binding.autonomyedto.adapter = adapter

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

            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.zuluedto, binding.popularedto).forEach {
            it.setOnClickListener {
                requireContext().showImageImportDialog(import = {
                    val launcher = (requireActivity() as Thoseedto).launcher
                    launcher.launch(
                        Intent(requireContext(), Speededto::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.establishedto.setOnRefreshListener {
            binding.establishedto.isRefreshing = true
            initData {
                binding.root.postDelayed({
                    binding.establishedto.isRefreshing = false
                }, Random.nextLong(1500))
            }
        }
        binding.preachedto.setOnClickListener {
            startActivity(Intent(requireContext(), Personedto::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 = {
                Musicaledto.logDebug(TAG, "$it")
                list.add(it.absolutePath)
            },
            onFinish = {
                lifecycleScope.launch(Dispatchers.Main) {
                    if (list.isNotEmpty()) {
                        binding.spontaneousedto.visibility = View.GONE
                        binding.autonomyedto.visibility = View.VISIBLE
                        binding.zuluedto.visibility = View.VISIBLE
                        binding.preachedto.visibility = View.VISIBLE
                    }
                    adapter.setData(list)
                    callback?.invoke()
                }
            })
    }
}