package net.woodyfolsom.cs6601.p2;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import net.woodyfolsom.cs6601.p2.Ingredient.TYPE;

import org.apache.commons.math3.stat.regression.SimpleRegression;

import dkohl.bayes.bayesnet.BayesNet;
import dkohl.bayes.builders.FoodNetBuilder;
import dkohl.bayes.inference.EnumerateAll;
import dkohl.bayes.probability.Assignment;
import dkohl.bayes.probability.ProbabilityAssignment;
import dkohl.bayes.probability.Variable;

public class BayesChef {

	public static void main(String... args) {
		System.out.println("Reading recipe book.");
		RecipeBook recipeBook = RecipeBookReader.readRecipeBook(new File("data/short_recipebook.xml"));
		
		System.out.println("Reading survey data.");
		Survey survey = SurveyReader.readSurvey(new File("data/short_survey.xml"));
		
		new BayesChef().reportBestMeal(recipeBook,survey);
	}
	
	private void reportBestMeal(RecipeBook recipeBook, Survey survey) {
		int numRecipes = recipeBook.getSize();
		System.out.println("Read data for " + numRecipes + " recipes.");
		
		int numDiners = survey.getDinerCount();
		System.out.println("Read data for " + numDiners + " diner(s).");
		
		System.out.println("Creating Bayes net for survey dataset...");
		BayesNet net = FoodNetBuilder.createDishNet(survey, recipeBook);
		
		System.out.println("Querying Bayes net for individual flavor preferences: ");
		double[][] flavorPrefs = new double[4][];
		
		flavorPrefs[0] = new double[] {0.0,printPreference(net, TYPE.PORK)};
		flavorPrefs[1] = new double[] {1.0,printPreference(net, TYPE.BEEF)};
		flavorPrefs[2] = new double[] {2.0,printPreference(net, TYPE.POTATO)};
		flavorPrefs[3] = new double[] {3.0,printPreference(net, TYPE.TOMATO)};
		
		SimpleRegression simpleRegression = new SimpleRegression();	
		simpleRegression.addData(flavorPrefs);
		
		System.out.println("Individual flavor pref MSE: " + simpleRegression.getMeanSquareError());
		simpleRegression.clear();
		
		System.out.println("Querying Bayes net for recipe flavor preferences: ");
		flavorPrefs = new double[recipeBook.getSize()][];
		
		for (int i = 0; i < recipeBook.getSize(); i++) {
			simpleRegression.addData(i,printPreference(net, recipeBook.getRecipe(i)));
		}
		System.out.println("Recipe flavor pref MSE: " + simpleRegression.getMeanSquareError());
	}
	
	int printPreference(BayesNet net, Recipe recipe) {
		Set<TYPE> ingredientTypes = recipe.getIngredients().getTypes();
		
		List<ProbabilityAssignment> probs = EnumerateAll.enumerateAsk(new Variable(
				FoodNetBuilder.TASTE, FoodNetBuilder.RATING_DOMAIN),
				net, createQuery(ingredientTypes.toArray(new TYPE[ingredientTypes.size()])));
		
		double max_val = 0.0;
		String max_arg = "N/A";
		for (ProbabilityAssignment p : probs) {
			if (p.getProbability() > max_val) {
				max_val = p.getProbability();
				max_arg = p.getValue();
			}
		}

		System.out.println("TASTE for " + recipe.getName() + " " + ingredientTypes + " : " + max_arg + " " + max_val);
		return Integer.valueOf(max_arg);
	}
	
	int printPreference(BayesNet net, TYPE type) {
		List<ProbabilityAssignment> probs = EnumerateAll.enumerateAsk(new Variable(
				FoodNetBuilder.TASTE, FoodNetBuilder.RATING_DOMAIN),
				net, createQuery(type));
		double max_val = 0.0;
		String max_arg = "N/A";
		for (ProbabilityAssignment p : probs) {
			if (p.getProbability() > max_val) {
				max_val = p.getProbability();
				max_arg = p.getValue();
			}
		}

		System.out.println("TASTE " + type + ": " + max_arg + " " + max_val);
		return Integer.valueOf(max_arg);
	}
	
	List<Assignment> createQuery(TYPE... types) {
		List<Assignment> assignment = new LinkedList<Assignment>();
		for (TYPE type : types) {
		switch (type) {
		case PORK:
		case BEEF:
		case POTATO:
		case TOMATO:
			assignment.add(new Assignment(new Variable(type.toString(), FoodNetBuilder.DOMAIN),
					FoodNetBuilder.TRUE_VALUE));
			break;
		default:
		}
		}
		return assignment;
	}
}
