<!--
@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="../../tf-graph-board/tf-graph-board.html">
<link rel="import" href="../../tf-graph-loader/tf-graph-loader.html">
<link rel="import" href="../../tf-graph/tf-graph-controls.html">

<!--
Element for tf-graph demo page

Example:

<tf-graph-demo></tf-graph-demo>
-->
<dom-module id="tf-graph-demo">
<template>
<style>

:host /deep/ {
  font-family: 'Roboto', sans-serif;
}

.main {
  position: absolute;
  right: 0;
  left: 250px;
  height: 100%;
}

.side {
  position: absolute;
  left: 0;
  width: 250px;
  height: 100%;
  border: 1px solid black;
  box-sizing: border-box;
}

.all {
  position: relative;
  width: 100%;
  height: 100%
}

</style>
<div class="all">
  <div class="side">
    <!-- The observatory header component is injected in during vulcanization
         and an instance of it is initialized and filled here when the demo
         app initializes. -->
    <div id="observatory-header"></div>
    <tf-graph-controls
        devices-for-stats="{{_devicesForStats}}"
        color-by-params="[[colorByParams]]"
        stats="[[stats]]"
        color-by="{{colorBy}}"
        datasets="[[datasets]]"
        render-hierarchy="[[_renderHierarchy]]"
        selected-dataset="{{selectedDataset}}"
        selected-file="{{selectedFile}}"
        selected-metadata-tag="{{selectedMetadataTag}}"
        show-session-runs-dropdown="[[showSessionRunsDropdown]]"
        show-upload-button="[[showUploadButton]]"
    ></tf-graph-controls>
    <tf-graph-loader id="loader"
        datasets="[[datasets]]"
        selected-dataset="[[selectedDataset]]"
        selected-metadata-tag="[[selectedMetadataTag]]"
        selected-file="[[selectedFile]]"
        out-graph-hierarchy="{{graphHierarchy}}"
        out-graph="{{graph}}"
        out-stats="{{stats}}"
        progress="{{_progress}}"
        out-hierarchy-params="{{_hierarchyParams}}"
    ></tf-graph-loader>
  </div>
  <div class="main">
    <tf-graph-board id="graphboard"
        color-by="[[colorBy]]"
        color-by-params="{{colorByParams}}"
        devices-for-stats="[[_devicesForStats]]"
        graph-hierarchy="[[graphHierarchy]]"
        graph="[[graph]]"
        hierarchy-params="[[_hierarchyParams]]"
        progress="[[_progress]]"
        render-hierarchy="{{_renderHierarchy}}"
        stats="[[stats]]"
    ></tf-graph-board>
  </div>
</div>
</template>
</dom-module>

<script>
(function(){

Polymer({
  is: 'tf-graph-demo',
  properties: {
    datasets: {
      type: Object
    },
    selectedDataset: {
      type: Number,
      value: 0,
      observer: '_selectedDatasetChanged'
    },
    _renderHierarchy: Object,
    _progress: Object,
    showSessionRunsDropdown: {
      type: Boolean,
      value: true
    },
    showUploadButton: {
      type: Boolean,
      value: true
    }
  },
  created: function() {
    let queryParams = tf.graph.util.getQueryParams(location.search);
    let selectedDataset = 0;

    if (typeof DEMO_DATASETS === 'undefined') {
      DEMO_DATASETS = 'demo_datasets.json';
    }

    d3.json(DEMO_DATASETS, function (error, datasets) {
        let publicDatasets = [];

        if (error) {
          console.log('Error loading demo datasets:');
          console.log(error);
          return;
        }

        if(typeof DEMO_DIR_PREFIX === 'undefined') {
          DEMO_DIR_PREFIX = 'tf_model_zoo/';
        }
        _.each(datasets, function(dataset, index) {
          if (queryParams['graphid'] && dataset.id == queryParams['graphid']) {
            selectedDataset = index;
          } else if (dataset['private']) {
            return;
          }

          dataset.path = this._normalizePath(dataset.path);
          if (dataset.runMetadata != null) {
            _.each(dataset.runMetadata, function(metadata) {
              metadata.path = this._normalizePath(metadata.path);
            }, this);
          }
          publicDatasets.push(dataset);
        }, this);
        this.set('datasets', publicDatasets);
        if (selectedDataset != 0) {
          this.set('selectedDataset', selectedDataset);
        }
    }.bind(this));
  },
  ready: function() {
    if (typeof IS_OBSERVATORY !== 'undefined' && IS_OBSERVATORY) {
      // Create the header and add it to the DOM. This component is injected in
      // during vulcanization.
      document.getElementById('observatory-header').appendChild(
          document.createElement('tf-graph-observatory-header'));

      this.set('showSessionRunsDropdown', false);
      this.set('showUploadButton', false);
    }
  },
  _normalizePath: function(path) {
    return this.resolveUrl(DEMO_DIR_PREFIX + path);
  },
  _selectedDatasetChanged: function() {
    if (this.datasets) {
      let dataset = this.datasets[this.selectedDataset];
      let queryParams = '';
      if (dataset['id']) {
        queryParams = '?graphid=' + dataset['id'];
      }
      window.history.replaceState(
          null, null, location.pathname + queryParams);
    }
  }
});
})();
</script>
