package com.cheng.blzb.ui.fragment.mine.favorite

import com.cheng.blzb.R
import com.cheng.blzb.bean.BidItemEntity
import com.cheng.blzb.databinding.FragmentFavoriteBinding
import com.cheng.blzb.event.FavoriteEvent
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.fragment.bid.BidAdapter
import com.cheng.blzb.ui.fragment.bid.detail.BidDetailFragment
import com.cheng.blzb.ui.fragment.mine.vip.VipFragment
import com.example.base.common.RxBus
import com.example.base.ui.list.ListFragment

class FavoriteFragment: ListFragment<FragmentFavoriteBinding, FavoriteViewModel, BidItemEntity>() {
    private var item: BidItemEntity? = null

    override fun noDataClick() {

    }

    override fun bindAdapter() = BidAdapter()

    override fun initView() {
        super.initView()
        mTitleBar?.background = null

        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_favorite)
        mEmptyView.setNoDataText("暂无收藏")
    }

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

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

    override fun initObserve() {
        super.initObserve()
        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"))
            }
        }

        val favoriteEvent = RxBus.defaultInstance.toObservable(FavoriteEvent::class.java).subscribe {
            firstLoad()
        }
        addDisposable(favoriteEvent)
    }
}