package com.smartdevicelink.managers;

import com.smartdevicelink.proxy.rpc.ImageField;
import com.smartdevicelink.proxy.rpc.TextField;
import com.smartdevicelink.proxy.rpc.WindowCapability;
import com.smartdevicelink.proxy.rpc.enums.ImageFieldName;
import com.smartdevicelink.proxy.rpc.enums.TextFieldName;

/**
 * <strong>ManagerUtility</strong> <br>
 * <p>
 * Static Methods to be used throughout the Manager classes <br>
 */
public class ManagerUtility {

    public static class WindowCapabilityUtility {

        /**
         * Check to see if WindowCapability has an ImageFieldName of a given name.
         *
         * @param windowCapability WindowCapability representing the capabilities of the desired window
         * @param name ImageFieldName representing a name of a given Image field that would be stored in WindowCapability
         * @return true if name exist in WindowCapability else false
         */
        public static boolean hasImageFieldOfName(WindowCapability windowCapability, ImageFieldName name) {
            if (windowCapability == null || name == null) {
                return false;
            }
            if (windowCapability.getImageFields() != null) {
                for (ImageField field : windowCapability.getImageFields()) {
                    if (field != null && name.equals(field.getName())) {
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * Check to see if WindowCapability has a textField of a given name.
         *
         * @param windowCapability WindowCapability representing the capabilities of the desired window
         * @param name TextFieldName representing a name of a given text field that would be stored in WindowCapability
         * @return true if name exist in WindowCapability else false
         */
        public static boolean hasTextFieldOfName(WindowCapability windowCapability, TextFieldName name) {
            if (windowCapability == null || name == null) {
                return false;
            }
            if (windowCapability.getTextFields() != null) {
                for (TextField field : windowCapability.getTextFields()) {
                    if (field != null && name.equals(field.getName())) {
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * Method to get number of textFields allowed to be set according to WindowCapability
         *
         * @param windowCapability WindowCapability representing the capabilities of the desired window
         * @return linesFound Number of textFields found in WindowCapability
         */
        public static int getMaxNumberOfMainFieldLines(WindowCapability windowCapability) {
            int highestFound = 0;
            TextFieldName name;
            if (windowCapability != null && windowCapability.getTextFields() != null) {
                for (TextField field : windowCapability.getTextFields()) {
                    if (field.getName() != null) {
                        name = field.getName();
                        if (name == TextFieldName.mainField1 || name == TextFieldName.mainField2 || name == TextFieldName.mainField3 || name == TextFieldName.mainField4) {
                            int fieldNumber = Integer.parseInt(name.toString().substring(name.toString().length() - 1));
                            highestFound = Math.max(highestFound, fieldNumber);
                            if (highestFound == 4) {
                                break;
                            }
                        }
                    }
                }
            }
            return highestFound;
        }
    }
}

@@ -2859,40 +2859,8 @@
  0;%0A
-            TextFieldName name;%0A
     
@@ -3035,25 +3035,70 @@
            i
-f
+nt fieldNumber = 0;%0A                    switch
  (field.getN
@@ -3102,24 +3102,16 @@
 etName()
- != null
 ) %7B%0A    
@@ -3134,190 +3134,376 @@
     
-name = field.getName();%0A                        if (name == TextFieldName.mainField1 %7C%7C name == TextFieldName.mainField2 %7C%7C name == TextFieldName.mainField3 %7C%7C name == TextFieldName.
+case mainField1:%0A                            fieldNumber = 1;%0A                            break;%0A                        case mainField2:%0A                            fieldNumber = 2;%0A                            break;%0A                        case mainField3:%0A                            fieldNumber = 3;%0A                            break;%0A                        case 
 main
@@ -3508,19 +3508,17 @@
 inField4
-) %7B
+:
 %0A       
@@ -3530,36 +3530,32 @@
                 
-int 
 fieldNumber = In
@@ -3556,85 +3556,111 @@
 r = 
-Integer.parseInt(name.toString().substring(name.toString().length() - 1));%0A  
+4;%0A                            break;%0A                    %7D%0A                    if (fieldNumber %3E 0) %7B%0A
     
@@ -3679,18 +3679,16 @@
         
-  
 highestF
@@ -3751,28 +3751,24 @@
             
-    
 if (highestF
@@ -3776,28 +3776,24 @@
 und == 4) %7B%0A
-    
             
@@ -3819,38 +3819,8 @@
 ak;%0A
-                            %7D%0A
     

Simplify getMaxNumberOfMainFieldLines() logic