package com.base.easyfilemanager.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import com.base.easyfilemanager.adapter.FileBrowseAdapter
import com.base.easyfilemanager.databinding.ActivityFileCategoryBinding
import com.base.easyfilemanager.bean.FileBean
import com.base.easyfilemanager.bean.FileCategoryBean
import com.base.easyfilemanager.helps.BaseActivity
import com.base.easyfilemanager.helps.PermissionHelp.checkStorePermission
import com.base.easyfilemanager.helps.PermissionHelp.requestStorePermission
import com.base.easyfilemanager.helps.ads.AdmobUtils
import com.base.easyfilemanager.utils.BarUtils
import com.base.easyfilemanager.utils.MediaStoreEx.geFileMedia
import com.base.easyfilemanager.utils.MediaStoreEx.getAudioMedia
import com.base.easyfilemanager.utils.MediaStoreEx.getImageMedia
import com.base.easyfilemanager.utils.MediaStoreEx.getVideoMedia
import com.base.easyfilemanager.view.AdPreparationPop
import com.base.easyfilemanager.view.DialogViews.showGerPermission
import com.base.easyfilemanager.view.FileDeleteDialog.showFileDeleteDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.File

class FileCategoryActivity : BaseActivity<ActivityFileCategoryBinding>() {

    private val TAG = "FileCategoryActivity"
    private var type: String = ""
    private lateinit var adapter: FileBrowseAdapter

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


    @SuppressLint("SetTextI18n")
    override fun initView() {
        initStatusBar()
        type = intent.extras?.getString("type") ?: ""
        binding.tvTittle.text = type
        binding.tvEmptyTip.text = "$type Not Found"
        adapter = FileBrowseAdapter(select = { size ->
            binding.tvDelete.isSelected = size > 0
            binding.tvDelete.isEnabled = size > 0
        }, empty = {
            binding.llEmpty.isVisible = adapter.isEmpty()
        })
        binding.rv.adapter = adapter
        binding.rv.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL))

        AdPreparationPop(this) {
            AdmobUtils.showInterstitialAd(this) {
                if (checkStorePermission()) {
                    initData()
                } else {
                    dialog = showGerPermission(deny = { finishToMain() }) {
                        requestStorePermission(launcher, result = { flag ->
                            if (flag) {
                                initData()
                            } else {
                                finishToMain()
                            }
                        })
                    }
                }
            }
        }.show()

        AdmobUtils.showNativeAd(this, binding.flAd)
    }

    private fun initStatusBar() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
    }


    override fun initListener() {
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            AdmobUtils.showInterstitialAd(this@FileCategoryActivity) {
                finishToMain()
            }
        }
        binding.tvDelete.setOnClickListener {
            val deleteList = adapter.getSelect()
            showFileDeleteDialog {
                binding.progressbar.visibility = View.VISIBLE

                lifecycleScope.launch(Dispatchers.Main) {
                    async {
                        try {
                            deleteList.forEach {
                                val deleteFile = File(it.path)
                                if (deleteFile.isFile) deleteFile.delete()
                            }
                        } catch (e: Exception) {
                        }
                    }.await()
                    adapter.removeData(deleteList)
                    binding.progressbar.visibility = View.GONE

                }

            }
        }
    }

    private fun initData() {
        binding.progressbar.visibility = View.VISIBLE
        val list = arrayListOf<FileBean>()
        when (type) {
            FileCategoryBean.Audio -> {
                getAudioMedia(list)
                list.forEach { fileBean -> fileBean.type = "mp3" }
            }

            FileCategoryBean.Image -> {
                getImageMedia(list)
                list.forEach { fileBean -> fileBean.type = "jpg" }
            }

            FileCategoryBean.Video -> {
                getVideoMedia(list)
                list.forEach { fileBean -> fileBean.type = "mp4" }
            }

            FileCategoryBean.ZIP -> {
                geFileMedia(list, arrayOf("application/zip"))
                list.forEach { fileBean -> fileBean.type = "zip" }
            }

            FileCategoryBean.APK -> {
                geFileMedia(list, arrayOf("application/vnd.android.package-archive"))
                list.forEach { fileBean -> fileBean.type = "apk" }
            }
        }
        binding.progressbar.visibility = View.GONE
        if (list.isEmpty()) {
            binding.llEmpty.isVisible = true
        } else {
            adapter.setData(list)
        }
    }


}