package com.atmob.channel.atmob_channel_reader;

import android.content.Context;

import androidx.annotation.NonNull;

import com.atmob.channelreader.ChannelReader;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
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;

/**
 * AtmobChannelReaderPlugin
 */
public class AtmobChannelReaderPlugin implements FlutterPlugin, MethodCallHandler {
    private static final String METHOD_DEFAULT_4_TEST = "default4Test";
    private static final String METHOD_GET_CHANNEL = "getChannel";
    private static final String METHOD_GET_APP_ID = "getAppId";
    private static final String METHOD_GET_TG_PLATFORM_ID = "getTgPlatformId";

    private static final String ARG_CHANNEL = "channel";
    private static final String ARG_APP_ID = "appId";
    private static final String ARG_TG_PLATFORM_ID = "tgPlatformId";


    /// 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 applicationContext;

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

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        switch (call.method) {
            case METHOD_DEFAULT_4_TEST:
                default4Test(call, result);
                break;
            case METHOD_GET_CHANNEL:
                getChannel(call, result);
                break;
            case METHOD_GET_APP_ID:
                getAtmobAppId(call, result);
                break;
            case METHOD_GET_TG_PLATFORM_ID:
                getTgPlatformId(call, result);
                break;
            default:
                result.notImplemented();
                break;
        }
    }

    private void getTgPlatformId(MethodCall call, Result result) {
        result.success(ChannelReader.getTgPlatformId(applicationContext));
    }

    private void getAtmobAppId(MethodCall call, Result result) {
        result.success(ChannelReader.getAppId(applicationContext));
    }

    private void getChannel(MethodCall call, Result result) {
        result.success(ChannelReader.getChannel(applicationContext));
    }

    private void default4Test(MethodCall call, Result result) {
        String channel = call.argument(ARG_CHANNEL);
        Integer appId = call.argument(ARG_APP_ID);
        Integer tgPlatformId = call.argument(ARG_TG_PLATFORM_ID);
        if (channel == null || appId == null || tgPlatformId == null) {
            result.error("400", "Invalid arguments", null);
            return;
        }
        ChannelReader.default4Test(channel, appId, tgPlatformId);
        result.success(null);
    }

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