<%--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you 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.
  --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:url var="iconDeleteUrl" value="/images/icons/delete.gif"/>
<c:url var="iconCreateUrl" value="/images/icons/create.png"/>

<%-- This hidden 'pattern' field is used by remove (white|black)list scripts --%>
<s:hidden name="pattern" />

<s:hidden name="connector.order" />

<s:select name="connector.proxyId" list="proxyIdOptions" label="Network Proxy" required="true"/>
<s:select name="connector.sourceRepoId" list="managedRepoIdList"
           label="Managed Repository" required="true"/>
<s:select name="connector.targetRepoId" list="remoteRepoIdList"
           label="Remote Repository" required="true"/>

<tr>
  <td valign="top"><label>Policies:</label>
  </td>
  <td>
    <table>
      <c:forEach items="${policyMap}" var="policy" varStatus="i">
        <tr>
          <td>
            <s:label for="policy_%{#attr.policy.key}" required="true"
                      theme="simple">${policy.value.name}:
            </s:label>
          </td>
          <td>
            <c:set var="value">
              <s:property id="value" value="%{#attr.connector.policies[#attr.policy.key]}" default="%{#attr.policy.value.defaultOption}" />
            </c:set>
            <s:select name="connector.policies['%{#attr.policy.key}']"
                       list="%{#attr.policyMap[#attr.policy.key].options}"
                       value="%{#attr.value}"
                       id="policy_%{#attr.policy.key}"
                       theme="simple"
                       cssStyle="width: 10em"/>
          </td>
        </tr>
      </c:forEach>
    </table>
  </td>
</tr>

<tr class="seperator">
  <td valign="top">
    <label for="propertiesEntry">Properties:</label>
  </td>
  <td>
    <s:textfield name="propertyKey" size="15" id="propertiesEntry" theme="simple"
                  onkeypress="submitenter(event, 'addProperty')"/>
    :
    <s:textfield name="propertyValue" size="15" id="propertiesValue" theme="simple"
                  onkeypress="submitenter(event, 'addProperty')"/>
    <input type="button" onclick="submitForm('addProperty')" value="Add Property" />
  </td>
</tr>

<tr>
  <td>
  </td>
  <td>
    <c:choose>
      <c:when test="${empty (connector.properties)}">
        <i>No properties have been set.</i>
      </c:when>
      <c:otherwise>
        <table>
          <c:forEach items="${connector.properties}" var="property" varStatus="i">
            <tr>
              <td>
                <s:label for="property_%{#attr.property.key}"
                          theme="simple">${property.key}</s:label>
              </td>
              <td>
                <s:textfield name="connector.properties['%{#attr.property.key}']"
                              size="15"
                              id="property_%{#attr.property.key}"
                              theme="simple"/>
              </td>
              <td>
                <s:a href="#" title="Remove [%{#attr.property.key}] Property"
                      onclick="setAndSubmit('propertyKey', '%{#attr.property.key}', 'removeProperty')"
                      theme="simple">
                  <img src="${iconDeleteUrl}"/></s:a>
              </td>
            </tr>
          </c:forEach>
        </table>
      </c:otherwise>
    </c:choose>
  </td>
</tr>

<tr class="seperator">
  <td valign="top">
    <label for="blackListEntry">Black List:</label>
  </td>
  <td>
    <s:textfield name="blackListPattern" size="30" id="blackListEntry" theme="simple"
                  onkeypress="submitenter(event, 'addBlackListPattern')"/>
    <input type="button" onclick="submitForm('addBlackListPattern')" value="Add Pattern" />
  </td>
</tr>

<tr>
  <td>
  </td>
  <td>
    <c:choose>
      <c:when test="${empty (connector.blackListPatterns)}">
        <i>No black list patterns have been set.</i>
      </c:when>
      <c:otherwise>
        <table>
          <c:forEach items="${connector.blackListPatterns}" var="pattern" varStatus="i">
            <tr>
              <td>
                <s:hidden name="connector.blackListPatterns" value="%{#attr.pattern}"/>
                <code>"${pattern}"</code>
              </td>
              <td>
                <a href="#" title="Remove [${pattern}] Pattern"
                      onclick="setAndSubmit('pattern', '${pattern}', 'removeBlackListPattern')">
                  <img src="${iconDeleteUrl}"/></a>
              </td>
            </tr>
          </c:forEach>
        </table>
      </c:otherwise>
    </c:choose>
  </td>
</tr>

<tr class="seperator">
  <td valign="top">
    <label for="whiteListEntry">White List:</label>
  </td>
  <td>
    <s:textfield name="whiteListPattern" size="30" id="whiteListEntry" theme="simple"
                  onkeypress="submitenter(event, 'addWhiteListPattern')"/>
    <input type="button" onclick="submitForm('addWhiteListPattern')" value="Add Pattern" />
  </td>
</tr>
<tr>
  <td>
  </td>
  <td>
    <c:choose>
      <c:when test="${empty (connector.whiteListPatterns)}">
        <i>No white list patterns have been set.</i>
      </c:when>
      <c:otherwise>
        <table>
          <c:forEach items="${connector.whiteListPatterns}" var="pattern" varStatus="i">
            <tr>
              <td>
                <s:hidden name="connector.whiteListPatterns" value="%{#attr.pattern}"/>
                <code>"${pattern}"</code>
              </td>
              <td>
                <s:a href="#" title="Remove [%{#attr.pattern}] Pattern"
                      onclick="setAndSubmit('pattern', '%{#attr.pattern}', 'removeWhiteListPattern')"
                      theme="simple">
                  <img src="${iconDeleteUrl}"/></s:a>
              </td>
            </tr>
          </c:forEach>
        </table>
      </c:otherwise>
    </c:choose>
  </td>
</tr>

<script type="text/javascript">
  <!--
  function adjustActionMethod( action, method )
  {
    var idx = action.indexOf( "!" );
    if( idx == (-1) )
    {
      // no "name!method.action" defined, split by ".action" instead.
      idx = action.indexOf( ".action" );
    }
    
    return action.substring( 0, idx ) + "!" + method + ".action";
  }
  
  function setAndSubmit( id, value, method )
  {
    var f = document.forms['saveProxyConnector'];

    f.action = adjustActionMethod( f.action, method );
    f.elements[id].value = value;
    f.submit();
  }

  function submitForm( method )
  {
    var f = document.forms['saveProxyConnector'];

    var before = f.action;
    f.action = adjustActionMethod( f.action, method );
    f.submit();
  }

  function submitenter( e, method )
  {
    var keycode;


    if ( window.event )
    {
      keycode = window.event.keyCode;
    }
    else if ( e )
    {
      keycode = e.which;
    }
    else
    {
      return true;
    }

    if ( keycode == 13 )
    {
      submitForm( method );
      return false;
    }
    else
    {
      return true;
    }
  }
  
  document.forms["saveProxyConnector"]["connector.proxyId"].focus();
  //-->
</script>

