package com.robinpowered.RNMDMManager;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;

// For MDM
import android.app.Activity;
import android.content.RestrictionsManager;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.WritableNativeMap;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Nullable;

public class RNMobileDeviceManagerModule extends ReactContextBaseJavaModule {
    public static final String MODULE_NAME = "MobileDeviceManager";
    public static final String APP_CONFIG_CHANGED = "react-native-mdm/managedAppConfigDidChange";

    public RNMobileDeviceManagerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        final ReactApplicationContext thisContext = reactContext;

        final RestrictionsManager restrictionsManager = (RestrictionsManager) reactContext.getSystemService(Context.RESTRICTIONS_SERVICE);

        IntentFilter restrictionFilter = new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
        BroadcastReceiver restrictionReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle appRestrictions = restrictionsManager.getApplicationRestrictions();
                WritableNativeMap data = new WritableNativeMap();
                for (String key : appRestrictions.keySet()){
                    data.putString(key, appRestrictions.getString(key));
                }
                if (thisContext.hasActiveCatalystInstance()) {
                    thisContext
                            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                            .emit(APP_CONFIG_CHANGED, data);
                }
            }
        };
        thisContext.registerReceiver(restrictionReceiver,restrictionFilter);
    }

    public boolean enableLockState() {
        if (!isLockState()) {
            Activity activity = getCurrentActivity();
            if (activity == null) {
                return false;
            }
            activity.startLockTask();
            return true;
        }

        return false;
    }

    public boolean disableLockState() {
        if (isLockState()) {
            try {
                Activity activity = getCurrentActivity();
                if (activity == null) {
                    return false;
                }
                activity.stopLockTask();
                return true;
            } catch (SecurityException e) {
                return false;
            }
        }
        return false;
    }

    public boolean isLockStatePermitted() {
        DevicePolicyManager dpm = (DevicePolicyManager)
                getReactApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
        return dpm.isLockTaskPermitted(getReactApplicationContext().getPackageName());
    }

    public boolean isLockState() {
        boolean isLocked = false;
        ActivityManager am = (ActivityManager) getReactApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        try {
            if (am.getLockTaskModeState() != ActivityManager.LOCK_TASK_MODE_NONE) {
                isLocked = true;
            }
        } catch (Exception e) {

        }
        return isLocked;
    }

    @Override
    public String getName() {
        return MODULE_NAME;
    }

    @Override
    public @Nullable Map<String, Object> getConstants() {
        HashMap<String, Object> constants = new HashMap<String, Object>();
        constants.put("APP_CONFIG_CHANGED", APP_CONFIG_CHANGED);
        return constants;
    }

    private boolean isMDMSupported() {
        RestrictionsManager restrictionsManager = (RestrictionsManager) super.getReactApplicationContext().getSystemService(Context.RESTRICTIONS_SERVICE);
        return restrictionsManager.getApplicationRestrictions().size() > 0;
    }

    @ReactMethod
    public void isSupported(final Promise promise) {
        promise.resolve(isMDMSupported());
    }

    @ReactMethod
    public void getConfiguration(final Promise promise) {
        if (isMDMSupported()) {
            RestrictionsManager restrictionsManager = (RestrictionsManager) super.getReactApplicationContext().getSystemService(Context.RESTRICTIONS_SERVICE);
            Bundle appRestrictions = restrictionsManager.getApplicationRestrictions();
            WritableNativeMap data = new WritableNativeMap();
            for (String key : appRestrictions.keySet()){
                data.putString(key, appRestrictions.getString(key));
            }
            promise.resolve(data);
        } else {
            promise.reject(new Error("Managed App Config is not supported"));
        }
    }

    @ReactMethod
    public void isAutonomousSingleAppModeSupported(final Promise promise) {
        promise.resolve(isLockStatePermitted());
    }

    @ReactMethod
    public void isAutonomousSingleAppModeEnabled(final Promise promise) {
        promise.resolve(isLockState());
    }

    @ReactMethod
    public void enableAutonomousSingleAppMode(final Promise promise) {
        promise.resolve(enableLockState());
    }

    @ReactMethod
    public void disableAutonomousSingleAppMode(final Promise promise) {
        promise.resolve(disableLockState());
    }
}

@@ -429,24 +429,49 @@
 .os.Bundle;%0A
+import android.os.Build;%0A
 import andro
@@ -2590,25 +2590,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -3540,32 +3540,102 @@
 ;%0A        try %7B%0A
+            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A    
             if (
@@ -3718,16 +3718,20 @@
         
+    
 isLocked
@@ -3735,24 +3735,163 @@
 ked = true;%0A
+                %7D%0A            %7D else %7B%0A                if (am.isInLockTaskMode()) %7B%0A                    isLocked = true;%0A                %7D%0A
             
@@ -3909,26 +3909,16 @@
  catch (
-Exception 
 e) %7B%0A%0A  

support android 5 and below