package com.yutou.jianr_mg.Fragments;

import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.appcompat.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yutou.jianr_mg.Adapters.ModListRecyclerAdapter;
import com.yutou.jianr_mg.Data.MAppMg;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;
import com.yutou.jianr_mg.Tools.Utils;

import org.json.JSONObject;

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

import cn.lemon.view.RefreshRecyclerView;

/**
 * Created by 58381 on 2018/1/23.
 */

public class MGList extends Fragment {
    private static MGList modView;
    private RefreshRecyclerView recyclerView;
    private ModListRecyclerAdapter adapter;
    private List<MAppMg> datas;
    private int min=0, max=10;
    private boolean isInit=true;
    private Handler handler;
    private View view;
    public static MGList init(){
        if(modView==null){
            modView=new MGList();
        }
        return modView;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view!=null)
            return view;
        view=inflater.inflate(R.layout.fragment_mg,null);
        handler=new Handler();
        initView(view);
        return view;
    }
    private void initView(View view){
        recyclerView=view.findViewById(R.id.recyclerView);
        recyclerView.setSwipeRefreshColors(0xFF437845,0xFFE44F98,0xFF2FAC21);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        adapter=new ModListRecyclerAdapter(getContext());
        TextView endText=new TextView(getContext());
        endText.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.getPixelsFromDp(getActivity(),48)));
        endText.setTextSize(16);
        endText.setGravity(Gravity.CENTER);
        endText.setText(" --已经没有了--");
        adapter.setFooter(endText);
        recyclerView.setAdapter(adapter);
        recyclerView.addRefreshAction(() -> {
            min=0;
            max=10;
            isInit=true;
            initData();
        });
        recyclerView.setLoadMoreAction(() -> {
            if(isInit){
                recyclerView.showNoMore();
            }else {
                min = max;
                max += 10;
                initData();
            }
        });
        recyclerView.showSwipeRefresh();
        initData();
    }
    private void initData(){
        try {
            datas=new ArrayList<>();
            Log.i("载入数据");
            JSONObject json=new JSONObject();
            json.put("min",min);
            json.put("max",max);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MG_MG_LIST, json, new HttpInterface() {
                @Override
                public void httpError(Exception e) {

                }

                @Override
                public void httpGetData(String string, int code) {
                    Log.i(HttpApi.MG_MG_LIST,string);
                    try{
                        JSONObject json=new JSONObject(string);
                        if(json.getInt("code")==100){
                            datas= JSON.parseArray(json.getString("data"), MAppMg.class);
                        }
                        handler.post(() -> setData());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void setData(){
        if(datas.size()>0){
            adapter.clear();
            adapter.addAll(datas);
            recyclerView.dismissSwipeRefresh();
            recyclerView.getRecyclerView().scrollToPosition(0);
        }else{
            adapter.addAll(datas);
        }
    }
}
