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

import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.browserwhite.bean.BookmarkBean
import com.base.browserwhite.databinding.ActivityBookmarkBinding
import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.SpBeanUtils
import com.base.browserwhite.utils.SpBeanUtils.BOOKMARK_SP_KEY
import com.google.gson.Gson

class BookmarkActivity : BaseActivity<ActivityBookmarkBinding>() {

    private val TAG = "BookmarkActivity"
    private var currentTab = BOOKMARK_TAB


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

    private lateinit var bookmarkAdapter: BookmarkAdapter

    private val bookmarkList = arrayListOf<BookmarkBean>()

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        bookmarkAdapter = BookmarkAdapter()
        bookmarkAdapter.changeFolderAction = { folder ->
            val list = bookmarkList.filter { it.folder == folder }
            bookmarkAdapter.submitList(list)
        }

        changeBookmark()
    }

    private fun changeBookmark() {
        binding.rv.adapter = bookmarkAdapter
        bookmarkAdapter.submitList(bookmarkList)
    }

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

    private fun initData() {
        initBookmarkData()
    }

    private fun initBookmarkData() {
        bookmarkList.clear()
        val list = SpBeanUtils.getSpBeanList(BOOKMARK_SP_KEY)
        val items = list.map { Gson().fromJson(it, BookmarkBean::class.java) }

        bookmarkList.addAll(items)
        bookmarkAdapter.submitList(bookmarkList.filter { it.folder == bookmarkAdapter.currentFolder })
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (currentTab == BOOKMARK_TAB) {
                if (bookmarkAdapter.canBeforeFolder()) {
                    bookmarkAdapter.beforeFolder()
                } else {
                    finishToMain()
                }
            } else {
                finishToMain()
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

        binding.tvTabBookmark.setOnClickListener {
            if (binding.tvTabBookmark.isSelected) {
                return@setOnClickListener
            }
            binding.tvTabBookmark.isSelected = true
            binding.tvTabHistory.isSelected = false
            currentTab = BOOKMARK_TAB
        }
        binding.tvTabBookmark.callOnClick()

        binding.tvTabHistory.setOnClickListener {
            if (binding.tvTabHistory.isSelected) {
                return@setOnClickListener
            }
            binding.tvTabHistory.isSelected = true
            binding.tvTabBookmark.isSelected = false
            currentTab = HISTORY_TAB
        }
    }

    companion object {
        private val BOOKMARK_TAB = "bookmark_tab"
        private val HISTORY_TAB = "history_tab"
    }

}