package com.atmob.shortcut;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import io.flutter.Log;
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;
import io.flutter.plugin.common.PluginRegistry;


/**
 * FlutterShortcutPlugin
 */
public class ShortcutPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.NewIntentListener {
    /// 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 MethodChannel channel;
    private Context context;
    private Activity activity;
    private ShortcutUtil shortcutUtil;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        shortcutUtil = new ShortcutUtil(context);
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_shortcut");
        channel.setMethodCallHandler(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("createShortcut")) {
            result.success(shortcutUtil.setShortcutItem(call));
        } else if (call.method.equals("getLaunchAction")) {
            shortcutUtil.getLaunchAction(result, activity);
        } else {
            result.notImplemented();
        }
    }

    @Override
    public boolean onNewIntent(@NonNull Intent intent) {
        String action = shortcutUtil.extraAction(intent);
        if (action != null) {
            Log.d("ShortcutPlugin", "onNewIntent: " + action + " " + channel);
            if (channel != null) channel.invokeMethod("shortcutAction", action);
            return true;
        }
        return false;
    }


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

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();
        binding.addOnNewIntentListener(this);

    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();
    }

    @Override
    public void onDetachedFromActivity() {
        if (activity != null) {
            activity = null;
        }
    }
}
