package GUI;

import java.util.Arrays;
import java.util.ArrayList;

import round.Roundness;

public class Result {
	
	private static double heightTextLine;
	private static double[] diffLines;
	private static double[] diffWords;
	private static double[] heightHandwr;
	private static double[] densityHandwr;//important
	private static double[] ratioWordsSegments; //important
	private static double[] ratioLinesWord;
	private static double[] threeElementsChar1, threeElementsChar2, threeElementsChar3;
	private static double[] ratioThreeElementsChar1, ratioThreeElementsChar2, ratioThreeElementsChar3;	//important
	private static double[] ratioLinesHeight;//important
	private static double[] slant;//important
	
	private static int densityHandwrPerc = 10;//important
	private static int ratioWordsSegmentsPerc = 18; //important
	private static int ratioThreeElementsChar1Perc = 18;//important
	private static int ratioThreeElementsChar2Perc = 18;//important
	private static int ratioThreeElementsChar3Perc = 18;	//important
	private static int ratioLinesHeightPerc = 18;//important
	private static int ratioLinesWordPerc = 18;//important
	private static int slantPerc = 18;//important
	private static int diffLinesPerc = 18;//important
	private static int heightHandwrPerc = 18;//important
	
	
	//private static double[] ratioWordsSegments; //important
	//private static double[] ratioLinesHeight;//important

	//skontroluje ci dany obrazok bol napisany rovnakou osobou
	public static String checkImage() {
		String s = "";
		int num = 0;
		System.out.println("SLANT");
		if (!oneImageSame(slant, 0.2)) {
			s = s.concat(" Slant ");
			num++;
		}
		System.out.println("DENSITY");
		if (!oneImageSame(densityHandwr, 400)) {
			s = s.concat(" Density ");
			num++;
		}
		System.out.println("RATIO1");
		if (!oneImageSame(ratioThreeElementsChar1, 30)) {
			s = s.concat(" Proportion1 ");
			num++;
		}
		System.out.println("RATIO2");
		if (!oneImageSame(ratioThreeElementsChar2, 30)) {
			s = s.concat(" Proportion2 ");
			num++;
		}
		System.out.println("RATIO3");
		if (!oneImageSame(ratioThreeElementsChar3, 30)) {
			s = s.concat(" Propotion3 ");
			num++;
		}
		System.out.println("BLOCKLETTERS");
		if (!oneImageSame(ratioWordsSegments, 0.7)) {
			s = s.concat(" Block letters ");
			num++;
		}
		s = Integer.toString(num).concat(s);
/*		System.out.println("Ratio lines/height");
		if (!oneImageSame(ratioLinesHeight, 0.6)) {
			s = s.concat(" Ratio lines/height ");
		}*/
		return s;
	}
	
	//vrati true ak je obrazok napisany rovnakou osobou, false ak nie
	private static boolean oneImageSame(double[] input, double threshold){
		
		Arrays.sort(input);
		for (int i = 0; i < input.length; i++) {
			System.out.println("input "+input[i]);
		}
		
		double[] dif = new double[input.length];
		int s = 0;
		for (int i = 0; i < input.length; i++) {
			if (i != input.length / 2) {
				dif[i] = Math.abs(input[input.length / 2] - input[i]);
				System.out.println("dif input "+dif[i]);
			}
		}
		
/*		ArrayList newInput = new ArrayList();
		for (int i = 0; i < dif.length; i++) {
			if (dif[i] < 1) {
				newInput.add(input[i]);
				System.out.println("new input[i] "+input[i]);
			}
		}*/		
		
		//if ()
		if ( (dif[1] > threshold) || (dif[dif.length - 2] > threshold) )
			return false;
		else
			return true;

	}	
	
	private static double changeArray(double[] input) {
		
		Arrays.sort(input);
		//for (int i = 0; i < input.length; i++) {
		//	System.out.println("input "+input[i]);
		//}
		
		double[] dif = new double[input.length];
		int s = 0;
		for (int i = 0; i < input.length; i++) {
			if (i != input.length / 2) {
				dif[i] = Math.abs(input[input.length / 2] - input[i]);
				//System.out.println("dif input "+dif[s]);
			}
		}
		
		ArrayList newInput = new ArrayList();
		for (int i = 0; i < dif.length; i++) {
			if (dif[i] < 1) 
				newInput.add(input[i]);
		}
		
/*		double[] returnInput = new double[newInput.size()];
		for (int i = 0; i < returnInput.length; i++) {
			returnInput[i] = Roundness.roundTwoDecimal((Double)(newInput.get(i)));
		}*/
		
	//	return returnInput;
		
		double average = 0;
		for (int i = 0; i < newInput.size(); i++) {
			average = average + (Double)(newInput.get(i));
		}
		
		average = average / newInput.size();
		
		average = Roundness.roundTwoDecimal(average);
	//	System.out.println("average "+average);
		
		return average;
	}
	
	public static int getHeightHandwrPerc(){
		return heightHandwrPerc;
	}	

	public static int getRatioLinesWordPerc(){
		return ratioLinesWordPerc;
	}	
	
	public static int getDiffLinesPerc(){
		return diffLinesPerc;
	}	
	
	public static int getSlantPerc(){
		return slantPerc;
	}	
	
	public static int getDensityHandwrPerc(){
		return densityHandwrPerc;
	}
	
	public static int getRatioWordsSegmentsPerc(){
		return ratioWordsSegmentsPerc;
	}
	
	public static int getRatioThreeElementsChar1Perc(){
		return ratioThreeElementsChar1Perc;
	}
	
	public static int getRatioThreeElementsChar2Perc(){
		return ratioThreeElementsChar2Perc;
	}
	
	public static int getRatioThreeElementsChar3Perc(){
		return ratioThreeElementsChar3Perc;
	}
	
	public static int getRatioLinesHeightPerc(){
		return ratioLinesHeightPerc;
	}	
	
	public static double getHeightTextLine(){
		return heightTextLine;
	}		
	
	public static double getSlant(){
		return changeArray(slant);
	}	
	
	public static double getHeightHandwr(){
		return changeArray(heightHandwr);
	}	
	
	public static double getDiffLines(){
		return changeArray(diffLines);
	}	
	
	public static double[] getDiffLinesArray(){
		return diffLines;
	}		
	
	public static double getDensityHandwr(){
		return changeArray(densityHandwr);
	}
	
	public static double[] getDensityHandwrArray(){
		return densityHandwr;
	}	
	
	public static double getrRatioWordsSegments(){
		return changeArray(ratioWordsSegments);
	}
	
	public static double getRatioThreeElementsChar1(){
		return changeArray(ratioThreeElementsChar1);
	}
	
	public static double getRatioThreeElementsChar2(){
		return changeArray(ratioThreeElementsChar2);
	}	
	
	public static double getRatioThreeElementsChar3(){
		return changeArray(ratioThreeElementsChar3);
	}	
	
	public static double getRatioLinesHeight(){
	//	System.out.println("changeArray(diffLines) "+changeArray(diffLines));
	//	System.out.println("changeArray(heightHandwr) "+changeArray(heightHandwr));
		return Roundness.roundTwoDecimal((changeArray(diffLines) / changeArray(heightHandwr)));
	}
		
	
	public static double getRatioLinesWord(){
			//System.out.println("changeArray(diffLines) "+changeArray(diffLines));
			//System.out.println("changeArray(diffWords) "+changeArray(diffWords));
			return Roundness.roundTwoDecimal((changeArray(diffLines) / changeArray(diffWords)));
		}	
	
	
	public static void setSlant(double[] input, int numberResult){
		slant = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			slant[i] = input[i];
	//		System.out.println("slant[i] "+slant[i]);
		}
	}
	
	public static void setDiffLines(double[] input, int numberResult){
	//	System.out.println("numberResult "+numberResult);
		diffLines = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			diffLines[i] = input[i];
		}
/*		for (int i = 0; i < diffLines.length; i++) {
			mainWindow.intFrameResult[numberResult].cbDiffLines.addItem(Roundness.roundTwoDecimal((Double)(diffLines[i])));			
		}*/

	}
	
	public static void setDiffWords(double[] input, int numberResult){
		diffWords = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			diffWords[i] = input[i];
		}
/*		for (int i = 0; i < diffWords.length; i++) {
			mainWindow.intFrameResult[numberResult].cbDiffWords.addItem(Roundness.roundTwoDecimal((Double)(diffWords[i])));			
		}*/

	}	
	
	public static void setDensityHandwr(double[] input, int numberResult){
		densityHandwr = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			densityHandwr[i] = input[i];
		}
/*		for (int i = 0; i < densityHandwr.length; i++) {
			mainWindow.intFrameResult[numberResult].cbDensityHandwr.addItem(Roundness.roundTwoDecimal((Double)(densityHandwr[i])));			
		}*/

	}	
	
	public static void setHeightHandwr(double[] input, int numberResult){
		heightHandwr = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			heightHandwr[i] = input[i];
		}
/*		for (int i = 0; i < heightHandwr.length; i++) {
			mainWindow.intFrameResult[numberResult].cbHeightHandwr.addItem(Roundness.roundTwoDecimal((Double)(heightHandwr[i])));			
		}*/

	}		

	public static void setRatioWordsSegments(double[] input, int numberResult){
		ratioWordsSegments = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			ratioWordsSegments[i] = input[i];
		}
/*		for (int i = 0; i < ratioWordsSegments.length; i++) {
			mainWindow.intFrameResult[numberResult].cbRatioWordsSegments.addItem(Roundness.roundTwoDecimal((Double)(ratioWordsSegments[i])));			
		}*/

	}
	
	public static void setThreeElementsChar1(double[] input, int numberResult){
		threeElementsChar1 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			threeElementsChar1[i] = input[i];
		}
/*		for (int i = 0; i < threeElementsChar1.length; i++) {
			mainWindow.intFrameResult[numberResult].cbthreeElementsChar1.addItem(Roundness.roundTwoDecimal((Double)(threeElementsChar1[i])));			
		}*/

	}	
	
	public static void setThreeElementsChar2(double[] input, int numberResult){
		threeElementsChar2 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			threeElementsChar2[i] = input[i];
		}
/*		for (int i = 0; i < threeElementsChar2.length; i++) {
			mainWindow.intFrameResult[numberResult].cbthreeElementsChar2.addItem(Roundness.roundTwoDecimal((Double)(threeElementsChar2[i])));			
		}*/

	}	
	
	public static void setThreeElementsChar3(double[] input, int numberResult){
		threeElementsChar3 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			threeElementsChar3[i] = input[i];
		}
/*		for (int i = 0; i < threeElementsChar3.length; i++) {
			mainWindow.intFrameResult[numberResult].cbthreeElementsChar3.addItem(Roundness.roundTwoDecimal((Double)(threeElementsChar3[i])));			
		}*/

	}	
	
	public static void setRatioThreeElementsChar2(double[] input, int numberResult){
		ratioThreeElementsChar2 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			ratioThreeElementsChar2[i] = input[i];
		}
/*		for (int i = 0; i < ratioThreeElementsChar2.length; i++) {
			mainWindow.intFrameResult[numberResult].cbRatioThreeElementsChar2.addItem(Roundness.roundTwoDecimal((Double)(ratioThreeElementsChar2[i])));			
		}*/

	}
	
	public static void setRatioThreeElementsChar3(double[] input, int numberResult){
		ratioThreeElementsChar3 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			ratioThreeElementsChar3[i] = input[i];
		}
/*		for (int i = 0; i < ratioThreeElementsChar3.length; i++) {
			mainWindow.intFrameResult[numberResult].cbRatioThreeElementsChar3.addItem(Roundness.roundTwoDecimal((Double)(ratioThreeElementsChar3[i])));			
		}*/

	}	
	
	public static void setRatioThreeElementsChar1(double[] input, int numberResult){
		ratioThreeElementsChar1 = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			ratioThreeElementsChar1[i] = input[i];
		}
/*		for (int i = 0; i < ratioThreeElementsChar1.length; i++) {
			mainWindow.intFrameResult[numberResult].cbRatioThreeElementsChar1.addItem(Roundness.roundTwoDecimal((Double)(ratioThreeElementsChar1[i])));			
		}*/

	}	
	
	public static void setHeightTextLine(double input, int numberResult){
		heightTextLine = input;
	}		
	
/*	public static void setRatioLinesHeight(double[] input, int numberResult){
		ratioLinesHeight = new double[input.length];
		for (int i = 0; i < input.length; i++) {
			ratioLinesHeight[i] = input[i];
		}
		for (int i = 0; i < ratioLinesHeight.length; i++) {
			mainWindow.intFrameResult[numberResult].cbRatioLinesHeight.addItem(Roundness.roundTwoDecimal((Double)(ratioLinesHeight[i])));			
		}

	}*/	
}
