package com.atmob.map_amap_android;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;

import com.atmob.map_amap_android.contants.Constants;
import com.atmob.map_amap_android.lifecycle.FlutterLifecycleAdapter;
import com.atmob.map_amap_android.trace.TraceClientHelper;
import com.atmob.map_amap_android.util.AMapHelper;
import com.atmob.map_amap_android.util.LogUtil;

import java.util.Objects;

import io.flutter.BuildConfig;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * MapAmapAndroidPlugin
 */
public class MapAmapAndroidPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private final String TAG = "MapAmapAndroidPlugin";
    private MethodChannel channel;
    private Activity mActivity;
    private Context applicationContext;
    private FlutterPluginBinding flutterPluginBinding;
    private Lifecycle lifecycle;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        LogUtil.setLogEnabled(BuildConfig.DEBUG);
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), Constants.MAP_METHOD_CHANNEL);
        applicationContext = flutterPluginBinding.getApplicationContext();
        channel.setMethodCallHandler(this);
        this.flutterPluginBinding = flutterPluginBinding;
        FlutterLocationEventPlugin flutterLocationEventPlugin = new FlutterLocationEventPlugin();
        flutterLocationEventPlugin.onAttachedToEngine(flutterPluginBinding);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        try {
            String method = call.method;
            LogUtil.d(TAG, "onMethodCall: " + method + " " + call.arguments);
            if (Objects.equals(method, Constants.METHOD_INIT)) {
                AMapHelper.init(applicationContext);
                result.success(true);
            } else if (Objects.equals(method, Constants.METHOD_GET_PLATFORM_MAP_NAME)) {
                result.success(Constants.MAP_NAME);
            } else if (Objects.equals(method, Constants.METHOD_START_LOCATION)) {
                AMapHelper.startLocation(applicationContext);
                result.success(true);
            } else if (Objects.equals(method, Constants.METHOD_QUERY_PROCESSED_TRACE)) {
                TraceClientHelper.queryProcessedTrace(applicationContext, call, result);
            } else {
                result.notImplemented();
            }
        } catch (Exception e) {
            result.error("error", e.getMessage(), null);
        }
    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) {
        mActivity = activityPluginBinding.getActivity();
        lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityPluginBinding);
        FlutterViewPlugin.registerWith(flutterPluginBinding, mActivity, () -> lifecycle);
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        this.onDetachedFromActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding activityPluginBinding) {
        onAttachedToActivity(activityPluginBinding);
    }

    @Override
    public void onDetachedFromActivity() {
        mActivity = null;
        lifecycle = null;
    }
}
