/*
 * Copyright (c) 2013 DataTorrent, 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.
 */

package com.datatorrent.demos.visualdata;

import java.util.HashMap;
import java.util.Map.Entry;

import com.google.common.collect.Maps;

/**
 * A util class return the json notation of the schema of the Demo UI Widget
 */
public class WidgetSchemaUtil
{
  
  public static String getSimpleSchema(){
    return "{type:'simple'}";
  }
  
  public static String getTimeseriesSchema(int min, int max)
  {
    return "{type:'timeseries',minValue:" + min + ",maxValue:" + max + "}";
  }

  public static String getPercentageSchema()
  {
    return "{type:'percentage'}";
  }

  public static String getTopNSchema(int n)
  {
    return "{type:'topN',n:" + n + "}";
  }
  
  public static HashMap<String, Number> createTimeSeriesData(long timestamp, Number value){
    HashMap<String, Number> timeseriesMap = Maps.newHashMapWithExpectedSize(2);
    timeseriesMap.put("timestamp", timestamp);
    timeseriesMap.put("value", value);
    return timeseriesMap;
  }

  public static HashMap<String, Object>[] createTopNData(HashMap<String, Number> topNMap)
  {
    
    @SuppressWarnings("unchecked")
    HashMap<String, Object>[] result = new HashMap[topNMap.size()];
    int j = 0;
    for (Entry<String, Number> e : topNMap.entrySet()) {
      result[j] = new HashMap<String, Object>();
      result[j].put("name", e.getKey());
      result[j++].put("value", e.getValue());
    }
    return result;
  }

}

@@ -935,21 +935,27 @@
 n %22%7B
+%5C%22
 type
-:'
+%5C%22:%5C%22
 simple
-'
+%5C%22
 %7D%22;%0A
@@ -1039,22 +1039,27 @@
 eturn %22%7B
+%5C%22
 type
-:'
+%5C%22:%5C%22
 timeseri
@@ -1064,18 +1064,21 @@
 ries
-',
+%5C%22,%5C%22
 minValue
 :%22 +
@@ -1069,24 +1069,26 @@
 %22,%5C%22minValue
+%5C%22
 :%22 + min + %22
@@ -1092,16 +1092,18 @@
 + %22,
+%5C%22
 maxValue
 :%22 +
@@ -1098,16 +1098,18 @@
 maxValue
+%5C%22
 :%22 + max
@@ -1183,22 +1183,27 @@
 eturn %22%7B
+%5C%22
 type
-:'
+%5C%22:%5C%22
 percenta
@@ -1204,17 +1204,18 @@
 rcentage
-'
+%5C%22
 %7D%22;%0A  %7D%0A
@@ -1280,21 +1280,31 @@
 n %22%7B
+%5C%22
 type
-:'topN',n
+%5C%22:%5C%22topN%5C%22,%5C%22n%5C%22
 :%22 +

#MLHR-669 Fix the schema json format