package com.sheep.gamegroup.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sheep.gamegroup.absBase.BaseRefreshLoadMoreFragment;
import com.sheep.gamegroup.model.entity.Article;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.GameInfoList;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.view.adapter.AdpGcGameAppActivityRecommend;
import com.sheep.gamegroup.view.adapter.AdpGcGameAppArticleRecommend;
import com.sheep.gamegroup.view.adapter.AdpGcGameAppGiftBag;
import com.sheep.gamegroup.view.adapter.AdpGcGameAppInfoList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 * 玩转游戏 福利
 */
public class FgtGcGameAppWelfare extends BaseRefreshLoadMoreFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    public static FgtGcGameAppWelfare newInstance(int appId) {
        FgtGcGameAppWelfare fgt = new FgtGcGameAppWelfare();
        fgt.appId = appId;
        return fgt;
    }

    @Override
    public int getLayoutId() {
        return R.layout.common_rv;
    }


    private ArrayList<GameInfoList> gameInfoListArrayList = ListUtil.emptyList();

    @Override
    public void onViewCreated() {
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        AdpGcGameAppInfoList adpGcGameAppInfoList = new AdpGcGameAppInfoList(gameInfoListArrayList);
        adpGcGameAppInfoList.bindToRecyclerView(recyclerView);
        adpGcGameAppInfoList.setEmptyView(R.layout.include_empty_view);

        refreshData();
        if(gameEntity != null){
            loadGameData();
        }
    }

    //加载游戏数据
    private void loadGameData() {
        if (gameEntity.isGift()) {
            gameInfoListArrayList.add(new GameInfoList("礼包推荐", new AdpGcGameAppGiftBag(SheepApp.getInstance(), gameEntity.getHas_gift_bag(), gameEntity)).setSort(SORT_GIFT));
        }
        recyclerView.getAdapter().notifyDataSetChanged();
    }


    private int appId;


    private GameEntity gameEntity;
    public void loadData(final GameEntity gameEntity) {
        this.gameEntity = gameEntity;
        if(recyclerView != null) {
            loadGameData();
        }
    }

    @Override
    public void refreshData() {
        ListUtil.removeItem(gameInfoListArrayList, new ListUtil.CallBack<GameInfoList, Boolean>() {
            @Override
            public Boolean call(GameInfoList gameInfoList) {
                return gameInfoList.getSort() == SORT_ACTIVITY || gameInfoList.getSort() == SORT_ARTICLE;
            }
        });
        recyclerView.getAdapter().notifyDataSetChanged();
        //文章推荐
        SheepApp.getInstance().getNetComponent().getApiService().getDetailArticleRecommend(appId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<Article> newList = baseMessage.getDatas(Article.class);
                        if (!ListUtil.isEmpty(newList)) {
                            gameInfoListArrayList.add(new GameInfoList("文章推荐", new AdpGcGameAppArticleRecommend(ActivityManager.getInstance().currentActivity(), newList)).setSort(SORT_ARTICLE));
                            recyclerView.getAdapter().notifyDataSetChanged();
                        }
                        refreshOrLoadMoreComplete();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if (TestUtil.isDev()) G.showToast(baseMessage);
                        refreshOrLoadMoreComplete();
                    }
                });
        //活动推荐
        SheepApp.getInstance().getNetComponent().getApiService().getDetailActivityRecommend(appId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<Article> newList = baseMessage.getDatas(Article.class);
                        if (!ListUtil.isEmpty(newList)) {
                            gameInfoListArrayList.add(new GameInfoList("活动推荐", new AdpGcGameAppActivityRecommend(ActivityManager.getInstance().currentActivity(), newList)).setSort(SORT_ACTIVITY));
                            recyclerView.getAdapter().notifyDataSetChanged();
                        }
                        refreshOrLoadMoreComplete();
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if (TestUtil.isDev()) G.showToast(baseMessage);
                        refreshOrLoadMoreComplete();
                    }
                });
    }

    @Override
    public void loadMoreData() {
        setNoMore(true);
        refreshOrLoadMoreComplete();
    }

    //礼包推荐
    public static final int SORT_GIFT = 1;
    //文章推荐
    public static final int SORT_ARTICLE = 2;
    //活动推荐
    public static final int SORT_ACTIVITY = 3;
}
