package org.commcare.activities.components;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.widget.TextViewCompat;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.commcare.activities.CommCareActivity;
import org.commcare.activities.FormEntryActivity;
import org.commcare.dalvik.R;
import org.commcare.models.ODKStorage;
import org.commcare.preferences.FormEntryPreferences;

/**
 * @author ctsims
 */
public class FormLayoutHelpers {
    public static boolean determineNumberOfValidGroupLines(FormEntryActivity activity,
                                                           Rect newRootViewDimensions,
                                                           boolean groupNativeVisibility,
                                                           boolean groupForcedInvisible) {
        FrameLayout header = (FrameLayout)activity.findViewById(R.id.form_entry_header);
        TextView groupLabel = ((TextView)header.findViewById(R.id.form_entry_group_label));

        int numberOfGroupLinesAllowed =
                getNumberOfGroupLinesAllowed(groupLabel, newRootViewDimensions, activity);

        if (TextViewCompat.getMaxLines(groupLabel) == numberOfGroupLinesAllowed) {
            return groupForcedInvisible;
        }

        groupLabel.setMaxLines(numberOfGroupLinesAllowed);
        if (numberOfGroupLinesAllowed == 0) {
            updateGroupViewVisibility(header, true, groupNativeVisibility);
            return true;
        } else {
            updateGroupViewVisibility(header, false, groupNativeVisibility);
            return false;
        }
    }

    private static int getNumberOfGroupLinesAllowed(TextView groupLabel,
                                                    Rect newRootViewDimensions,
                                                    FormEntryActivity activity) {
        int contentSize = newRootViewDimensions.height();

        View navBar = activity.findViewById(R.id.nav_pane);
        int headerSize = navBar.getHeight();
        if (headerSize == 0) {
            headerSize = activity.getResources().getDimensionPixelSize(R.dimen.new_progressbar_minheight);
        }

        int availableWindow = contentSize - headerSize - getActionBarSize(activity);

        int questionFontSize = getFontSizeInPx(activity);

        //Request a consistent amount of the screen before groups can cut down

        int spaceRequested = questionFontSize * 6;
        int spaceAvailable = availableWindow - spaceRequested;

        int defaultHeaderSpace = activity.getResources().getDimensionPixelSize(R.dimen.content_min_margin) * 2;

        float textSize = groupLabel.getTextSize();
        int numberOfGroupLinesAllowed = (int)((spaceAvailable - defaultHeaderSpace) / textSize);

        if (numberOfGroupLinesAllowed < 0) {
            numberOfGroupLinesAllowed = 0;
        }
        return numberOfGroupLinesAllowed;
    }

    public static void updateGroupViewVisibility(FormEntryActivity activity,
                                                 boolean groupNativeVisibility,
                                                 boolean groupForcedInvisible) {
        FrameLayout header = (FrameLayout)activity.findViewById(R.id.form_entry_header);
        updateGroupViewVisibility(header, groupNativeVisibility, groupForcedInvisible);
    }

    private static void updateGroupViewVisibility(FrameLayout header,
                                                  boolean groupNativeVisibility,
                                                  boolean groupForcedInvisible) {
        TextView groupLabel = ((TextView)header.findViewById(R.id.form_entry_group_label));

        if (groupNativeVisibility && !groupForcedInvisible) {
            header.setVisibility(View.VISIBLE);
            groupLabel.setVisibility(View.VISIBLE);
        } else {
            header.setVisibility(View.GONE);
            groupLabel.setVisibility(View.GONE);

        }
    }

    private static int getFontSizeInPx(Activity activity) {
        SharedPreferences settings =
                PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
        String question_font =
                settings.getString(FormEntryPreferences.KEY_FONT_SIZE, ODKStorage.DEFAULT_FONTSIZE);

        int sizeInPx = Integer.valueOf(question_font);

        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInPx,
                activity.getResources().getDisplayMetrics());
    }

    private static int getActionBarSize(CommCareActivity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            int actionBarHeight = activity.getActionBar().getHeight();

            if (actionBarHeight != 0) {
                return actionBarHeight;
            }
            final TypedValue tv = new TypedValue();
            if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
            }
            return actionBarHeight;
        }
        return 0;
    }
}

@@ -1529,36 +1529,49 @@
 lowed);%0A        
-if (
+boolean result = 
 numberOfGroupLin
@@ -1580,32 +1580,26 @@
 Allowed == 0
-) %7B%0A    
+;%0A
         upda
@@ -1628,20 +1628,22 @@
 header, 
-true
+result
 , groupN
@@ -1672,154 +1672,22 @@
     
-    
 return 
-true;%0A        %7D else %7B%0A            updateGroupViewVisibility(header, false, groupNativeVisibility);%0A            return false;%0A        %7D
+result;
 %0A   

Remove need for if/else