package com.workspaceonesdk;

import android.util.Log;

import com.airwatch.sdk.AirWatchSDKException;
import com.airwatch.tunnelsdk.util.Logger;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.vmware.ws1androidsdkbridge.WorkspaceOneSdkBridge;
import com.vmware.ws1androidsdkbridge.InitializationListener;

public class WorkspaceOneSdkModule extends ReactContextBaseJavaModule {

    private static final String E_MODULE_ERROR = "E_WS1_MODULE_ERROR";
    private final ReactApplicationContext reactContext;

    public WorkspaceOneSdkModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "WorkspaceOneSdk";
    }

    public static final String LOG_TAG = WorkspaceOneSdkModule.class.getSimpleName();

   //private CallbackContext persistedContext;
    private WorkspaceOneSdkBridge bridge;

    @Override
    public void initialize() {
       bridge = WorkspaceOneSdkBridge.Companion.getInstance();
    }


    private InitializationListener initListener = new InitializationListener() {
        @Override
        public void initSuccess() {
            sendEvent("initSuccess");
        }

        @Override
        public void initFailure() {
            sendEvent("initFailure");
        }
    };



    @ReactMethod
    public void startSDK(){
        Log.d(LOG_TAG,"Inside startSDK");
        bridge = WorkspaceOneSdkBridge.Companion.getInstance();
        bridge.initWorkspaceOneSDKManager(getCurrentActivity(),initListener);
    }

    public void sendEvent(String eventName ) {
        sendEvent(reactContext, eventName, null);
    }

    public void sendEvent(String eventName, WritableMap params)
    {
        sendEvent(reactContext, eventName, params);
    }

    private void sendEvent(ReactContext reactContext, String eventName, WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);

    }

    @ReactMethod
    public void groupId( Promise promise)  {
        try
        {
            String groupId = bridge.getGroupId();
            promise.resolve(groupId);
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }

    }
    @ReactMethod
    public void userName( Promise promise)  {
        try
        {
            String userName = bridge.getUserName();
            promise.resolve(userName);
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }

    }

    @ReactMethod
    public void serverName( Promise promise)  {
        try
        {
            String serverName = bridge.getServerName();
            promise.resolve(serverName);
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }

    }

    @ReactMethod
    public void allowCopyPaste( Promise promise)  {
        try
        {
            Boolean allowCopyPaste = bridge.getAllowCopyPaste();
            promise.resolve(allowCopyPaste);
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void allowOffline( Promise promise)  {
        try
        {
            promise.resolve(bridge.isOfflineAllowed());
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void restrictDocumentToApps( Promise promise)  {
        try
        {
            promise.resolve(bridge.getRestrictDocumentToApps());
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void allowedApplications( Callback senderCallback)  {
        try
        {
            String[] apps = bridge.allowedApplications().toArray(new String[0]);
            senderCallback.invoke(apps);
        } catch (AirWatchSDKException exception)
        {
            Log.e(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void customSettings( Promise promise)  {
        try
        {
            promise.resolve(bridge.getCustomSettings());
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void isCompliant( Promise promise)  {
        try
        {
            promise.resolve(bridge.isCompliant());
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void isCompromised(Callback senderCallback)  {
        try
        {
            senderCallback.invoke(bridge.isCompromised());
        } catch (AirWatchSDKException exception)
        {
            Log.e(E_MODULE_ERROR,"Exception",exception);
        }
    }

    @ReactMethod
    public void isEnrolled(Callback senderCallback, Callback errorCallback)  {
        try
        {
            senderCallback.invoke(bridge.isEnrolled());
        } catch (AirWatchSDKException exception)
        {
            errorCallback.invoke(exception);
            Log.e(E_MODULE_ERROR,"Exception",exception);
        }
    }


    @ReactMethod
    public void registerPushNotificationToken(String token, Promise promise)  {
        try
        {
            bridge.registerPushNotificationToken(token);
            promise.resolve(null);
        } catch (AirWatchSDKException exception)
        {
            promise.reject(E_MODULE_ERROR,"Exception",exception);
        }
    }




}
