package com.cheng.blzb.ui.fragment.merchant.list

import android.os.Bundle
import com.cheng.blzb.R
import com.cheng.blzb.bean.MerchantGoodsEntity
import com.cheng.blzb.databinding.FragmentMerchantGoodsListBinding
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.fragment.merchant.detail.MerchantGoodsDetailFragment
import com.cheng.blzb.ui.fragment.mine.vip.VipFragment
import com.example.base.decoration.FirstItemOffsetDecoration
import com.example.base.ui.list.ListFragment
import com.example.base.utils.DensityUtils

class MerchantGoodsListFragment : ListFragment<FragmentMerchantGoodsListBinding, MerchantGoodsListViewModel, MerchantGoodsEntity>() {
    private val type by lazy { arguments?.getString("type") ?: "" }
    private val userId by lazy { arguments?.getString("userId") ?: "" }

    private var keyword: String = ""

    private var item: MerchantGoodsEntity? = null

    companion object {
        fun newInstance(type: String, userId: String = ""): MerchantGoodsListFragment {
            val args = Bundle()
            args.putString("type", type)
            args.putString("userId", userId)
            val fragment = MerchantGoodsListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun noDataClick() {

    }

    override fun bindAdapter() = MerchantGoodsAdapter()

    override fun initView() {
        super.initView()

        binding.mRecyclerView.addItemDecoration(FirstItemOffsetDecoration(DensityUtils.dp2px(7f), FirstItemOffsetDecoration.top))
        mEmptyView.setNoDataLogo(R.mipmap.ic_empty_data)
        mEmptyView.setNoDataText("暂无合作商")
    }

    override fun initData() {
        super.initData()
        mViewModel.params["query"] = keyword
        mViewModel.params["type"] = type
        mViewModel.params["user_id"] = userId
        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(requireActivity(), MerchantGoodsDetailFragment::class.java, Pair("id", item!!.id))
            } else {
                PublicActivity.start(requireContext(), VipFragment::class.java, Pair("origin", "view_goods"))
            }
        }
    }


    fun setKeyword(keyword: String) {
        this.keyword = keyword
        if (isResumed) {
            mViewModel.params["query"] = keyword
            mViewModel.params["type"] = type
            mViewModel.params["user_id"] = userId
            firstLoad()
        }
    }

    fun setCity(cityId: String) {
        if (isResumed) {
            mViewModel.params["city_id"] = cityId
            mViewModel.params["type"] = type
            mViewModel.params["user_id"] = userId
            firstLoad()
        }
    }
}