package com.github.yamill.orientation;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class OrientationModule extends ReactContextBaseJavaModule {
    final private Activity mActivity;

    public OrientationModule(ReactApplicationContext reactContext, final Activity activity) {
        super(reactContext);

        mActivity = activity;

        final ReactApplicationContext ctx = reactContext;

        final BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Configuration newConfig = intent.getParcelableExtra("newConfig");
                Log.d("receiver", String.valueOf(newConfig.orientation));

                String orientationValue = newConfig.orientation == 1 ? "PORTRAIT" : "LANDSCAPE";

                WritableMap params = Arguments.createMap();
                params.putString("orientation", orientationValue);

                ctx
                        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                        .emit("orientationDidChange", params);
            }
        };

        activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));

        LifecycleEventListener listener = new LifecycleEventListener() {
            @Override
            public void onHostResume() {
                activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
            }

            @Override
            public void onHostPause() {
                try
                {
                    activity.unregisterReceiver(receiver);
                }
                catch (java.lang.IllegalArgumentException e) {
                    FLog.e(ReactConstants.TAG, "receiver already unregistered", e);
                }
            }

            @Override
            public void onHostDestroy() {
                try
                {
                    activity.unregisterReceiver(receiver);
                }
                catch (java.lang.IllegalArgumentException e) {
                    FLog.e(ReactConstants.TAG, "receiver already unregistered", e);
                }
            }
        };

        reactContext.addLifecycleEventListener(listener);
    }

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

    @ReactMethod
    public void getOrientation(Callback callback) {
        final int orientation = getReactApplicationContext().getResources().getConfiguration().orientation;

        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            callback.invoke(null, "LANDSCAPE");
        } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            callback.invoke(null, "PORTRAIT");
        } else if (orientation == Configuration.ORIENTATION_UNDEFINED) {
            callback.invoke(null, "UNKNOWN");
        } else {
            callback.invoke(orientation, null);
        }
    }

    @ReactMethod
    public void lockToPortrait() {
      mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @ReactMethod
    public void lockToLandscape() {
      mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @ReactMethod
    public void unlockAllOrientations() {
      mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

    @Override
    public @Nullable Map<String, Object> getConstants() {
      HashMap<String, Object> constants = new HashMap<String, Object>();
      int orientation = getReactApplicationContext().getResources().getConfiguration().orientation;

      if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
          constants.put("initialOrientation", "LANDSCAPE");
      } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
          constants.put("initialOrientation", "PORTRAIT");
      } else if (orientation == Configuration.ORIENTATION_UNDEFINED) {
          constants.put("initialOrientation", "UNKNOWN");
      } else {
          constants.put("initialOrientation", "null");
      }

      return constants;
    }
}

@@ -3269,32 +3269,35 @@
  int orientation
+Int
  = getReactAppli
@@ -3364,28 +3364,31 @@
 n;%0A%0A        
-if (
+String 
 orientation 
@@ -3392,111 +3392,60 @@
 on =
-= Configuration.ORIENTATION_LANDSCAPE) %7B%0A            callback.invoke(null, %22LANDSCAPE%22);%0A 
+ this.getOrientationString(orientationInt);%0A%0A
        
-%7D else
  if 
@@ -3463,49 +3463,20 @@
 n ==
- Configuration.ORIENTATION_PORTRAIT
+= %22null%22
 ) %7B%0A
-  
     
@@ -3501,143 +3501,28 @@
 oke(
-null, %22PORTRAIT%22);%0A        %7D else if (orientation == Configuration.ORIENTATION_UNDEFINED) %7B%0A            callback.invoke(null, %22UNKNOWN%22
+orientationInt, null
 );%0A 
@@ -3539,34 +3539,32 @@
 lse %7B%0A          
-  
 callback.invoke(
@@ -3555,32 +3555,38 @@
 callback.invoke(
+null, 
 orientation, nul
@@ -3580,22 +3580,16 @@
 entation
-, null
 );%0A     
@@ -4204,16 +4204,19 @@
 entation
+Int
  = getRe
@@ -4283,24 +4283,356 @@
 ientation;%0A%0A
+      String orientation = this.getOrientationString(orientationInt);%0A      if (orientation === %22null%22) %7B%0A        constants.put(%22initialOrientation%22, null);%0A      %7D else %7B%0A        constants.put(%22initialOrientation%22, orientation);%0A      %7D%0A%0A      return constants;%0A    %7D%0A%0A    private String getOrientationString(int orientationInt) %7B%0A
       if (or
@@ -4697,43 +4697,14 @@
     
-constants.put(%22initialOrientation%22,
+return
  %22LA
@@ -4711,17 +4711,16 @@
 NDSCAPE%22
-)
 ;%0A      
@@ -4797,43 +4797,14 @@
     
-constants.put(%22initialOrientation%22,
+return
  %22PO
@@ -4810,17 +4810,16 @@
 ORTRAIT%22
-)
 ;%0A      
@@ -4897,43 +4897,14 @@
     
-constants.put(%22initialOrientation%22,
+return
  %22UN
@@ -4909,17 +4909,16 @@
 UNKNOWN%22
-)
 ;%0A      
@@ -4940,51 +4940,21 @@
     
-constants.put(%22initialOrientation%22,
+return
  %22null%22
-)
 ;%0A  
@@ -4962,33 +4962,8 @@
    %7D
-%0A%0A      return constants;
 %0A   

Add func to get orientationString