package com.cheng.bole.ui.fragment.home

import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.RecyclerView.HORIZONTAL
import com.cheng.bole.R
import com.cheng.bole.bean.BidItemBean
import com.cheng.bole.bean.MenuEntity
import com.cheng.bole.common.Constants
import com.cheng.bole.common.EventConstants
import com.cheng.bole.databinding.FragmentHomeBinding
import com.cheng.bole.manager.EventReportManager
import com.cheng.bole.manager.SearchHistoryManager
import com.cheng.bole.ui.activity.PublicActivity
import com.cheng.bole.ui.fragment.bid.BidAdapter
import com.cheng.bole.ui.fragment.bid.detail.BidDetailFragment
import com.cheng.bole.ui.fragment.mine.vip.VipFragment
import com.cheng.bole.ui.fragment.search.SearchFragment
import com.cheng.bole.ui.fragment.search.list.SearchListFragment
import com.example.base.decoration.GridSpaceItemDecoration
import com.example.base.decoration.SpacesItemDecoration
import com.example.base.extensions.getColor
import com.example.base.extensions.onClick
import com.example.base.ui.list.ListFragment
import com.example.base.utils.DensityUtils
import com.example.base.utils.SpanUtils
import java.text.DecimalFormat
import kotlin.math.abs


/**
 * 首页
 */
class HomeFragment : ListFragment<FragmentHomeBinding, HomeViewModel, BidItemBean>() {
    private val menuAdapter by lazy { HomeMenuAdapter() }

    private val keywordAdapter by lazy { HomeKeywordAdapter() }

    override fun noDataClick() {}

    override fun bindAdapter() = BidAdapter()

    private var item: BidItemBean? = null

    override fun initView() {
        super.initView()
        binding.tvTitle.typeface = Constants.pmzdbt
        binding.tvHotTitle.typeface = Constants.pmzdbt
        binding.tvBidTitleNew.typeface = Constants.pmzdbt
        binding.tvBidTitle.typeface = Constants.pmzdbt

        binding.rvMenu.adapter = menuAdapter
        binding.rvMenu.addItemDecoration(GridSpaceItemDecoration(4, DensityUtils.dp2px(16f), 0))
        menuAdapter.setList(MenuEntity.getHomeMenu())

        binding.rvHotKeyword.adapter = keywordAdapter
        binding.rvHotKeyword.addItemDecoration(SpacesItemDecoration(DensityUtils.dp2px(8f), HORIZONTAL))
    }

    override fun initData() {
        super.initData()
        mViewModel.getHotKeywordList("")
        firstLoad()
    }

    override fun initListener() {
        super.initListener()
        binding.appBarLayout.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
            val lp = binding.layoutContent.layoutParams as CoordinatorLayout.LayoutParams
            lp.topMargin = (-DensityUtils.dp2px(45f) * (1f - abs(verticalOffset).toFloat() / appBarLayout.totalScrollRange)).toInt()
            binding.layoutContent.layoutParams = lp
        }

        binding.tvSearch.onClick {
            PublicActivity.start(requireContext(), SearchFragment::class.java)
            EventReportManager.eventReport(EventConstants.JUMP_TO_BID_SEARCH)
        }

        binding.ivRefresh.onClick {
            if (keywordAdapter.data.isNotEmpty()) {
                mViewModel.getHotKeywordList(keywordAdapter.data.last())
            } else {
                mViewModel.getHotKeywordList("")
            }
        }

        mAdapter.setOnItemClickListener { _, _, i ->
            mAdapter.setOnItemClickListener { _, _, i ->
                item = mAdapter.getItem(i)
                mViewModel.checkAuth()
            }
        }

        keywordAdapter.setOnItemClickListener { _, _, i ->
            val item = keywordAdapter.getItem(i)
            PublicActivity.start(requireContext(), SearchListFragment::class.java,Pair("keyword", item))
            SearchHistoryManager.addHistory(item)
        }
    }

    override fun initObserve() {
        super.initObserve()
        mViewModel.recommendLiveData.observe(this) {
            keywordAdapter.setList(it)
        }

        mViewModel.totalLiveData.observe(this) {
            SpanUtils.with(binding.tvUpdateCount)
                .append("已更新")
                .append(DecimalFormat("0.#").format(it.toInt()/ 10000f))
                .setForegroundColor(getColor(R.color.color_ff2222))
                .append("万条")
                .create()
        }

        mViewModel.authLiveData.observe(this) {
            if (it.auth) {
                PublicActivity.start(
                    requireContext(),
                    BidDetailFragment::class.java,
                    Pair("id", item!!.id),
                    Pair("info_source", item!!.info_source)
                )
            } else {
                PublicActivity.start(requireContext(), VipFragment::class.java, Pair("origin", "view_bid"))
            }
        }
    }
}