package com.zxbw.modulemain.fragment

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.zxbw.modulemain.R
import com.zxbw.modulemain.activity.XymhTbGoodsDetailActivity
import com.zxbw.modulemain.adapter.*
import com.zxbw.modulemain.contract.ShopRecommendContract
import com.zxbw.modulemain.presenter.ShopRecommendPresenter
import com.zxhl.cms.common.Constant
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.model.box.TabEntity
import com.zxhl.cms.net.model.qy.JgqQyEntity
import com.zxhl.cms.net.model.qy.TbGoodsItemEntity
import com.zxhl.cms.utils.JumpUtils
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.widget.RecyclerViewSpacesItemDecoration2
import kotlinx.android.synthetic.main.fragment_layout_recommend.*

/**
 * @author (wangXuewei)
 * @datetime 2022-05-20 19:01 GMT+8
 * @detail : 商城推荐
 */
class ShopRecommendFragment : BaseFragment, ShopRecommendContract.View,
    OnRecycleItemClickListener<TbGoodsItemEntity> {

    private val tabEntity: TabEntity
    private var mPresenter: ShopRecommendPresenter? = null
    private var mGoodsAdapterXymh: XymhShopRecommendAdapter? = null;

    override fun layoutID(): Int {
        return R.layout.fragment_layout_recommend
    }

    override fun init(view: View?) {
        mPresenter = ShopRecommendPresenter(this)


        mGoodsAdapterXymh = XymhShopRecommendAdapter(activity, this)
        //底部商品列表
        val gridLayoutManager = GridLayoutManager(activity, 2)
        gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (position == 0||position == 1) {
                    return 2
                }
                return 1
            }
        }
        id_rcl_goods_list?.layoutManager = gridLayoutManager
        id_rcl_goods_list?.addItemDecoration(RecyclerViewSpacesItemDecoration2())
        id_rcl_goods_list.adapter = mGoodsAdapterXymh

        id_refresh_view.setRefreshHeader(ClassicsHeader(activity))
        id_refresh_view.setRefreshFooter(ClassicsFooter(activity))

        id_refresh_view.setOnRefreshListener {
            mPresenter?.getCategoryList(tabEntity.name)
            mPresenter?.getShopGoodsList(true, tabEntity)
        }
        id_refresh_view.setOnLoadMoreListener {
            mPresenter?.getShopGoodsList(false, tabEntity)
        }
        mPresenter?.getCategoryList(tabEntity.name)
        mPresenter?.getShopGoodsList(true, tabEntity)
    }

    companion object {
        fun newInstance(tab: TabEntity): ShopRecommendFragment {
            return ShopRecommendFragment(tab)
        }
    }

    constructor(tab: TabEntity) : super() {
        this.tabEntity = tab
    }

    constructor() : super() {
        this.tabEntity = TabEntity()
    }

    override fun onItemClick(view: View?, position: Int, data: TbGoodsItemEntity?) {
//        JumpUtils.TbGoodsDetailJump()
        if (data?.bannerType.equals("blindBox")) {
            if (!Constant.Switch.isOpenVip) {
                JumpUtils.MemberOrderJump()
            } else {
                JumpUtils.goodsDetailJump(data?.boxId)
            }
        } else {
            var intent = Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.putExtra("goods", data)
            intent.setClass(activity!!, XymhTbGoodsDetailActivity().javaClass)
            activity?.startActivity(intent)
        }

    }

    override fun setCategoryList(result: List<JgqQyEntity>?) {
        if (!result.isNullOrEmpty()) {
            mGoodsAdapterXymh?.updateShopCategoryData(result)
        }
    }

    override fun setShopGoodsList(isRefresh: Boolean, result: List<TbGoodsItemEntity>?) {
        if (isRefresh) {
            id_refresh_view.finishRefresh(!result.isNullOrEmpty());//传入false表示加载失败
        } else {
            id_refresh_view.finishLoadMore(!result.isNullOrEmpty());//传入false表示加载失败
        }

        if (isRefresh && !result.isNullOrEmpty()) {
            mGoodsAdapterXymh?.clear()
        }
        if (!result.isNullOrEmpty()) {
            mGoodsAdapterXymh?.appendToList(result)
        }
    }

}