package com.base.datarecovery.fragment

import android.view.View
import androidx.lifecycle.lifecycleScope
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.view.DialogViews.showImageImportDialog
import java.io.File

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()
            }
        }

    }


    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 = { list.add(it.absolutePath) },
            onFinish = {
                if (list.isNotEmpty()) {
                    binding.flEmpty.visibility = View.GONE
                    binding.ivAdd.visibility = View.VISIBLE
                    binding.tvManager.visibility = View.VISIBLE
                    adapter.setData(list)

                }
            })
    }
}