package com.yutou.jianr_mg.views;

import android.content.Context;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.yutou.jianr_mg.Adapters.MapRecyclierAdapter;
import com.yutou.jianr_mg.Data.MapTop;
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.Tools.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import cn.lemon.view.RefreshRecyclerView;

public class MapView {
    private LinearLayout mainLayout;
    private Context context;

    private Button search;
    private RefreshRecyclerView recyclerView;
    private MapRecyclierAdapter adapter;
    private int limit = 0;
    private Handler handler;

    public MapView(Context context) {
        this.context = context;
        handler=new Handler();
        initView();
        initData(true);
    }

    public ViewGroup getView() {
        return mainLayout;
    }

    private void initView() {
        mainLayout = new LinearLayout(context);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        search = new Button(context);
        search.setText("查询");

        adapter = new MapRecyclierAdapter(context);
        recyclerView = new RefreshRecyclerView(context);
        recyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        mainLayout.addView(search);
        mainLayout.addView(recyclerView);

        recyclerView.addRefreshAction(() -> {
            limit = 0;
            initData(true);
        });
    }


    private void initData(boolean init) {
        try {
            if (init)
                limit = 0;
            JSONObject json = new JSONObject();
            json.put("limit", limit);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MAPTOP_ALL, json, new HttpInterface() {
                @Override
                public void httpGetData(String string, int code) {
                    try {
                        Log.i(string);
                        JSONObject json = new JSONObject(string);
                        JSONArray data = json.getJSONArray("data");
                        List<String> list = new ArrayList<>();
                        for (int i = 0; i < data.length(); i++) {
                            list.add(data.getString(i));
                        }
                        handler.post(()-> {
                            showData(list, false);
                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void httpError(Exception e) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showData(List<String> list, boolean next) {
        if(limit==0){
            adapter.clear();
        }
        adapter.addAll(list);
        adapter.notifyDataSetChanged();
        recyclerView.dismissSwipeRefresh();
        if (!next) {
            recyclerView.showNoMore();
        }
        if (limit == 0)
            recyclerView.getRecyclerView().scrollToPosition(0);


    }
}
