package yi.ds.hash;

import yi.ds.LinkedListHashMap;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.util.List;

/**
 * The values in the hashmap is a linked list of double array.  One
 * functionality is to draw those data in color.  Each double array will be
 * treated as a (H,S,B)
 */

public class DoubleArrayListMap extends LinkedListHashMap {
  public DoubleArrayListMap() {
  }

  public DoubleArrayListMap(HashMap map) {
    for (Iterator i=map.keySet().iterator(); i.hasNext();) {
      Object k = i.next();
      put(k, map.get(k));
    }
  }

  /**
   * Each double array is treated as HSB, then the array list will be draw
   * as a horizontal color blocks.  Each entry in the map will be draw as a row.
   * @param d to return the dimension of the drawn iamge
   * @param block_width
   * @param block_height
   * @return
   */
  public int[] drawDataAsRGB(Dimension d, int block_width, int block_height) {

    // sort the keys first
    ArrayList objs = new ArrayList(keySet());
    Collections.sort(objs);
    // to save the colors of color blocks
    int[][] data = new int[objs.size()][]; // means in color
    // to get the length of the array list
    int iteration_num = 0;

    // loop through each entry
    int i_index = 0;
    for (Iterator i = objs.iterator(); i.hasNext(); i_index++) {
      // get the array list
      java.util.List array_list = (java.util.List) get(i.next());
      // save the length of the array list
      if (array_list.size() > iteration_num) {
        iteration_num = array_list.size();
      }
      // save the data
      data[i_index] = new int[iteration_num];
      int j_index = 0;
      for (Iterator j = array_list.iterator(); j.hasNext(); j_index++) {
        double[] mean = (double[]) j.next();
        // convert to RGB color
        Color color = new Color(Color.HSBtoRGB( (float) mean[0], (float) mean[1],
                                               (float) mean[2]));
        data[i_index][j_index] = color.getRGB();
      }
    }

    d.height = objs.size() * block_height;
    d.width = iteration_num * block_width;
    return yi.iu.processor.color.ColorUtil.createSpectrum(data, block_width, block_height);
  }

  public String toString() {
    StringWriter str = new StringWriter();
    PrintWriter out = new PrintWriter(str);

    ArrayList keys = new ArrayList(keySet());
    Collections.sort(keys);
    for (Iterator i = keys.iterator(); i.hasNext(); ) {
      Object key = i.next();
      out.print(key.toString() + "->{");
      List list = (List) get(key);
      boolean first = true;
      for (Iterator j = list.iterator(); j.hasNext(); ) {
        if (first) {
          first = false;
        }
        else {
          out.print(";");
        }
        double[] data = (double[]) j.next();
        out.print("[");
        for (int k=0; k<data.length; k++) {
          if (k!=0) out.print(",");
          out.print(data[k]);
        }
        out.print("]");
      }
      out.println("}");
    }
    return str.toString();
  }

}