package yi.iu.processor.clc;

import java.awt.*;
import java.util.*;

import yi.iu.processor.color.*;
import yi.iu.processor.ort.*;
import yi.iu.region.*;
import yi.util.config.*;
import yi.util.io.*;
import yi.util.io.input.*;
import yi.util.io.output.*;
import yi.util.log.*;
import yi.ds.*;

/**
 * CLC_LineStruct is used to generate the interrelated and intrarelated line
 * clusters for an image
 */

public class CLC_LineStruct extends CLC_Clustering{

  public final static int POST_MERGE_NONE = 0;
  public final static int POST_MERGE_RECTANGLE = 1;
  public final static int POST_MERGE_POLYGON = 2;

  /**
   * The method used to post merge the line clusters
   */
  public int POST_MERGE_METHOD = POST_MERGE_NONE;

  /**
   * The threshold for post merge
   */
  public double POST_MERGE_THRESHOLD = 0.9;

  public CLC_LineStruct() {
    Configuration.configParameters(this);
  }

  public String[] getParameterNames() {
    return super.getParameterNames(new String[] {
                                   "POST_MERGE_METHOD",
                                   "POST_MERGE_THRESHOLD"

    });
  }

  /**
   * This method should never be called.
   * @param lines
   * @param rgb
   * @param width
   * @param height
   * @return
   */
  public LineClusterSets getClusters(LineClusterSet lines, int[] rgb, int width, int height) {
    String error_msg = "CLC_LineStruct.getClusters(LineClusterSet, ...) is meaningless " +
      " since it doesn't deal with one line cluster directly";
    LogHandler.log(this, LOG_LEVEL, 1, error_msg);
    throw new RuntimeException(error_msg);
  }

  /**
   * Get interrelated and intrarelated line clusters
   * @param line_clusters
   * @param rgb
   * @param width
   * @param height
   * @return
   */
  public LineClusterSets getClusters(LineClusterSets line_clusters, int[] rgb, int width, int height) {

    // first color
    CLC_Clustering clc_clustering = new CLC_Color();
    line_clusters = clc_clustering.getReusableClusteres(line_clusters, rgb, width, height);

    if (DEMO_STEPS) {
      String img_name = Configuration.getStringParameter("SOURCE_NAME",
          "unknown");
      int[] lineimg = line_clusters.demo(width, height);
      OutputResult.outputGIFImage(img_name + ".cclc.gif", lineimg, width,
                                  height);
    }


    // second orientation
    clc_clustering = new yi.iu.processor.clc.CLC_Orientation();
    line_clusters = clc_clustering.getReusableClusteres(line_clusters, rgb, width, height);

    if (DEMO_STEPS) {
      String img_name = Configuration.getStringParameter("SOURCE_NAME",
          "unknown");
      int[] lineimg = line_clusters.demo(width, height);
      OutputResult.outputGIFImage(img_name + ".oclc.gif", lineimg, width,
                                  height);
    }

    // third spatial
    clc_clustering = new yi.iu.processor.clc.CLC_Spatial();
    line_clusters = clc_clustering.getReusableClusteres(line_clusters, rgb, width, height);

    if (DEMO_STEPS) {
      String img_name = Configuration.getStringParameter("SOURCE_NAME",
          "unknown");
      int[] lineimg = line_clusters.demo(width, height);
      OutputResult.outputGIFImage(img_name + ".sclc.gif", lineimg, width,
                                  height);
    }

    // fourth.1 intrarelationship
    clc_clustering = new CLC_Intrarelationship();
    LineClusterSets intra_clusters = clc_clustering.getReusableClusteres(line_clusters, rgb, width, height);

    if (DEMO_STEPS) {
      String img_name = Configuration.getStringParameter("SOURCE_NAME",
          "unknown");
      int[] lineimg = intra_clusters.demo(width, height);
      OutputResult.outputGIFImage(img_name + ".iaclc.gif", lineimg, width,
                                  height);
    }

    // fourth.2 interrelationship
    clc_clustering = new CLC_Interrelationship();
    LineClusterSets inter_clusters = clc_clustering.getReusableClusteres(line_clusters, rgb, width, height);
//    LineClusterSets inter_clusters = clc_clustering.getReusableClusteres(intra_clusters, rgb, width, height);

    if (DEMO_STEPS) {
      String img_name = Configuration.getStringParameter("SOURCE_NAME",
          "unknown");
      int[] lineimg = inter_clusters.demo(width, height);
      OutputResult.outputGIFImage(img_name + ".ieclc.gif", lineimg, width,
                                  height);
    }

    // combine the result
    LineClusterSets combined = new LineClusterSets(width, height);
    combined.add(inter_clusters);
    combined.add(intra_clusters);

    switch (POST_MERGE_METHOD) {
      case POST_MERGE_NONE:
        break;
      case POST_MERGE_RECTANGLE:
        combined = LineClusterSets.mergeThoseBeenCovered(combined,
            POST_MERGE_THRESHOLD);
        break;

      case POST_MERGE_POLYGON:
        combined = LineClusterSets.mergeThoseBeenCovered2(combined,
            POST_MERGE_THRESHOLD);
        break;
    }

    return combined;

  }



}