package com.base.pdfreader2.ui.main

import android.annotation.SuppressLint
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.pdfreader2.bean.ConstObject
import com.base.pdfreader2.bean.DocumentBean.Companion.TYPE_PDF
import com.base.pdfreader2.databinding.FragmentDocumentBinding
import com.base.pdfreader2.helper.BaseFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class DocumentFragment() : BaseFragment<FragmentDocumentBinding>() {

    private val TAG = "DocumentFragment"
    private var type = TYPE_PDF

    private lateinit var adapter: DocumentAdapter
    private lateinit var documentPresenter: DocumentPresenter

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

    constructor(type: String) : this() {
        this.type = type
    }

    override fun setView() {
        documentPresenter = DocumentPresenter(requireContext())
        adapter = DocumentAdapter()
        binding.rv.adapter = adapter

        binding.swipeRefreshLayout.isRefreshing = true
    }

    override fun setListener() {
        super.setListener()
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.swipeRefreshLayout.isRefreshing = true
            refreshData()
        }
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    fun refreshData() {
        if (isInitialized && !isHidden && isVisible) {
            val mainActivity = (requireActivity() as MainActivity?)
            mainActivity ?: return
            when (mainActivity.dataType) {
                ConstObject.DOCUMENT_DATA_TYPE -> refreshAllData()
                ConstObject.RECENT_DATA_TYPE -> refreshRecentData()
                ConstObject.BOOKMARK_DATA_TYPE -> refreshBookmarkData()
            }
        }
    }

    private fun refreshRecentData() = lifecycleScope.launch(Dispatchers.IO) {
        val list = documentPresenter.getRecentDocument(type)
        launch(Dispatchers.Main) {
            binding.llEmpty.isVisible = list.isEmpty()
            adapter.submitList(list)
            binding.swipeRefreshLayout.isRefreshing = false
        }
    }

    private fun refreshBookmarkData() = lifecycleScope.launch(Dispatchers.IO) {
        val list = documentPresenter.getBookmarkDocument(type)
        lifecycleScope.launch(Dispatchers.Main) {
            binding.llEmpty.isVisible = list.isEmpty()
            binding.swipeRefreshLayout.isRefreshing = false
            adapter.submitList(documentPresenter.sortByMode(list))
        }
    }

    private fun refreshAllData() = lifecycleScope.launch(Dispatchers.IO) {
        val list = documentPresenter.getAllDocument(type)
        launch(Dispatchers.Main) {
            binding.llEmpty.isVisible = list.isEmpty()
            binding.swipeRefreshLayout.isRefreshing = false
            adapter.submitList(documentPresenter.sortByMode(list))
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun changeSelectUi(select: Boolean) {
        if (isVisible) {
            adapter.items.forEach {
                if (select) {
                    it.uiType = 1
                    binding.swipeRefreshLayout.isEnabled = false
                } else {
                    it.isSelect = false
                    it.uiType = 0
                    binding.swipeRefreshLayout.isEnabled = true
                }
            }
            adapter.notifyDataSetChanged()
        }
    }


}