package com.atmob.map_amap_android.trace;

import android.content.Context;

import androidx.annotation.NonNull;

import com.amap.api.maps.model.LatLng;
import com.amap.api.trace.LBSTraceClient;
import com.amap.api.trace.TraceListener;
import com.amap.api.trace.TraceLocation;
import com.atmob.map_amap_android.bean.FTraceLocation;
import com.atmob.map_amap_android.util.GsonUtil;
import com.atmob.map_amap_android.util.LogUtil;
import com.atmob.map_amap_android.util.ParamUtil;
import com.google.gson.reflect.TypeToken;

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

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class TraceClientHelper {

    private static final String TAG = "TraceClientHelper";

    private TraceClientHelper() {

    }

    public static void queryProcessedTrace(Context context, MethodCall call, MethodChannel.Result result) {
        try {
            Map<String, Object> arguments = call.arguments();
            if (arguments == null) {
                result.error("params is empty", "params is empty", null);
                return;
            }
            Integer lineID = ParamUtil.getInt(arguments, "lineID");
            if (lineID == null) {
                result.error("lineID is null", "lineID is null", null);
                return;
            }
            String locations = (String) arguments.get("locations");
            if (locations == null || locations.isEmpty()) {
                result.error("locations is empty", "原始数据为空", null);
                return;
            }
            LBSTraceClient lbsTraceClient;
            try {
                lbsTraceClient = LBSTraceClient.getInstance(context);
            } catch (Exception e) {
                result.error("lbsTraceClient init error", e.getMessage(), null);
                return;
            }
            List<FTraceLocation> originalList = GsonUtil.getInstance().fromJson(locations, new TypeToken<List<FTraceLocation>>() {
            }.getType());
            List<TraceLocation> locationList = getTraceLocations(originalList);
            List<LatLng> queryResultList = new ArrayList<>();
            lbsTraceClient.queryProcessedTrace(lineID, locationList, LBSTraceClient.TYPE_AMAP, new TraceListener() {
                @Override
                public void onRequestFailed(int lineID, String s) {
                    LogUtil.e(TAG, "onRequestFailed: lineID = " + lineID + ",message: " + s);
                    if (queryResultList.size() > originalList.size() / 2) {
                        LogUtil.d(TAG, "onRequestFailed: call success queryResultList.size:" + queryResultList.size() + " originalList.size:" + originalList.size());
                        result.success(GsonUtil.getInstance().toJson(queryResultList));
                        return;
                    }
                    LogUtil.e(TAG, "onRequestFailed: call error ");
                    result.error("queryProcessedTrace error lineID:" + lineID, s, null);
                }

                @Override
                public void onTraceProcessing(int lineID, int index, List<LatLng> list) {
                    LogUtil.d(TAG, "onTraceProcessing:" + list.size());
                    queryResultList.addAll(list);
                }

                @Override
                public void onFinished(int i, List<LatLng> list, int i1, int i2) {
                    LogUtil.d(TAG, "onFinished:" + list.size());
                    result.success(GsonUtil.getInstance().toJson(queryResultList));
                }
            });

        } catch (Exception e) {
            LogUtil.d(TAG, "error:" + e.getMessage());
            result.error("error", e.getMessage(), null);
        }
    }

    private static @NonNull List<TraceLocation> getTraceLocations(List<FTraceLocation> originalList) {
        List<TraceLocation> locationList = new ArrayList<>();
        for (FTraceLocation fTraceLocation : originalList) {
            TraceLocation traceLocation = new TraceLocation();
            traceLocation.setLatitude(fTraceLocation.getLatitude());
            traceLocation.setLongitude(fTraceLocation.getLongitude());
            traceLocation.setSpeed(fTraceLocation.getSpeed());
            traceLocation.setBearing(fTraceLocation.getBearing());
            traceLocation.setTime(fTraceLocation.getTime());
            locationList.add(traceLocation);
        }
        return locationList;
    }
}
