package Item;

import java.util.ArrayList;


/**
 * 
 * This Meal class is created by the use of a pattern given in the constructor of
 * subClasses, this pattern must consist on an array, each case contain an obligatory
 * typeOfMeal (starter,mainDish...), if the user have the choice between two typeOfMeal
 * it can be implemented by using a subArray.
 * For exemple, the patterns below show how a full meal may be created and then a half meal :
 * fullMealPattern={{"Starter"},{"MainDish"},{"Dessert"}}
 * halfMealPattern={{"Starter","Dessert"},{"MainDish"}}
 * 
 * We may create a children meal very easily :
 * chlidrenMealPattern={{"MainDish"},{"Dessert"},{"Drink"}}
 *
 */
public abstract class Meal extends Item implements VisitorPrice{
	
	ArrayList<Dish> listDish = new ArrayList<Dish>();
	String [][] patternTypeInMeal;
	
	private double price=0;
	
	/** CONSTRUCTORS **/
	
	public Meal(String name,String typeOfFood) {
		super(name,typeOfFood);
	}
	
	/** add and remove dishes **/
	
	public void addDish(Dish dish){
		if(dish.getTypeOfFood() != this.getTypeOfFood()){
			System.out.println("Trying to add "+dish.getTypeOfFood()+" in "+this.getTypeOfFood()+" menu, resulted in error.");
		}else{
			int idInMenu=-1;
			int idInMenuInc=0;
			for(String[] tabOfType : this.getPatternTypeInMeal()){
				for(String typeFromPatternType : tabOfType){
					if(typeFromPatternType==dish.getTypeInMeal()){
						idInMenu=idInMenuInc;
					}
				}
				idInMenuInc+=1;
			}
			if(idInMenu>-1){
				if(listDish.get(idInMenu)==null){
					listDish.set(idInMenu, dish);
				}
				else{
					if(Cli.Confirm.text("This dish : "+listDish.get(idInMenu).toString()+" exists already. Do you want to replace it ?))")){
						listDish.remove(idInMenu);
						listDish.add(idInMenu, dish);
					}
				}
			}
		}
	}
	
	public void removeDish(String name){
		boolean itemRemoved = false;
		for(Dish dish: listDish){
			if(dish.getName()==name){
				listDish.remove(dish);
				itemRemoved=true;
			}
		}
		if(itemRemoved)
			System.out.println("Dish removed !");
	}
	public void removeDish(int num){
		Dish dish = listDish.get(num);
		if(dish.getName()==name)
			if(!listDish.contains(dish))
				listDish.add(dish);
			else{
				System.out.println("This dish is in this menu.");
				if(Cli.Confirm.text("Do you really want to add it ?"))
					listDish.add(dish);
			}
	}
	
	/** GETTER AND SETTER **/
	
	public double getPrice(){
		return price;
	}
	public ArrayList<Dish> getListDish() {
		return listDish;
	}
	public String[][] getPatternTypeInMeal() {
		return patternTypeInMeal;
	}

	protected void setPatternTypeInMeal(String[][] patternTypeInMeal) {
		this.patternTypeInMeal = patternTypeInMeal;
	}

	/** VISIT LOGIC **/

	@Override	
	public double acceptPrice(VisitorPrice v){
		price=0;
		for(Dish item: listDish){
			item.acceptPrice(this);
		}
		return this.getPrice();
	}

	@Override
	public double visitPrice(VisitablePrice v) {
		return v.acceptPrice(this);
	}
}