package com.atmob.map_amap_android.amap;

import android.content.Context;

import androidx.annotation.NonNull;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.atmob.map_amap_android.contants.Constants;
import com.atmob.map_amap_android.overlays.MyMethodCallHandler;
import com.atmob.map_amap_android.util.LogUtil;
import com.atmob.map_amap_android.util.ParamUtil;

import java.util.Map;

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

public class MapController implements MyMethodCallHandler {

    private final String TAG = "MapController";

    private final AMap map;

    private final Context context;

    private final MethodChannel methodChannel;

    public MapController(Context context, MethodChannel methodChannel, AMap map) {
        this.context = context;
        this.methodChannel = methodChannel;
        this.map = map;
    }


    @Override
    public void doMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        switch (call.method) {
            case Constants.METHOD_MOVE_CAMERA:
                moveToCamera(call, result);
                break;
            case Constants.METHOD_ANIMATE_CAMERA:
                animateCamera(call, result);
                break;
                case Constants.METHOD_MAP_CLEAR:
                clearMap(result);
                break;
        }
    }

    private void clearMap(MethodChannel.Result result) {
        LogUtil.i(TAG, "clearMap");
        map.clear();
        result.success(null);
    }

    private void animateCamera(MethodCall call, MethodChannel.Result result) {
        Map<String, Object> arguments = call.arguments();
        Double longitude = ParamUtil.getDouble(arguments, "longitude");
        Double latitude = ParamUtil.getDouble(arguments, "latitude");
        Integer zoom = ParamUtil.getInt(arguments, "zoom");
        if (longitude == null || latitude == null) {
            result.error("error", "longitude or latitude is null", null);
            return;
        }
        if (zoom == null) {
            zoom = 18;
        }
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new com.amap.api.maps.model.LatLng(latitude, longitude), zoom));
        LogUtil.i(TAG, "animateCamera===>success");
        result.success(null);
    }

    private void moveToCamera(MethodCall call, MethodChannel.Result result) {
        LogUtil.i(TAG, "moveToCamera===>" + call.arguments());
        Map<String, Object> arguments = call.arguments();
        Double longitude = ParamUtil.getDouble(arguments, "longitude");
        Double latitude = ParamUtil.getDouble(arguments, "latitude");
        Integer zoom = ParamUtil.getInt(arguments, "zoom");
        if (longitude == null || latitude == null) {
            result.error("error", "longitude or latitude is null", null);
            return;
        }
        if (zoom == null) {
            zoom = 18;
        }
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new com.amap.api.maps.model.LatLng(latitude, longitude), zoom));
        LogUtil.i(TAG, "moveToCamera===>success");
        result.success(null);
    }

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