package com.base.browserwhite.ui.activity.bookmark

import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.base.browserwhite.R
import com.base.browserwhite.bean.BookmarkBean
import com.base.browserwhite.bean.HistoryBean
import com.base.browserwhite.databinding.FragmentHistoryBinding
import com.base.browserwhite.ui.activity.webbrowser.WebBrowserActivity
import com.base.browserwhite.ui.fragment.BaseFragment
import com.base.browserwhite.ui.views.BookmarkDialog.showBookmarkMoreDialog
import com.base.browserwhite.ui.views.DialogViews.showDeleteTipDialog
import com.base.browserwhite.utils.KotlinExt.toFormatTime
import com.base.browserwhite.utils.ShortcutUtils
import com.base.browserwhite.utils.SpBeanUtils
import com.base.browserwhite.utils.SpBeanUtils.BOOKMARK_SP_KEY
import com.base.browserwhite.utils.SpBeanUtils.HISTORY_SP_KEY
import com.base.browserwhite.utils.ToastUtils.toast
import com.google.gson.Gson

class HistoryFragment : BaseFragment<FragmentHistoryBinding>() {


    private lateinit var historyAdapter: HistoryAdapter
    private val historyList = arrayListOf<HistoryBean>()


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

    private var viewInit: Boolean = false

    override fun setView() {

        historyAdapter = HistoryAdapter()
        historyAdapter.moreAction = { view, item: HistoryBean ->
            requireContext().showBookmarkMoreDialog(
                view, 1,
                deleteAction = {
                    requireContext().showDeleteTipDialog("Delete this search history?") {
                        SpBeanUtils.addSpBean(HISTORY_SP_KEY, item)
                    }
                },
                addBookmarkAction = {
                    SpBeanUtils.addSpBean(BOOKMARK_SP_KEY, BookmarkBean(name = item.name, url = item.url))
                    requireContext().toast("add success")
                },
                addDeskAction = {
                    val appName = this.resources.getString(R.string.app_name)
                    ShortcutUtils.requestCreateShortcut(
                        requireContext(), "my_shortcut_id", appName, appName,
                        WebBrowserActivity::class.java, R.mipmap.logo, intentAction = { jIntent ->
                            jIntent.putExtra("url", item.url)
                        }
                    )
                },
                addNavAction = {
                    SpBeanUtils.addSpBean(SpBeanUtils.BOOKMARK_NAV_SP_KEY, BookmarkBean(name = item.name, url = item.url))
                })
        }
        binding.rv.adapter = historyAdapter

        viewInit = true
        initData()
    }

    override fun setListener() {
        super.setListener()

        binding.editSearch.addTextChangedListener {
            if (it.isNullOrEmpty()) {
                showAllList()
                binding.tvCancel.visibility = View.GONE
                return@addTextChangedListener
            } else {
                binding.tvCancel.visibility = View.VISIBLE
                filterList(it.toString())
            }
        }
        binding.tvCancel.setOnClickListener {
            binding.editSearch.setText("")
            showAllList()
        }
    }

    private fun showAllList() {
        val list = historyList
        binding.llEmpty.isVisible = list.isEmpty()
        historyAdapter.submitList(list)
    }

    private fun filterList(content: String) {
        val filterList = historyList.filter { it.name.contains(content) }
        binding.llEmpty.isVisible = filterList.isEmpty()
        historyAdapter.submitList(filterList)
    }

    fun initData() {
        if (!viewInit) return
        historyList.clear()
        val list = SpBeanUtils.getSpBeanList(SpBeanUtils.HISTORY_SP_KEY)
        val gson = Gson()
        val item = list.map { gson.fromJson(it, HistoryBean::class.java) }

        var time = ""
        item.reversed().forEach {
            if (time != it.time.toFormatTime()) {
                it.showTime = true
                time = it.time.toFormatTime()
            }
            historyList.add(it)
        }
        binding.llEmpty.isVisible = historyList.isEmpty()
        runCatching {
            historyAdapter.submitList(historyList)
        }
    }

}