package com.base.datarecovery.fragment

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.activity.PrivacyImportActivity
import com.base.datarecovery.activity.PrivacySpaceActivity
import com.base.datarecovery.adapter.FileGridAdapter
import com.base.datarecovery.databinding.FragmentPrivacyPageBinding
import com.base.datarecovery.help.BaseFragment
import com.base.datarecovery.help.FileHelp.loadFileByFilter
import com.base.datarecovery.utils.LogEx
import com.base.datarecovery.view.DialogViews.showCameraSettingDialog
import com.base.datarecovery.view.DialogViews.showImageImportDialog
import java.io.File
import kotlin.random.Random

class PrivacyPageFragment(
    val type: String
) : BaseFragment<FragmentPrivacyPageBinding>() {

    private val TAG = "PrivacyPageFragment"

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

    private lateinit var adapter: FileGridAdapter

    override fun setView() {
        adapter = FileGridAdapter()
        binding.rv.adapter = adapter
        initData()
    }

    override fun setListener() {
        arrayOf(binding.ivAdd, binding.ivAddEmpty).forEach {
            it.setOnClickListener {
                requireContext().showImageImportDialog(import = {
                    val launcher = (requireActivity() as PrivacySpaceActivity).launcher
                    launcher.launch(
                        Intent(requireContext(), PrivacyImportActivity::class.java)
                    ) { activityResult ->
                        val result = activityResult.data?.extras?.getString("Result")
                        Toast.makeText(requireContext(), result, Toast.LENGTH_SHORT).show()
                        initData()
                    }
                }, camera = {
                    requireContext().showCameraSettingDialog()
                })
            }
        }
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.swipeRefreshLayout.isRefreshing = true
            initData()
            binding.root.postDelayed({
                binding.swipeRefreshLayout.isRefreshing = false
            }, Random.nextLong(2500))
        }

    }


    private fun initData() {
        val dir = if (type == "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 = {
                LogEx.logDebug(TAG, "$it")
                list.add(it.absolutePath)
            },
            onFinish = {
                if (list.isNotEmpty()) {
                    binding.flEmpty.visibility = View.GONE
                    binding.rv.visibility = View.VISIBLE
                    binding.ivAdd.visibility = View.VISIBLE
                    binding.tvManager.visibility = View.VISIBLE
                    adapter.setData(list)

                }
            })
    }
}