/**
 * @copyright Copyright 2014 Google Inc. 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.
 *
 * @fileoverview chartWrapperService is an angular service used to create
 * instances of google.visualization.ChartWrapper. It also provides some helper
 * methods around ChartWrapper.
 * @author joemu@google.com (Joe Allan Muharsky)
 */

goog.provide('p3rf.perfkit.explorer.components.widget.data_viz.gviz.ChartWrapperService');

goog.require('p3rf.perfkit.explorer.components.widget.data_viz.gviz.getGvizChartWrapper');
goog.require('p3rf.perfkit.explorer.models.ChartModel');
goog.require('p3rf.perfkit.explorer.models.ChartType');

goog.scope(function() {
const explorer = p3rf.perfkit.explorer;
const ChartModel = explorer.models.ChartModel;
const ChartType = explorer.models.ChartType;



/**
 * See module docstring for more information about purpose and usage.
 *
 * @param {function(new:google.visualization.ChartWrapper)} GvizChartWrapper
 * @constructor
 * @ngInject
 */
explorer.components.widget.data_viz.gviz.ChartWrapperService = function($http,
    GvizChartWrapper) {
  /**
   * @private
   */
  this.http_ = $http;

  /**
   * @type {function(new:google.visualization.ChartWrapper)}
   * @private
   */
  this.GvizChartWrapper_ = GvizChartWrapper;

  /**
   * A list of legend alignments for GViz charts.
   * @export {!Array.<!string>}
   */
  this.LEGEND_ALIGNMENTS = [
    'start',
    'center',
    'end'
  ];

  /**
   * A list of legend positions for GViz charts.
   * @export {!Array.<!string>}
   */
  this.LEGEND_POSITIONS = [
    'none',
    'top',
    'right',
    'bottom',
    'left',
    'in'
  ];

  /**
   * An angular-exposed copy of ChartType.
   * @export @enum {string}
   */
  this.CHART_TYPES = explorer.models.ChartType;

  /**
   * @type {Array<{{title: string, className: string}}>
   * @export
   */
  this.allCharts = [];

  this.loadCharts();
};
const ChartWrapperService = (
    explorer.components.widget.data_viz.gviz.ChartWrapperService);


/**
 * Loads a list of available charts from a JSON file.
 */
ChartWrapperService.prototype.loadCharts = function() {
  this.http_.get('/static/components/widget/data_viz/gviz/gviz-charts.json').
      success(angular.bind(this, function(response) {
        $.merge(this.allCharts, response);
      })).
      error(angular.bind(this, function(response) {
        while (this.allCharts.length > 0) {
          this.allCharts.pop();
        }
      }));
};

/**
 * Returns a new instance of a google.visualization.ChartWrapper.
 *
 * @param {?string=} opt_chartType
 * @param {*=} opt_gvizOptions
 * @param {?google.visualization.DataTable=} opt_dataTable
 * @return {google.visualization.ChartWrapper}
 */
ChartWrapperService.prototype.create = function(
    opt_chartType, opt_gvizOptions, opt_dataTable) {
  let chartWrapper = new this.GvizChartWrapper_();
  if (opt_chartType) {
    chartWrapper.setChartType(opt_chartType);
  }
  if (opt_gvizOptions) {
    chartWrapper.setOptions(opt_gvizOptions);
  }
  if (opt_dataTable) {
    chartWrapper.setDataTable(opt_dataTable);
  }
  return chartWrapper;
};


/**
 * Returns a ChartModel object based on the ChartWrapper configuration.
 *
 * @param {google.visualization.ChartWrapper} gvizChartWrapper
 * @return {ChartModel}
 */
ChartWrapperService.prototype.getChartModel = function(gvizChartWrapper) {
  let model = new ChartModel();
  model.chartType = gvizChartWrapper.getChartType();
  model.options = gvizChartWrapper.getOptions();
  return model;
};

});  // goog.scope

@@ -1316,16 +1316,465 @@
 ype;%0A%0A%0A%0A
+/**%0A * Describes the type definition for a chart.%0A * @constructor%0A */%0Aexplorer.components.widget.data_viz.gviz.ChartTypeModel = function() %7B%0A  /**%0A   * Provides a real-world title for the chart.%0A   * @export %7Bstring%7D%0A   */%0A  this.title = '';%0A%0A  /**%0A   * Provides the className (and documentation id) for the chart.%0A   * @export %7Bstring%7D%0A   */%0A  this.className = '';%0A%7D;%0Avar ChartTypeModel = explorer.components.widget.data_viz.gviz.ChartTypeModel;%0A%0A%0A
 /**%0A * S
@@ -2748,71 +2748,216 @@
   * 
-@type %7BArray%3C%7B%7Btitle: string, className: string%7D%7D%3E%0A   * @export
+Provides an ordered list of charts.%0A   * @type %7BArray%3C!ChartTypeModel%3E%0A   * @export%0A   */%0A  this.allCharts = %5B%5D;%0A%0A  /**%0A   * Provides an indexed list of charts.%0A   * @export %7BObject.%3Cstring, !ChartTypeModel%3E%7D
 %0A   
@@ -2971,29 +2971,34 @@
 is.allCharts
+Index
  = 
-%5B%5D
+%7B%7D
 ;%0A%0A  this.lo
@@ -3404,16 +3404,122 @@
 ponse);%0A
+        this.allChartsIndex = this.arrayUtilSvc_.getDictionary(%0A            this.allCharts, 'className');%0A
       %7D)
@@ -3674,16 +3674,17 @@
 ));%0A%7D;%0A%0A
+%0A
 /**%0A * R

Add index of charts by id.