package com.yutou.jianr_mg.Fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
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.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Adapters.HomeRecyclerAdapter;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Model.HomeCard;
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 com.yutou.jianr_mg.views.HomeHeader;

import org.json.JSONObject;

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

import cn.lemon.view.RefreshRecyclerView;

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

public class Home extends Fragment {
    private static Home home;

    private RefreshRecyclerView recyclerView;
    private HomeRecyclerAdapter adapter;
    private View view;
    private List<HomeCard> datas;
    private int min=0, max=10;
    private boolean isInit=true;
    public Handler handler;
    private ImageLoader imageLoader;
    private HomeHeader header;

    public static Home init(){
        if(home==null){
            home=new Home();
        }
        return home;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view!=null)
            return view;
        view= inflater.inflate(R.layout.fragment_home,null);
        imageLoader= Utils.initImageLoader(getContext());
        handler=new Handler();
        initView(view);
        return view;
    }


    private void initView(View view){
        Log.i("初始化主页");

        recyclerView=view.findViewById(R.id.recyclerView);
        recyclerView.setSwipeRefreshColors(0xFF437845,0xFFE44F98,0xFF2FAC21);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        adapter=new HomeRecyclerAdapter(getContext());
        adapter.setHeader(HomeHeader.getHome(getActivity()).getView());
        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();
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MG_HOME_LIST, json, new HttpInterface() {
                @Override
                public void httpError(Exception e) {

                }

                @Override
                public void httpGetData(String string, int code) {
                    Log.i(HttpApi.MG_HOME_LIST,string);
                    try{
                        JSONObject json=new JSONObject(string);
                        if(json.getInt("code")==100){
                            datas= JSON.parseArray(json.getString("data"),HomeCard.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);
        }
    }

}
