package com.zxbw.modulemain.fragment

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.zxbw.modulemain.R
import com.zxbw.modulemain.activity.TbGoodsDetailActivity
import com.zxbw.modulemain.adapter.GoodsListAdapter
import com.zxbw.modulemain.adapter.HomeRecommendAdapter
import com.zxbw.modulemain.adapter.JgqQyAdapter
import com.zxbw.modulemain.adapter.ZeroBuyGoodsListAdapter
import com.zxbw.modulemain.contract.HomeRecommendContract
import com.zxbw.modulemain.presenter.HomeRecommendPresenter
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 kotlinx.android.synthetic.main.fragment_layout_recommend.*

/**
 * @author (wangXuewei)
 * @datetime 2022-05-20 19:01 GMT+8
 * @detail : 首页推荐
 */
class RecommendFragment : BaseFragment, HomeRecommendContract.View,
    OnRecycleItemClickListener<TbGoodsItemEntity> {

    private val tabEntity: TabEntity
    private var mPresenter: HomeRecommendPresenter? = null
    private var mGoodsAdapter: HomeRecommendAdapter? = null;

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

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


        mGoodsAdapter = HomeRecommendAdapter(activity, this)
        mGoodsAdapter?.IsShowQyView(tabEntity.name == "推荐")
        //底部商品列表
        id_rcl_goods_list?.layoutManager = LinearLayoutManager(activity)
        id_rcl_goods_list.adapter = mGoodsAdapter

        id_refresh_view.setRefreshHeader(ClassicsHeader(activity))
        id_refresh_view.setRefreshFooter(ClassicsFooter(activity))
        //TODO 测试只加载权益，不加载列表是否有问题
        id_refresh_view.setOnRefreshListener {
//            it.finishRefresh(2000/*,false*/);//传入false表示加载失败
            if (tabEntity.name == "猜你喜欢") {
                mPresenter?.getGuessLikeGoodsList(true)
            } else {
                mPresenter?.getTbGoodsList(true, tabEntity)
            }
            mPresenter?.getQyCardList(tabEntity.name)
        }
        id_refresh_view.setOnLoadMoreListener {
//            it.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            if (tabEntity.name == "猜你喜欢") {
                mPresenter?.getGuessLikeGoodsList(false)
            } else {
                mPresenter?.getTbGoodsList(false, tabEntity)
            }
        }
        if (tabEntity.name == "猜你喜欢") {
            mPresenter?.getGuessLikeGoodsList(true)
        } else {
            mPresenter?.getTbGoodsList(true, tabEntity)
        }

        mPresenter?.getQyCardList(tabEntity.name)
    }

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

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

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

    override fun setQyCardList(result: List<JgqQyEntity>?) {
        if (!result.isNullOrEmpty()) {
            mGoodsAdapter?.updateHomeQyData(result)
        }
    }

    override fun setTbGoodsList(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()) {
            mGoodsAdapter?.clear()
        }
        if (!result.isNullOrEmpty()) {
            mGoodsAdapter?.appendToList(result)
        }
    }

    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!!, TbGoodsDetailActivity().javaClass)
            activity?.startActivity(intent)
        }

    }

}