import java.io.BufferedReader;
//import weka.clusterers.SimpleKMeans;
//import java.io.BufferedWriter;
import java.io.File;
//import java.io.FileNotFoundException;
import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.LinkedList;
import java.util.List;

public class DiswasherRecipe {
		
		public void NOFuzzy(Request request) throws IOException {
			//List to store all database cases
			List<Rule> myList = new ArrayList<Rule>();
			List<Rule> AvailableCase = new ArrayList<Rule>();
			List<Rule> SimilarCase = new ArrayList<Rule>();
			Rule SelectedRule= new Rule(null, null, null, null, 0, 0);
			//Input FILE Data Base
			String filename ="Resources\\RULES.csv";
			BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
			String filename1 ="Resources\\RECIPES.csv";

			String line = null;
			float[] value={0,0,0,0,0,0,0,0,0,0};
			int div=0;
			int mult=0;
			float[] total={0,0,0,0,0,0,0,0,0,0};
			int numRows = 0;
			String nul="null";
			
			//Load every Case
			while ((line = br.readLine()) != null) {
				String[] tmp = line.split(","); /*Splits a string*/
				int eval= Integer.parseInt(tmp[4].trim());
				Rule e = new Rule(tmp[0].trim(), tmp[1].trim(), tmp[2].trim(), tmp[3].trim(),eval, 0);
				try {
					if (tmp.length>4)
					{			
						if((e.Load.compareTo(nul)!=0)&&(e.Type_Load.compareTo(nul)!=0)&&(e.Dirtiness.compareTo(nul)!=0)){
							myList.add(e);
							//System.out.println(e.Load+"\t"+e.Type_Load+"\t"+e.Dirtiness+"\t"+e.Program+"\t"+e.Evaluation+"\t"+e.Similarity);
							}
						}
					}catch (Exception o) {
					System.out.println("Error");
				}
			numRows++;
			}
			//Compare request with cases
			for(int i=0;i<myList.size();i++){
			    //Compare with request
				if((myList.get(i).Load.compareTo(request.Load.trim())==0)||(myList.get(i).Type_Load.compareTo(request.Type_Load.trim())==0)||(myList.get(i).Dirtiness.compareTo(request.Dirtyness.trim())==0))
			    	{
			    	if ((myList.get(i).Load.compareTo(request.Load)==0)&&(myList.get(i).Type_Load.compareTo(request.Type_Load)==0)&&(myList.get(i).Dirtiness.compareTo(request.Dirtyness)==0))
				    	myList.get(i).Similarity=3;
			    	else if(((myList.get(i).Load.compareTo(request.Load)==0)&&(myList.get(i).Type_Load.compareTo(request.Type_Load)==0))||((myList.get(i).Load.compareTo(request.Load)==0)&&(myList.get(i).Dirtiness.compareTo(request.Dirtyness)==0))||((myList.get(i).Type_Load.compareTo(request.Type_Load)==0)&&(myList.get(i).Dirtiness.compareTo(request.Dirtyness)==0)))
			    		myList.get(i).Similarity=2;
			    	else 
			    		myList.get(i).Similarity=1;
			    	}

				//Case selection
				if((myList.get(i).Similarity==3)&&(myList.get(i).Evaluation>=4))
			    {
					AvailableCase.add(myList.get(i));
			    }
				else if((myList.get(i).Similarity>=1)&&(myList.get(i).Evaluation>=4))
			    {
					SimilarCase.add(myList.get(i));
			    }
			}
			
			//Case Available in Database
			if(AvailableCase.size()>0)
			{
				System.out.println("---------------Case available in Database-----------------------");
				for(int i = 0;i<AvailableCase.size();i++)
				{
					if(AvailableCase.get(i).Evaluation>SelectedRule.Evaluation)
						SelectedRule=AvailableCase.get(i);
				}
				System.out.println(SelectedRule.Load+"\t"+SelectedRule.Type_Load+"\t"+SelectedRule.Dirtiness+"\t"+SelectedRule.Program+"\t"+SelectedRule.Evaluation);
				System.out.println("-----"+SelectedRule.Program+" Values--------------");
				BufferedReader br1 = new BufferedReader(new FileReader(new File(filename1)));
				while ((line = br1.readLine()) != null) {
					String[] tmp = line.split(","); /*Splits a string*/
					if(tmp[0].trim().compareTo(SelectedRule.Program)==0)
					{
							for(int j=1; j<tmp.length;j++)
							{
								System.out.println(tmp[j]);
							}
					}
				}
			}
			//Similar Case Available in Database
			else if (SimilarCase.size()>0)
			{
				System.out.println("---------------Case NO available in Database-----------------------");
				System.out.println("---------------Similar Cases-----------------------");
				for(int i = 0;i<SimilarCase.size();i++)
				{
					System.out.println(SimilarCase.get(i).Load+"\t"+SimilarCase.get(i).Type_Load+"\t"+SimilarCase.get(i).Dirtiness+"\t"+SimilarCase.get(i).Program+"\t"+SimilarCase.get(i).Evaluation+"\t"+SimilarCase.get(i).Similarity);
					mult=SimilarCase.get(i).Evaluation*SimilarCase.get(i).Similarity;
					div=div+mult;
					BufferedReader br1 = new BufferedReader(new FileReader(new File(filename1)));
					while ((line = br1.readLine()) != null) {
						String[] tmp = line.split(","); /*Splits a string*/
						if(tmp[0].trim().compareTo(SimilarCase.get(i).Program.toString())==0)
						{
								for(int j=1; j<tmp.length;j++)
								{
									value[j-1]=Integer.parseInt(tmp[j])*mult;
									total[j-1]=total[j-1]+value[j-1];
								}
						}
						
					}
					br1.close();
				}
				System.out.println("---------------New Recipe Parameters-----------------------");
				for(int k=0; k<total.length;k++)
				{
					System.out.println(total[k]/div);
				}
			}
			//Unresolved Request
			else 
				System.out.println("The request can NOT be resolved");
			System.out.println("--------------------------------------------------");
			
			//Process values
			System.out.println("Number of rows in initial file="+numRows);
			System.out.println("Number of rows of the filtered list="+myList.size());
			br.close();

			
		}

}

