<!--
@license
Copyright 2018 The Tensor2Tensor Authors.

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.
-->
<link rel="import" href="../polymer/polymer.html">

<link rel="import" href="../app-route/app-location.html">
<link rel="import" href="../app-route/app-route.html">

<link rel="import" href="../iron-a11y-keys/iron-a11y-keys.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-list/iron-list.html">

<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-toggle-button/paper-toggle-button.html">
<link rel="import" href="../paper-progress/paper-progress.html">

<link rel="import" href="../query_card/query-card.html">
<link rel="import" href="../translation_result/translation-result.html">

<dom-module id="explore-view">
  <template>
    <style include="iron-flex iron-flex-alignment iron-flex-reverse">
      :host {
        padding: 24px;
        @apply --layout-vertical;
        @apply --layout-center;
      }

      query-card {
        margin: 0px;
        width: 90%;
      }

      div.rule {
        @apply --layout-vertical;
      }

      paper-progress {
        --paper-progress-active-color: #4285f4;
        --paper-progress-height: 10px;
        width: 90%;
      }

      translation-result {
        margin: 12px 0px;
      }

      paper-input {
        padding: 0px 6px;
      }

      paper-icon-button#clear {
        color: var(--paper-red-300);
        --paper-icon-button-ink-color: var(--paper-red-a100);
        height: 23px;
        padding: 0px 4px;
        width: 23px;
      }

      paper-icon-button#translate {
        background-color: #4d90fe;
        color: #fff;
        border-radius: 50%;
      }

      #result-list {
        margin: 24px 0px;
        width: 90%;
      }
    </style>
    <!--
      Extract the query information from the url if it exists.
    -->
    <app-route
        route="{{subroute}}"
        pattern="/:query"
        tail="{{tailRoute}}"
        data="{{queryData}}">
    </app-route>

    <query-card
        route="{{route}}"
        url="/api/list_models"
        sub-route="{{subroute}}"
        model="{{model_}}">
      <!--
        Include a text area and actionable button for sending translations.
      -->
      <div id="search-bar" class="layout horizontal center-center">
        <paper-input class="flex" value="{{query_}}" label="translate" id="input">
        </paper-input>
        <iron-a11y-keys target="{{input}}" keys="enter" on-keys-pressed="translate_">
        </iron-a11y-keys>
        <paper-icon-button id="translate" on-tap="translate_" icon="translate" title="translate">
        </paper-icon-button>
      </div>
      <div id="extra">
        <h4>Rapid Response</h4>
        <template is="dom-repeat" items="{{rules_}}">
          <div class="rule">
            <span on-tap="deleteRule_">
              <iron-icon icon="remove-circle-outline">
              </iron-icon>
              Rule
            </span>
            <paper-input label="Source" value="{{item.source}}" type="text"></paper-input>
            <paper-input label="Bad Target" value="{{item.bad_translations}}" type="text"></paper-input>
            <paper-input value="{{item.good_translations}}" label="Good Target" type="text"></paper-input>
            <paper-input value="{{item.attention_threshold}}" label="Threshold" type="number"></paper-input>
          </div>
        </template>
        <span on-tap="addRule_">
          <iron-icon icon="add-circle-outline">
          </iron-icon>
          Rule
        </span>
      </div>
    </query-card>

    <paper-progress id="loading" indeterminate disabled="[[!fetchingResult]]">
    </paper-progress>

    <div id="result-list" class="layout vertical vertical-reverse">
      <template is="dom-repeat" items="[[results]]" as="result">
        <translation-result result="[[result]]"></translation-result>
      </template>
    </div>

    <iron-ajax
      id="translateAjax"
      url="{{url}}"
      handle-as="json"
      on-response="handleTranslationResponse_">
    </iron-ajax>
  </template>
  <script src="../d3/d3.js"></script>
  <script src="explore-view.js"></script>
</dom-module>
