package com.kfzs.duanduan.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.android.volleyplus.Response;
import com.kfzs.appstore.utils.restful.KFZSNetwork;
import com.kfzs.duanduan.adp.ClassificationGridviewAdapter;
import com.kfzs.duanduan.view.diag.MyGridview;
import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.adp.ClassificationAdapter;
import com.kfzs.duanduan.bll.UrlBll;
import com.kfzs.duanduan.proto.ApiResponseOuterClass;
import com.kfzs.duanduan.proto.GameTypeOuterClass;
import com.kfzs.duanduan.proto.GameTypeSpecialOuterClass;
import com.kfzs.duanduan.utils.dlg.HelperUtils;
import com.kfzs.duanduan.utils.net.APIRequest;
import com.kfzs.duanduan.utils.net.APIRequestInstance;
import com.kfzs.duanduan.BaseCompatFragment;

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


/**
 * 分类
 * Created by Administrator on 2017/12/5.
 */
public class FgtMainCategory extends BaseCompatFragment {

    private final int topViewAddData = 0;
    private final int listViewAddData = 1;
    private ListView mListView;
    private ArrayList<Object> mListData = new ArrayList<>();
    private ClassificationAdapter mAdapter;
    private List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS = new ArrayList<>();//topview data
    private List<GameTypeOuterClass.GameType> listGameT = new ArrayList<>();//listview data

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case topViewAddData:
                    mListData.clear();
                    adapter.toBindTopviewData(listGameTS);
                    adapter.notifyDataSetChanged();
                    break;
                case listViewAddData:
                    mListData.addAll(listGameT);
                    mAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    MyGridview gridview;
    ClassificationGridviewAdapter adapter;
    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentViewChildMode(R.layout.fgt_main_category);

        this.gridview = mContentView.findViewById(R.id.classification_gridview);
        this.adapter = new ClassificationGridviewAdapter( listGameTS, mContext);
        gridview.setAdapter(adapter);

        mListView = mContentView.findViewById(R.id.list_fgt_main_category);
        mAdapter = new ClassificationAdapter(mListData, getActivity());
        mListView.setAdapter(mAdapter);
        loadClassificationListsTop();
    }


    /**
     * 加载分类列表top部分
     */
    private void loadClassificationListsTop() {
        APIRequest bannerRequest = APIRequestInstance.getReq(TAG, UrlBll.AppStoreApi.GAME_TYPES_SPECIAL, null, new Response.Listener<ApiResponseOuterClass.ApiResponse>() {

            @Override
            public void onResponse(ApiResponseOuterClass.ApiResponse apiResponse) {
                int code = apiResponse.getCodeValue();
                switch (code) {
                    case ApiResponseOuterClass.Codes.Success_VALUE:
                        if (apiResponse.getGameTypeSpecialsList() != null && apiResponse.getGameTypeSpecialsList().size() > 0) {
                            listGameTS.clear();
                            listGameTS.addAll(apiResponse.getGameTypeSpecialsList());
//                            listGameTS.add(null);
                            mHandler.sendEmptyMessage(topViewAddData);
                            loadClassificationLists();
                        }
                        break;
                    default:
                        break;
                }
                refreshOver();

            }
        }, HelperUtils.makeErr(getString(R.string.toast_init_data_fail)));
        KFZSNetwork.addRequest(bannerRequest);
    }

    /**
     * 界面列表部分
     */
    private void loadClassificationLists() {
        APIRequest bannerRequest = APIRequestInstance.getReq(TAG, UrlBll.AppStoreApi.GAME_TYPES, null, new Response.Listener<ApiResponseOuterClass.ApiResponse>() {

            @Override
            public void onResponse(ApiResponseOuterClass.ApiResponse apiResponse) {
                int code = apiResponse.getCodeValue();
                switch (code) {
                    case ApiResponseOuterClass.Codes.Success_VALUE:
                        if (apiResponse.getGametypesList() != null && apiResponse.getGametypesList().size() > 0) {
                            listGameT.clear();
                            listGameT.addAll(apiResponse.getGametypesList());
                            mHandler.sendEmptyMessage(listViewAddData);
                        }
                        break;
                    default:
                        break;
                }
                refreshOver();

            }
        }, HelperUtils.makeErr(getString(R.string.toast_init_data_fail)));
        KFZSNetwork.addRequest(bannerRequest);
    }

}
