// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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.

{namespace registry.soy.registrar.contacts}


/** Registrar contacts listing for view only. */
{template .set}
  {@param contactsByType: legacy_object_map<string, ?>}
  {let $possibleTypesLookup: [
      ['admin', 'Primary', 'Primary contact for general issues.'],
      ['billing', 'Billing', 'Contact for financial communications & invoices.'],
      ['tech', 'Technical', 'Contact for technical communications about the registry.'],
      ['legal', 'Legal', 'Contact for all legal communications.'],
      ['marketing', 'Marketing', 'Contact for registry promotions and campaigns.'],
      ['abuse', 'Abuse', 'Contact for abuse complaints.'],
      ['whois', 'WHOIS-Inquiry', 'Contact for inquiries about WHOIS accuracy.'],
      ['other', 'Other', 'Contact that is none of the above types']] /}
  <div class="{css('set')}">
  <h1>Contact settings</h1>
  <table>
    {for $type in $possibleTypesLookup}
      {if isNonnull($contactsByType[$type[0]])}
        <tr class="{css('kd-settings-pane-section')}">
          <td>
            <label class="{css('setting-label')}">{$type[1]}
              {sp}contact{if length($contactsByType[$type[0]]) > 1}s{/if}</label>
            <span class="{css('description')}">{$type[2]}</span>
          </td>
          <td id="{$type[0]}-contacts"
              class="{css('info')} {css('summary')} {css('domain-registrar-contacts')}">
            {for $c in $contactsByType[$type[0]]}
              {call .contactInfoCompact}
                {param namePrefix: 'contacts[' + index($c) + '].' /}
                {param name: $c['name'] /}
                {param emailAddress: $c['emailAddress'] /}
                {param visibleInWhois:
                    ($c['visibleInWhoisAsAdmin']
                    or $c['visibleInWhoisAsTech']
                    or $c['visibleInDomainWhoisAsAbuse']) /}
                {param phoneNumber: $c['phoneNumber'] /}
                {param faxNumber: $c['faxNumber'] /}
              {/call}
              {if (index($c) + 1) % 3 == 0}<br>{/if}
            {/for}
          </td>
      {/if}
    {/for}
  </table>
  </div>
{/template}


/** Compact readonly contact info view. */
{template .contactInfoCompact}
  {@param namePrefix: string}
  {@param name: string}
  {@param emailAddress: string}
  {@param visibleInWhois: bool}
  {@param? phoneNumber: string}
  {@param? faxNumber: string}
  <div class="{css('domain-registrar-contact')}">
    <div class="{css('domain-registrar-contact-name')}"
         id="{$namePrefix}name">
      {$name}
      {if $visibleInWhois}
        <i class="{css('domain-registrar-contact-visible-in-whois')}"
           onmouseover="this.childNodes[0].style.visibility = 'visible'"
           onmouseout="this.childNodes[0].style.visibility = 'hidden'">
          <div class="{css('tooltip')}">
            Visible in WHOIS <span class="{css('pointer')}"></span>
          </div>
        </i>
      {/if}
    </div>
    {if isNonnull($phoneNumber)}
      <div class="{css('domain-registrar-contact-phoneNumber')}"
           id="{$namePrefix}phoneNumber">{$phoneNumber}</div>
    {/if}
    {if isNonnull($faxNumber)}
      <div class="{css('domain-registrar-contact-faxNumber')}"
           id="{$namePrefix}faxNumber">{$faxNumber} (Fax)</div>
    {/if}
    <div class="{css('domain-registrar-contact-emailAddress')}"
         id="{$namePrefix}emailAddress">{$emailAddress}</div>
    <a href="#contact-settings/{$emailAddress}">View/Edit</a>
  </div>
{/template}


/** Registrar contact item page. */
{template .contact}
  {@param namePrefix: string}
  {@param item: legacy_object_map<string, ?>}
  {@param actualTypesLookup: legacy_object_map<string, bool>}
  {@param readonly: bool}
  {@param? registryLockAllowedForRegistrar: bool}
  <form name="item" class="{css('item')} {css('registrar')}">
    <h1>Contact details</h1>
    {call .contactInfo data="all"}
      {param namePrefix: $namePrefix /}
      {param item: $item /}
      {param actualTypesLookup: $actualTypesLookup /}
      {param readonly: $readonly /}
      {param registryLockAllowedForRegistrar: $registryLockAllowedForRegistrar /}
    {/call}
  </form>
{/template}


/** Contact info for view and edit. */
{template .contactInfo}
  {@param namePrefix: string}
  {@param item: legacy_object_map<string, ?>}
  {@param actualTypesLookup: legacy_object_map<string, bool>}
  {@param? readonly: bool}
  {@param? registryLockAllowedForRegistrar: bool}
  {let $possibleTypesLookup: [
      ['admin', 'Primary', 'Primary contact for general issues.'],
      ['billing', 'Billing', 'Contact for financial communications & invoices.'],
      ['tech', 'Technical', 'Contact for technical communications about the registry.'],
      ['legal', 'Legal', 'Contact for all legal communications.'],
      ['marketing', 'Marketing', 'Contact for registry promotions and campaigns.'],
      ['abuse', 'Abuse', 'Contact for abuse complaints.'],
      ['whois', 'WHOIS-Inquiry', 'Contact for inquiries about WHOIS accuracy.']] /}
  <div>
    <table>
      {call registry.soy.forms.inputFieldRow data="all"}
        {param label: 'Name' /}
        {param namePrefix: $namePrefix /}
        {param name: 'name' /}
      {/call}
      {call registry.soy.forms.inputFieldRow data="all"}
        {param label: 'Primary account email' /}
        {param namePrefix: $namePrefix /}
        {param name: 'emailAddress' /}
        {param disabled: not $readonly and $item['emailAddress'] != null /}
      {/call}
      {if isNonnull($item['registryLockEmailAddress'])}
        {call registry.soy.forms.inputFieldRow data="all"}
          {param label: 'Registry lock email address' /}
          {param namePrefix: $namePrefix /}
          {param name: 'registryLockEmailAddress' /}
          {param disabled: not $readonly /}
          {param description: 'Address to which registry (un)lock confirmation emails will be ' +
              'sent. This is not necessarily the account email address that is used for login.' /}
        {/call}
      {/if}
      {call registry.soy.forms.inputFieldRow data="all"}
        {param label: 'Phone' /}
        {param namePrefix: $namePrefix /}
        {param name: 'phoneNumber' /}
      {/call}
      {call registry.soy.forms.inputFieldRow data="all"}
        {param label: 'Fax' /}
        {param namePrefix: $namePrefix /}
        {param name: 'faxNumber' /}
      {/call}
      {if $readonly}
        {call .contactTypeSettingsView_}
          {param item: $item /}
          {param possibleTypesLookup: $possibleTypesLookup /}
          {param actualTypesLookup: $actualTypesLookup /}
        {/call}
      {else}
        {call .contactTypeSettingsEdit_}
          {param item: $item /}
          {param namePrefix: $namePrefix /}
          {param possibleTypesLookup: $possibleTypesLookup /}
          {param actualTypesLookup: $actualTypesLookup /}
          {param registryLockAllowedForRegistrar: $registryLockAllowedForRegistrar /}
        {/call}
      {/if}
    </table>
    {if isNonnull($item['gaeUserId'])}
      <input type="hidden" name="{$namePrefix}gaeUserId" value="{$item['gaeUserId']}">
    {/if}
  </div>
{/template}


/** @private */
{template .contactTypeSettingsView_ visibility="private"}
  {@param item: legacy_object_map<string, ?>}
  {@param possibleTypesLookup: list<list<string>>}
  {@param actualTypesLookup: legacy_object_map<string, bool>}
  <tr class="{css('kd-settings-pane-section')}">
    <td>
      <label class="{css('setting-label')}">Contact type</label>
    <td class="{css('setting')}">
      <div class="{css('setting-item-list')}">
        {for $type in $possibleTypesLookup}
          {if $actualTypesLookup[$type[0]]}
            <div>{$type[1]} contact</div>
          {/if}
        {/for}
      </div>
    </td>
  <tr><td colspan="2"><hr></tr>
  <tr class="{css('kd-settings-pane-section')}">
    <td>
      <label class="{css('setting-label')}">Show in WHOIS</label>
    </td>
    <td class="{css('setting')}">
      <p class="{css('setting-item-list')}">
        {let $visibleAsAdmin: $item['visibleInWhoisAsAdmin'] == true /}
        {let $visibleAsTech: $item['visibleInWhoisAsTech'] == true /}
        {let $visibleAsDomainAbuse: $item['visibleInDomainWhoisAsAbuse'] == true /}
        {if (not $visibleAsAdmin) and (not $visibleAsTech) and (not $visibleAsDomainAbuse)}
          <span class="{css('whois-not-visible')}">Not visible in WHOIS</span>
        {else}
          {if $visibleAsAdmin}Registrar Admin{/if}
          {if $visibleAsAdmin and $visibleAsTech},{sp}{/if}
          {if $visibleAsTech}Registrar Technical{/if}
          {if $visibleAsTech}
            {if $visibleAsDomainAbuse},{sp}{/if}
          {elseif $visibleAsAdmin and $visibleAsDomainAbuse},{sp}{/if}
          {if $visibleAsDomainAbuse}Domain Abuse{/if}
        {/if}
{/template}


/** @private */
{template .contactTypeSettingsEdit_ visibility="private"}
  {@param item: legacy_object_map<string, ?>}
  {@param namePrefix: string}
  {@param possibleTypesLookup: list<list<string>>}
  {@param actualTypesLookup: legacy_object_map<string, bool>}
  {@param? registryLockAllowedForRegistrar: bool}
  <tr class="{css('kd-settings-pane-section')}">
    <td>
      <label class="{css('setting-label')}">Contact type</label>
      <span class="{css('description')}">
        Subscribe this contact to the checked mailing lists.
      </span>
    </td>
    <td class="{css('setting')}">
      {call .contactCheckboxes_}
        {param namePrefix: $namePrefix /}
        {param possibleTypesLookup: $possibleTypesLookup /}
        {param actualTypesLookup: $actualTypesLookup /}
      {/call}
  </tr>
  {if $registryLockAllowedForRegistrar}
    {let $placeholder: $item['registryLockAllowed'] ?
      // If registry lock is allowed, there's already a password
      'Contact support to reset password' :
        // Otherwise, if not allowed, support must enable it
        not $item['allowedToSetRegistryLockPassword'] ?
          'Contact support to enable registry lock' : '' /}
    {call registry.soy.forms.inputFieldRow data="all"}
      {param label: 'Registry lock password:' /}
      {param name: 'registryLockPassword' /}
      {param disabled: not $item['allowedToSetRegistryLockPassword'] /}
      {param isPassword: true /}
      {param placeholder: $placeholder /}
    {/call}
    {if $item['allowedToSetRegistryLockPassword']}
      <tr>
        <td></td>
        <td>
          <a id="showOrHideRegistryLockPassword">Show password</a>
        </td>
      </tr>
    {/if}

  {/if}
  <input type="hidden" name="{$namePrefix}allowedToSetRegistryLockPassword"
      {if isNonnull($item['allowedToSetRegistryLockPassword'])}
         value="{$item['allowedToSetRegistryLockPassword']}"
      {else}
         value="false"
      {/if}
  >
  <tr>
    <td colspan="2">
      <hr>
  </tr>
  {call .whoisVisibleRadios_}
    {param description: 'Show in Registrar WHOIS record as admin contact' /}
    {param fieldName: $namePrefix + 'visibleInWhoisAsAdmin' /}
    {param visible: $item['visibleInWhoisAsAdmin'] == true /}
  {/call}
  {call .whoisVisibleRadios_}
    {param description: 'Show in Registrar WHOIS record as technical contact' /}
    {param fieldName: $namePrefix + 'visibleInWhoisAsTech' /}
    {param visible: $item['visibleInWhoisAsTech'] == true /}
  {/call}
  {call .whoisVisibleRadios_}
    {param description:
        'Show Phone and Email in Domain WHOIS Record as registrar abuse contact' +
        ' (per CL&D requirements)' /}
    {param note:
        '*Can only apply to one contact. Selecting Yes for this contact will' +
        ' force this setting for all other contacts to be No.' /}
    {param fieldName: $namePrefix + 'visibleInDomainWhoisAsAbuse' /}
    {param visible: $item['visibleInDomainWhoisAsAbuse'] == true /}
  {/call}
{/template}


/** @private */
{template .whoisVisibleRadios_ visibility="private"}
  {@param description: string}
  {@param? note: string}
  {@param fieldName: string}
  {@param visible: bool}
  <tr class="{css('kd-settings-pane-section')}">
    <td>
      <label for="{$fieldName}">{$description}</label>
      {if $note}
        <span class="{css('description')}">{$note}</span>
      {/if}
    </td>
    <td class="{css('setting')}">
      <label for="{$fieldName}">
        <input
          name="{$fieldName}"
          id="{$fieldName}.true"
          type="radio"
          value="true"
          {if $visible} checked{/if}>{sp}Yes
      </label>
      <label for="{$fieldName}">
        <input
          name="{$fieldName}"
          id="{$fieldName}.false"
          type="radio"
          value="false"
          {if not $visible} checked{/if}>{sp}No
      </label>
    </td>
{/template}


/** @private */
{template .contactCheckboxes_ visibility="private"}
  {@param namePrefix: string}
  {@param actualTypesLookup: legacy_object_map<string, bool>}
  {@param possibleTypesLookup: list<list<string>>}
  {for $type in $possibleTypesLookup}
    {let $name: $namePrefix + 'type.' + $type[0] /}
    {let $checked: $actualTypesLookup[$type[0]] /}
    <div class="{css('checkbox-with-label')}">
      <input type="checkbox"
             name="{$name}"
             id="{$name}"
             {if $checked} checked{/if}>
      <label for="{$name}">
        {$type[1]} contact
        <span class="{css('description')}">{$type[2]}</span>
      </label>
    </div>
  {/for}
{/template}


/** IP address form input. */
{template .ip}
  {@param name: string}
  {@param ip: string}
  <div class="{css('ip')}">
    <input name="{$name}" value="{$ip}" readonly>
    <button type="button" class="{css('kd-button')} {css('btn-remove')} {css('hidden')}">
      <i class="{css('icon-remove')} {css('edit')}">x</i>
    </button>
  </div>
{/template}
