/*-------------------------------------------------------------------------------------------------------------------*\
|  Copyright (C) 2015 eBay Software Foundation                                                                        |
|                                                                                                                     |
|  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance     |
|  with the License.                                                                                                  |
|                                                                                                                     |
|  You may obtain a copy of the License at                                                                            |
|                                                                                                                     |
|       http://www.apache.org/licenses/LICENSE-2.0                                                                    |
|                                                                                                                     |
|  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed   |
|  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for  |
|  the specific language governing permissions and limitations under the License.                                     |
\*-------------------------------------------------------------------------------------------------------------------*/

package com.paypal.selion.platform.grid.browsercapabilities;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.uiautomation.ios.IOSCapabilities;
import org.uiautomation.ios.communication.device.DeviceVariation;

import com.paypal.selion.platform.grid.Grid;
import com.paypal.selion.platform.grid.MobileTestSession;

class IOSDriverCapabilitiesBuilder extends DefaultCapabilitiesBuilder {

    private String MOBILE_NODE_TYPE = "mobileNodeType";
    private String IOS_DRIVER = "ios-driver";
    
    @Override
    public DesiredCapabilities getCapabilities(DesiredCapabilities capabilities) {
        MobileTestSession mobileSession = Grid.getMobileTestSession();
        
        IOSCapabilities iosCapabilities = new IOSCapabilities(capabilities.asMap());
        iosCapabilities.setCapability(IOSCapabilities.DEVICE, mobileSession.getDevice());
        iosCapabilities.setCapability(IOSCapabilities.LANGUAGE, mobileSession.getAppLanguage());
        iosCapabilities.setCapability(IOSCapabilities.LOCALE, mobileSession.getAppLocale());
        iosCapabilities.setCapability(IOSCapabilities.BUNDLE_NAME, mobileSession.getAppName());
        iosCapabilities.setCapability(MOBILE_NODE_TYPE, IOS_DRIVER);
        if (StringUtils.isNotBlank(mobileSession.getAppVersion())) {
            iosCapabilities.setCapability(IOSCapabilities.BUNDLE_VERSION, mobileSession.getAppVersion());
        }
        if (StringUtils.isNotBlank(mobileSession.getPlatformVersion())) {
            iosCapabilities.setCapability(IOSCapabilities.UI_SDK_VERSION, mobileSession.getPlatformVersion());
        }
        if (StringUtils.isNotBlank(mobileSession.getDeviceType())) {
            iosCapabilities.setCapability(IOSCapabilities.VARIATION,
                    DeviceVariation.valueOf(mobileSession.getDeviceType()));
        }
        return iosCapabilities;
    }
}

@@ -2184,58 +2184,8 @@
 e%22;%0A
-    private String IOS_DRIVER = %22ios-driver%22;%0A    
 %0A   
@@ -2290,79 +2290,216 @@
     
-MobileTestSession mobileSession = Grid.getMobileTestSession();%0A        
+IOSCapabilities iOSCapabilities = new IOSCapabilities();%0A        setMandatoryCapabilitiesFor(iOSCapabilities);%0A        setOptionalCapabilitiesFor(iOSCapabilities);%0A        iOSCapabilities.merge(capabilities);
 %0A   
@@ -2495,33 +2495,40 @@
 ities);%0A        
-I
+return i
 OSCapabilities i
@@ -2525,20 +2525,54 @@
 bilities
- ios
+;%0A    %7D%0A%0A    private void setMandatory
 Capabili
@@ -2567,39 +2567,36 @@
 toryCapabilities
- = new 
+For(
 IOSCapabilities(
@@ -2594,18 +2594,21 @@
 bilities
-(c
+ iOSC
 apabilit
@@ -2614,16 +2614,80 @@
 ties
-.asMap()
+) %7B%0A        MobileTestSession mobileSession = Grid.getMobileTestSession(
 );%0A 
@@ -2686,34 +2686,34 @@
 ion();%0A        i
-os
+OS
 Capabilities.set
@@ -2776,34 +2776,34 @@
 ce());%0A        i
-os
+OS
 Capabilities.set
@@ -2873,34 +2873,34 @@
 ge());%0A        i
-os
+OS
 Capabilities.set
@@ -2966,34 +2966,34 @@
 le());%0A        i
-os
+OS
 Capabilities.set
@@ -3062,34 +3062,34 @@
 me());%0A        i
-os
+OS
 Capabilities.set
@@ -3121,206 +3121,221 @@
 PE, 
-IOS_DRIVER);%0A        if (StringUtils.isNotBlank(mobileSession.getAppVersion())) %7B%0A 
+mobileSession.getMobileNodeType().getMobileNodeType());%0A
     
+%7D%0A%0A
     
-   iosCapabilities.setCapability(IOSCapabilities.BUNDLE_VERSION, mobileSession.getAppVer
+private void setOptionalCapabilitiesFor(IOSCapabilities iOSCapabilities) %7B%0A        MobileTestSession mobileSession = Grid.getMobileTestSes
 sion()
-)
 ;%0A
-        %7D%0A
     
@@ -3378,32 +3378,27 @@
 eSession.get
-Platform
+App
 Version())) 
@@ -3404,34 +3404,34 @@
  %7B%0A            i
-os
+OS
 Capabilities.set
@@ -3461,14 +3461,14 @@
 ies.
-UI_SDK
+BUNDLE
 _VER
@@ -3494,16 +3494,11 @@
 .get
-Platform
+App
 Vers
@@ -3597,18 +3597,18 @@
        i
-os
+OS
 Capabili
@@ -3748,40 +3748,9 @@
     
-    return iosCapabilities;%0A    
 %7D
+%0A
 %0A%7D%0A

Fix IOSDriverCapabilitiesBuilder to add simulator