<!--
@license
Copyright 2016 The TensorFlow 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.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-slider/paper-slider.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-checkbox/paper-checkbox.html">
<link rel="import" href="../tf-imports/lodash.html">

<!--
tf-smoothing-input creates an input component for exponential smoothing.
-->
<dom-module id="tf-smoothing-input">
  <template>
    <h3 class="title">Smoothing</h3>
    <div class="smoothing-block">
      <paper-slider
        id="slider"
        value="{{weight}}"
        immediate-value="{{_immediateWeightNumberForPaperSlider}}"
        type="number"
        step="[[step]]"
        min="[[min]]"
        max="[[max]]"
        ></paper-slider>
      <paper-input
        id="input"
        label="weight"
        no-label-float
        value="{{_inputWeightStringForPaperInput}}"
        type="number"
        step="[[step]]"
        min="[[min]]"
        max="[[max]]"
        ></paper-input>
    </div>
    <style>
      .title {
        color: var(--paper-grey-800);
        margin: 0;
        font-weight: normal;
        font-size: 14px;
        margin-bottom: 5px;
      }

      .smoothing-block {
        display: flex;
      }

      paper-slider {
        margin-left: 12px;
        --paper-slider-knob-color: var(--tb-orange-strong);
        --paper-slider-active-color: var(--tb-orange-strong);
        flex-grow: 2;
      }

      paper-input {
        --paper-input-container-focus-color: var(--tb-orange-strong);
        --paper-input-container-input: {
          font-size: 14px;
        };
        --paper-input-container-label: {
          font-size: 14px;
        };
        width: 60px;
      }
    </style>
  </template>
  <script>
    Polymer({
      is: "tf-smoothing-input",

      properties: {
        step: Number,
        max: Number,
        min: Number,

        weight: {
          type: Number,
          value: 0.6,
          notify: true
        },

        _immediateWeightNumberForPaperSlider: {
          type: Number,
          notify: true,
          observer: '_immediateWeightNumberForPaperSliderChanged'
        },

        // Paper input treats values as strings even if you specify them as
        // numbers.
        _inputWeightStringForPaperInput: {
          type: String,
          notify: true,
          observer: '_inputWeightStringForPaperInputChanged'
        }
      },

      _updateWeight: _.debounce(function(val) {
        this.weight = val;
      }, 250),

      _immediateWeightNumberForPaperSliderChanged: function() {
        this._inputWeightStringForPaperInput =
            this._immediateWeightNumberForPaperSlider.toString();
        this._updateWeight.call(this, this._immediateWeightNumberForPaperSlider);
      },

      _inputWeightStringForPaperInputChanged: function() {
        if (+this._inputWeightStringForPaperInput < 0) {
          this._inputWeightStringForPaperInput = '0';
        }
        else if (+this._inputWeightStringForPaperInput > 1) {
          this._inputWeightStringForPaperInput = '1';
        }

        var d = +this._inputWeightStringForPaperInput;
        if (!isNaN(d)) {
          this._updateWeight.call(this, d);
        }
      }
    });
  </script>
</dom-module>
