package com.base.superfilemanager.activity

import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import com.base.superfilemanager.adapter.FileBrowseAdapter
import com.base.superfilemanager.bean.FileBean
import com.base.superfilemanager.bean.FileCategoryBean
import com.base.superfilemanager.bean.FileCategoryBean.Companion.APK
import com.base.superfilemanager.bean.FileCategoryBean.Companion.Audio
import com.base.superfilemanager.bean.FileCategoryBean.Companion.DOC
import com.base.superfilemanager.bean.FileCategoryBean.Companion.Image
import com.base.superfilemanager.bean.FileCategoryBean.Companion.PDF
import com.base.superfilemanager.bean.FileCategoryBean.Companion.PPT
import com.base.superfilemanager.bean.FileCategoryBean.Companion.TXT
import com.base.superfilemanager.bean.FileCategoryBean.Companion.Video
import com.base.superfilemanager.bean.FileCategoryBean.Companion.XLS
import com.base.superfilemanager.bean.FileCategoryBean.Companion.ZIP
import com.base.superfilemanager.databinding.ActivityFileCategoryBinding
import com.base.superfilemanager.helps.BaseActivity
import com.base.superfilemanager.helps.FileHelps
import com.base.superfilemanager.helps.file.FileEx.deleteDirectory
import com.base.superfilemanager.view.FileDeleteDialog.showFileDeleteDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.File
import java.lang.Exception

class FileCategoryActivity : BaseActivity<ActivityFileCategoryBinding>() {

    private var type: String = ""
    private lateinit var adapter: FileBrowseAdapter

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


    override fun initView() {
        type = intent.extras?.getString("type") ?: ""
        binding.tvTittle.text = type
        adapter = FileBrowseAdapter { size ->
            binding.tvDelete.isSelected = size > 0
            binding.tvDelete.isEnabled = size > 0
        }
        binding.rv.adapter = adapter
        binding.rv.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL))
        binding.flBack.setOnClickListener {
            finishToMain()
        }
        binding.tvDelete.setOnClickListener {
            val deleteList = adapter.getSelect()
            showFileDeleteDialog {
                binding.clOther.visibility = View.VISIBLE
                binding.progressbar.visibility = View.VISIBLE

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

            }
        }
    }

    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    override fun onPermissionsResult(isGranted: Boolean) {
        if (isGranted) {
            initData()
        } else {
            finishToMain()
        }
    }

    private fun initData() {
        val showList = HashSet<FileBean>()
        binding.clOther.visibility = View.VISIBLE
        binding.progressbar.visibility = View.VISIBLE
        FileHelps.getFileList(
            onUpdate = {
                val temp = arrayListOf<FileBean>()
                temp.addAll(FileHelps.fileList)
                if (temp.isNotEmpty()) {
                    when (type) {
                        FileCategoryBean.ALL -> showList.addAll(temp.filter { it.isDoc() })
                        XLS -> showList.addAll(temp.filter { it.isXls() })
                        DOC -> showList.addAll(temp.filter { it.isDocx() })
                        PDF -> showList.addAll(temp.filter { it.isPdf() })
                        ZIP -> showList.addAll(temp.filter { it.isZip() })
                        TXT -> showList.addAll(temp.filter { it.isTxt() })
                        PPT -> showList.addAll(temp.filter { it.isPpt() })
                        Video -> showList.addAll(temp.filter { it.isVideo() })
                        Audio -> showList.addAll(temp.filter { it.isAudio() })
                        APK -> showList.addAll(temp.filter { it.isApk() })
                        Image -> showList.addAll(temp.filter { it.isImage() })
                    }
                    if (showList.isNotEmpty()) {
                        binding.clOther.visibility = View.GONE
                        binding.progressbar.visibility = View.GONE
                    }
                    adapter.setData(showList.toList())
                }
            }, onEnd = {
                if (showList.isEmpty()) {
                    binding.progressbar.visibility = View.GONE
                    binding.ivEmpty.visibility = View.VISIBLE
                    binding.tvDelete.visibility = View.GONE
                }
            })
    }
}