package com.example.nexilissamplecodeflutter;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.nexilis.service.API;
import io.nexilis.service.Callback;
import io.nexilis.service.utils.callback.SimCardDetectionCallback;
import io.nexilis.service.utils.xthunder.FloatingButton;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "nativeChannel";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         *
         * Nexilis provides an API security shield service, which functions to detect threats such as malware, ADB, screencapture, and Sim Swapping.
         */

        //SECURITY

        //SMS
        //EMAIL
        //SMARTBOT

        /*************************************
         Connect to our server with your newuniverse.io Account, and implement the required Callback.
         Please Subscribe or contact us to get your newuniverse.io Account.
         Do not share your newuniverse.io Account or ever give it out to someone outside your organization.
         ************************************/
        /**
         * API.connect (String NexilisAccount, Activity RegisteredActivity, int NexilisButtonMode, boolean UserMayModifyUID, Callback ConnectCallback)
         *
         * NexilisAccount 		: Your Nexilis.io Account.
         * RegisteredActivity 	: Android's Activity class that is used to register the Nexilis Button
         * NexilisButtonMode 	: The flag that determines when the Nexilis Button should appear.
         *      0 = Disabled Nexilis Button
         * 		1 = Within registered Activity, (Nexilis Button only appears when users are in the registered activity)
         * 		2 = Within App (Nexilis Button always appears as long as user is in the App),
         * 		3 = Always On (Nexilis Button always appears even if the application process is closed)
         * UserMayModifyUID 	: Sets whether users are allowed to change the Nexilis UserID.
         * 		true = enabled,
         * 		false = disabled
         * ConnectCallback	: The callback interface to be invoked when calling the method connect.
         * 		You need to implement onSuccess(String NexilisUserID) & onFailed(String reasonCode) to handle the RESULT.
         *
         */
        API.connect("***REPLACE***WITH***YOUR***NEXILIS***ACCOUNT***", this, 0, new Callback() {
            @Override
            public void onSuccess(final String NexilisUserID) {
                /**************************************
                 The NexilisUserID parameter is generated automatically and can be mapped to a User ID on the application level.
                 For example, the Nexilis User ID (e.g. User001) can be mapped into the corresponding Application User ID (e.g. John Doe),
                 so you don't have to share your Application User ID with Nexilis while still being able to monitor your user activities.
                 **************************************/
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //FLOATING
                        Toast.makeText(getBaseContext(), "Your User ID: " + NexilisUserID + " : " + getString(io.nexilis.service.R.string.pb_loading), Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onFailed(final String reasonCode) {
                /**
                 * reasonCode 	: Returns a code based on the status of the function connect called.
                 * 		2:Your trial subscription has expired. Please subscribe to continue using Nexilis.
                 * 		3:Your monthly subscription is not paid in full. Please pay your monthly subscription bill to continue using Nexilis service.
                 * 		4:Your Customer Engagement Credit has run out and your Prepaid Credit Balance is empty. Please top-up your Prepaid Credit Balance to continue using Nexilis
                 *		23:Unsupported Android version
                 * 		93:Missing the required overlay permission
                 * 		94:Unregistered User
                 * 		95:Invalid Nexilis Button Mode (1,2,3)
                 * 		96:Activity is null
                 * 		97:Account is empty
                 * 		98:Your account didn't match
                 * 		99:Something went wrong
                 */
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(), reasonCode, Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler((call, result) -> {
            if (call.method.equals("openContactCenter")) {
                String response = API.openContactCenter();
                result.success(response);
            } else if (call.method.equals("openInstantMessaging")) {
                String response = API.openChat();
                result.success(response);
            } else if (call.method.equals("openCall")) {
                String response = API.openCall();
                result.success(response);
            } else if (call.method.equals("openStreaming")) {
                String response = API.openLiveStreaming();
                result.success(response);
            } else if (call.method.equals("openSettings")) {
                API.openSettings();
            } else if (call.method.equals("openWhiteboard")) {
                String response = API.openWhiteboard();
                result.success(response);
            } else if (call.method.equals("openChatGPT")) {
                String response = API.openSmartChatbot();
                result.success(response);
            } else if (call.method.equals("openContactCenterChat")) {
                Map<String, Integer> map = (Map<String, Integer>) call.arguments;
                int category = map.get("category");
                String response = API.openContactCenter(0, category);
                result.success(response);
            } else if (call.method.equals("openContactCenterAudioCall")) {
                Map<String, Integer> map = (Map<String, Integer>) call.arguments;
                int category = map.get("category");
                String response = API.openContactCenter(1, category);
                result.success(response);
            } else if (call.method.equals("openContactCenterVideoCall")) {
                Map<String, Integer> map = (Map<String, Integer>) call.arguments;
                int category = map.get("category");
                String response = API.openContactCenter(2, category);
                result.success(response);
            } else if (call.method.equals("signInAdmin")) {
                Map<String, String> map = (Map<String, String>) call.arguments;
            	String pwd = map.get("pwd");
                String response = API.signInAdmin(pwd);
                result.success(response);
            } else if (call.method.equals("openSetOfficer")) {
                String response = API.openSetAsOfficerForm();
                result.success(response);
            } else if (call.method.equals("openSecureFolder")) {
                String response = API.openSecureFolder();
                result.success(response);
            }
        });
    }
}
