package io.github.v7lin.alipay_kit;

import androidx.annotation.NonNull;

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.PluginRegistry.Registrar;

/**
 * AlipayKitPlugin
 */
public class AlipayKitPlugin implements FlutterPlugin, ActivityAware {

    // --- FlutterPlugin

    private final AlipayKit alipayKit;

    public AlipayKitPlugin() {
        alipayKit = new AlipayKit();
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        alipayKit.setApplicationContext(binding.getApplicationContext());
        alipayKit.setActivity(null);
        alipayKit.startListening(binding.getBinaryMessenger());
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        alipayKit.stopListening();
        alipayKit.setActivity(null);
        alipayKit.setApplicationContext(null);
    }

    // --- ActivityAware

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        alipayKit.setActivity(binding.getActivity());
    }

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

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

    @Override
    public void onDetachedFromActivity() {
        alipayKit.setActivity(null);
    }
}
