package com.atmob.map_amap_android.overlays.face;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BaseOverlay;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.atmob.map_amap_android.contants.Constants;
import com.atmob.map_amap_android.overlays.MyMethodCallHandler;
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.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class FaceController implements MyMethodCallHandler {


    private final String TAG = "FaceController";

    private final AMap map;

    private final Context context;

    private final HashMap<String, BaseOverlay> currentFaceMap = new HashMap<>(10);

    private final MethodChannel methodChannel;
    private final Gson gson;

    public FaceController(Context context, MethodChannel methodChannel, AMap map) {
        this.context = context;
        this.methodChannel = methodChannel;
        this.map = map;
        gson = GsonUtil.getInstance();
    }

    @Override
    public void doMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        LogUtil.i(TAG, "doMethodCall===>" + call.method + "  arguments===>" + call.arguments);
        switch (call.method) {
            case Constants.METHOD_UPDATE_OR_ADD_CIRCLE:
                updateOrAddCircle(call, result);
                break;
        }
    }

    private void updateOrAddCircle(MethodCall call, MethodChannel.Result result) {
        try {
            Map<String, Object> arguments = call.arguments();
            if (arguments == null) {
                result.error("arguments is null", null, null);
                return;
            }
            String faceId = ParamUtil.getString(arguments, "key");
            String options = ParamUtil.getString(arguments, "circleOptions");
            if (TextUtils.isEmpty(faceId) || TextUtils.isEmpty(options)) {
                result.error("faceId or circleOptions is null", null, null);
                return;
            }
            FlutterCircleOptions flutterCircleOptions = gson.fromJson(options, new TypeToken<FlutterCircleOptions>() {
            }.getType());
            if (flutterCircleOptions == null) {
                result.error("circleOptions parse error", null, null);
                return;
            }
            updateOrAddCircle(faceId, flutterCircleOptions);
            result.success(null);
        } catch (Exception e) {
            LogUtil.d(TAG, "updateOrAddCircle error===>" + e.getMessage());
            result.error("JsonSyntaxException", e.getMessage(), null);
        }
    }

    @Override
    public String[] getRegisterMethodIdArray() {
        return Constants.METHOD_ID_LIST_FOR_FACE;
    }

    @Override
    public void dispose() {

    }


    private void updateOrAddCircle(String faceId, FlutterCircleOptions options) {
        if (map == null) {
            throw new IllegalArgumentException("map is null");
        }
        BaseOverlay overlay = currentFaceMap.get(faceId);
        Circle targetCircle = null;
        if (overlay instanceof Circle) {
            targetCircle = (Circle) overlay;
        }
        if (targetCircle == null) {
            Circle circle = map.addCircle(new CircleOptions()
                    .center(new LatLng(options.getLatitude(), options.getLongitude()))
                    .radius(options.getRadius())
                    .visible(options.isVisible())
                    .fillColor(Color.parseColor(options.getFillColor()))
                    .strokeColor(Color.parseColor(options.getStrokeColor()))
                    .strokeWidth(options.getStrokeWidth()));
            currentFaceMap.put(faceId, circle);
        } else {
            targetCircle.setCenter(new LatLng(options.getLatitude(), options.getLongitude()));
            targetCircle.setRadius(options.getRadius());
            targetCircle.setVisible(options.isVisible());
            targetCircle.setFillColor(Color.parseColor(options.getFillColor()));
            targetCircle.setStrokeColor(Color.parseColor(options.getStrokeColor()));
            targetCircle.setStrokeWidth(options.getStrokeWidth());
        }
    }

}
