import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.lang.Math;
import java.awt.Color;

public class DominantColor {
	public static final int MPEG7=1;
	public static final int ALTERNATIVE=2;
	
	public static VectorSet getDominantColor(BufferedImage input, int method, double dcolors) {
		VectorSet vs = new VectorSet(); // object for random vectors and domimant colors aftewards
		Vector vTemp = new Vector(); // temporary vector - test
		vTemp.generate(3);
		if (method==MPEG7) {						
			for (int i=0; i<dcolors; i++) {
				Vector v = new Vector();
				v.generate(3);
				v.setRandomHSV();
				vs.add(v);
			}
				
			double distance; // distance of random and pixel color vectors
			double min = 1000; // maximum distance of five vectors is cca 570, for HSV it is 386
			ArrayList<Cluster> clusterSet = new ArrayList<Cluster>();
			for (int i=0;i<dcolors;i++) {
				Cluster cluster = new Cluster();
				clusterSet.add(cluster);
			}
			int clusternumber = 0;
			
			for (int i=0; i<input.getWidth(); i++) {
				for(int j=0; j<input.getHeight(); j++) {
					for (int k=0; k<dcolors; k++) {
						int r = new Color(input.getRGB(i, j)).getRed();
						int g = new Color(input.getRGB(i, j)).getGreen();
						int b = new Color(input.getRGB(i, j)).getBlue();
						vTemp.set(r, g, b);
						vTemp.convertTo(Main.HSV);

						distance = vTemp.distance(vs.getVector(k));
						if (distance<min) {
							min = distance;
							clusternumber = k;
						}
					}
					clusterSet.get(clusternumber).addVector(vTemp);
					vs.setVector(clusternumber, clusterSet.get(clusternumber).getAverage());

				}
			}		
		} // end of if
		else if (method==ALTERNATIVE) {
			int delta = 4; // position difference on histogram
			int windowSize = 16; // size of window (number of similar colors)
			HistogramSet hs = new HistogramSet();
			Histogram h = new Histogram();
			double temp = 0; // initalizing of temporary values in histogram			
			ArrayList<Integer> pos = new ArrayList<Integer>(); // position of maximum value of histogram
			ArrayList<Double> max = new ArrayList<Double>() ;// maximum value of histogram
			ArrayList<Double> window = new ArrayList<Double>(); // preparing for window distribution
			for (int i=0;i<windowSize;i++) { // filling window: uniform distribution
				window.add((double) 1);
			}
			for (int i=0;i<dcolors;i++) { // filling max and positions by zeros
				max.add(-1.0);
				pos.add(-1);
			}
			hs = ScalableColor.getHistogram(input, Main.HSVDC);
			for (int i=0;i<dcolors;i++) { // calculate all dominant colors
				for (int j=0;j<hs.getLength();j++) { // go through all chrominance component
					h = hs.get(j);
					outer: for (int k=0;k<h.getLength()-window.size();k++) { // go through whole histogram
						if (i > 0) { // avoid posDelta-jumping in the first iteration
							for (int m=0; m<=i;m++) {
								if (pos.get(m) != -1) {
									if (Math.abs(k - pos.get(m)) < delta) { // are position difference bigger then posDelta?
										continue outer; // skip to next k
									}
								}
							}
						}
						temp=0;
						for (int l=0;l<window.size();l++) { // multiple all values in window
							temp+=h.getValue(k+l)*window.get(l); // multypling values
						}
						for (int l=0;l<=i;l++) {
							if (temp>max.get(l) && !max.contains(temp)) { // are values on maximum? are the values different (that contains thing)?
								max.set(l, temp); // save these values
								pos.set(l, k); // save the position of max values
							}
						}
						System.out.println(max); // check of max values
						System.out.println(pos); // check of positions
					}
					for (int l=0;l<dcolors;l++) {
						vTemp.set(h.getValue(pos.get(l)+windowSize/2), j); // get position of most occured color(hue) and put in vector
					}
				}
				vs.add(vTemp);
			} // end of last for			 	
		}	// end of if
		return vs;

	} // end of method
} // end of class
