package com.attehuhtakangas.navigationbar;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.view.Window;
import android.graphics.Color;
import android.view.WindowManager;

import com.facebook.react.bridge.NativeModule;
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 java.util.HashMap;
import java.util.Map;

public class NavigationBarModule extends ReactContextBaseJavaModule {

    private static final String DEFAULT_COLOR = "#000000";
    private static String mCurrentColor;
    private Activity mActivity;

    public NavigationBarModule(ReactApplicationContext reactContext, Activity activity) {
        super(reactContext);
        this.mActivity = activity;
        this.mCurrentColor = DEFAULT_COLOR;
    }

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

    @Override
    public Map<String, Object> getConstants() {
        final Map<String, Object> constants = new HashMap<>();
        constants.put(DEFAULT_COLOR, Color.BLACK);
        return constants;
    }

    @ReactMethod
    public void setColor(String color) {
        this.setTranslucent(false);
        final int colorInt = Color.parseColor(color);
        if (getReactApplicationContext().hasCurrentActivity()) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        mActivity.getWindow().setNavigationBarColor(colorInt);
                    }
                }
            });
        }
    }

    @ReactMethod
    public void setTranslucent(final boolean val) {
        if (getReactApplicationContext().hasCurrentActivity()) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        if(val) {
                            mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                        } else {
                            mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                        }
                    }
                }
            });
        }
    }

    @ReactMethod
    public int getHeight() {
        // Resources resources = getReactApplicationContext().getResources();
        Resources resources = mActivity.getResources();
        int orientationInt = resources.getConfiguration().orientation;
        /* if (orientation.equalsIgnoreCase("portrait")) {
            orientationInt = Configuration.ORIENTATION_PORTRAIT;
        } else if(orientation.equalsIgnoreCase("landscape")) {
            orientationInt = Configuration.ORIENTATION_PORTRAIT;
        } else {
            return 0;
        }*/

        int id = resources.getIdentifier(orientationInt == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");

        if (id > 0) {
            // int pixelSize = resources.getDimensionPixelSize(id);
            int dp = (int) (resources.getDimension(id) / resources.getDisplayMetrics().density);
            return dp;
        } else {
            return 0;
        }


    }

    @ReactMethod
    public String getCurrentColor() {
      return this.mCurrentColor;
    }

    @ReactMethod
    public void resetToDefault() {
        this.setTranslucent(false);
        this.setColor(DEFAULT_COLOR);
    }
}

@@ -165,44 +165,22 @@
 oid.
-os.Build;%0Aimport android.view.Window
+graphics.Color
 ;%0Aim
@@ -188,38 +188,32 @@
 ort android.
-graphics.Color
+os.Build
 ;%0Aimport and
@@ -242,55 +242,8 @@
 r;%0A%0A
-import com.facebook.react.bridge.NativeModule;%0A
 impo
@@ -300,55 +300,8 @@
 xt;%0A
-import com.facebook.react.bridge.ReactContext;%0A
 impo
@@ -618,24 +618,89 @@
 CurrentColor
+ = DEFAULT_COLOR;%0A    private static boolean mTranslucent = false
 ;%0A    privat
@@ -880,52 +880,8 @@
 ty;%0A
-        this.mCurrentColor = DEFAULT_COLOR;%0A
     
@@ -1167,32 +1167,196 @@
 nstants;%0A    %7D%0A%0A
+    // TODO: create color parser class, currently this only accepts colors in full hex format eg. %22#BADA55%22%0A    // React Native understands rgb, rgba, %22#fff%22, etc.%0A
     @ReactMethod
@@ -1401,44 +1401,8 @@
 ) %7B%0A
-        this.setTranslucent(false);%0A
     
@@ -1451,16 +1451,17 @@
 color);%0A
+%0A
         
@@ -1509,32 +1509,169 @@
 ntActivity()) %7B%0A
+            if (android.os.Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.LOLLIPOP) %7B%0A                mCurrentColor = color;%0A            %7D%0A
             mAct
@@ -2378,17 +2378,38 @@
       if
-(
+ (!isTranslucent() && 
 val) %7B%0A 
@@ -2595,38 +2595,116 @@
                 
-%7D else
+    mTranslucent = true;%0A                        %7D else if (isTranslucent() && !val)
  %7B%0A             
@@ -2800,32 +2800,82 @@
 NT_NAVIGATION);%0A
+                            mTranslucent = false;%0A
                 
@@ -4016,24 +4016,26 @@
 r() %7B%0A      
+  
 return this.
@@ -4048,32 +4048,127 @@
 ntColor;%0A    %7D%0A%0A
+    @ReactMethod%0A    public boolean isTranslucent() %7B%0A        return this.mTranslucent;%0A    %7D%0A%0A
     @ReactMethod

Implement method to check if is translucent