package com.sheep.gamegroup.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.GameEntityList;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.view.adapter.PlayGameListAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by ljy on 2018/9/3.
 */

public class FgtPlayGameSon extends BaseListFragment2<GameEntity> {
    private int type;

    public static FgtPlayGameSon newInstance(int type){
        FgtPlayGameSon fgt = new FgtPlayGameSon();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fgt.setArguments(bundle);
        return fgt;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        Bundle bundle = getArguments();
        if(bundle != null){
            type = bundle.getInt("type", 1002);
        }
        setPerpage(10);
        return rootView;
    }

    @Override
    public void setNoMore(boolean noMore) {
        super.setNoMore(noMore);
        EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.FGT_PLAY_GAME_SET_BOTTOM_LINE));
    }

    @Override
    public Action1<String> getNoMoreCallBack() {
        return new Action1<String>() {
            @Override
            public void call(String msg) {
                if(isSelected)
                    EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.FGT_PLAY_GAME_FOOT_VIEW_CALL_BACK).setData(msg));
            }
        };
    }

    private GameEntityList gameEntityList = new GameEntityList(list);
    private List<GameEntityList> gameEntityListList = ListUtil.emptyList();
    @Override
    protected RecyclerView.Adapter getAdapter() {
        if(gameEntityListList.isEmpty())
            gameEntityListList.add(gameEntityList);
        return new PlayGameListAdapter(activity, gameEntityListList);
    }

    @Override
    protected String getKey(int page, int per_page) {
        return String.format(Locale.CHINA, "app/find?page=%d&per_page=%d&is_hot=%d&daily_play=%d&type=%d", page, per_page, 0, 0, type);
    }

    @Override
    protected Observable<BaseMessage> getApi(ApiService apiService) {
        return apiService.playgameDailyOrHotOrTypePlay(page, per_page, 0, 0, type);
    }

    @Override
    protected Class<GameEntity> getTClass() {
        return GameEntity.class;
    }

    @Override
    public void notifyData() {
//        bottomLine.setVisibility(View.GONE);
        if(isSelected)
            EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.FGT_PLAY_GAME_REFRESH_H));
    }

    @Subscribe
    public void onEventMainThread(BigEvent event){
        CommonUtil.getInstance().setEventResultViewStatus(event, view_list);
    }
    private boolean isSelected = false;

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    @Override
    protected boolean isFirstGetACache() {
        return false;
    }
}
