package pizza.presentation.clientserver;

import pizza.service.ServiceException;
import pizza.service.AdminService;
import pizza.service.StudentService;
import pizza.domain.PizzaOrder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import pizza.config.PizzaSystemConfig;
import pizza.domain.*;

public class ShopAdmin  {
	// Commands for admin--
	public static final String INIT = "INIT";
	public static final String AT = "AT";
	public static final String DT = "DT";
	public static final String AS = "AS";
	public static final String DS = "DS";
	public static final String AD = "AD";
	public static final String NR = "NR";
	public static final String CR = "CR";
	public static final String PC = "PC";
	public static final String QS = "QS";
	public static final String DR = "DR";

	private AdminService adminService;
	private StudentService studentService;
	private BufferedReader in;  // the user 

	public ShopAdmin() {
		in = new BufferedReader(new InputStreamReader(System.in));
		
		PizzaSystemConfig.configureServices(); // using Hibernate config
		adminService = PizzaSystemConfig.getAdminService();
		studentService = PizzaSystemConfig.getStudentService();
	}

	public static void main(String[] args) {
		try {
			ShopAdmin admin = new ShopAdmin();
			admin.printCommands();
			admin.executeCommand(admin.getCommand());	
			System.out.println("Command done, exiting");
		} catch (Exception e) {
			System.out.println("Sorry, problem with command: " + e);
		}
	}

	public void printCommands() throws IOException {
		System.out.println("Enter one of the following commands:");
		System.out.println(INIT + ": Initialize Database");
		System.out.println(AT + ": Add new Topping");
		System.out.println(DT + ": Delete Topping");
		System.out.println(AS + ": Add new pizza Size");
		System.out.println(DS + ": Delete pizza Size");
		System.out.println(AD + ": Advance the Day");
		System.out.println(NR + ": make Next order Ready");
		System.out.println(CR + ": College Report since last report");
		System.out.println(DR + ": Daily Report");
		System.out.println(QS + ": Quit System");
		System.out.println(PC + ": Print list of Commands");
	}

	public String getCommand() throws IOException {
		return PresentationUtils.readEntry(in, "Please Enter The Command");
	}

	public void executeCommand(String command) throws IOException, ServiceException {
		if (command.equalsIgnoreCase(INIT))
			adminService.initializeDb();
		else if (command.equalsIgnoreCase(AT))
			adminService.addTopping(PresentationUtils.readEntry(in,"Enter the topping Name"));
		else if (command.equalsIgnoreCase(DT)) {
			String toppingName = PresentationUtils.readEntry(in, "Enter the topping name");
			Topping top = studentService.getToppingByName(toppingName);
			adminService.deleteTopping(top.getId());
		}
		else if (command.equalsIgnoreCase(AS))
			adminService.addPizzaSize( PresentationUtils.readEntry(in, "Enter the size name"));
		else if ( command.equalsIgnoreCase(DS)) {
			String sizeName = PresentationUtils.readEntry(in, "Enter the size name");
			PizzaSize size = studentService.getPizzaSizeByName(sizeName);
			adminService.deletePizzaSize( size.getId());
		}
		else if ( command.equalsIgnoreCase(AD) )
			adminService.advanceDay();
		else if ( command.equalsIgnoreCase(NR))
			adminService.markNextOrderReady();
		else if ( command.equalsIgnoreCase(CR) ) {
			List<PizzaOrder> report = adminService.getAdminReport();
			PresentationUtils.printReport(report, System.out);
		} else if ( command.equalsIgnoreCase(DR)) {
			List<PizzaOrder> report = adminService.getDailyReport();
			PresentationUtils.printReport(report, System.out);
		}
		else if(command.equalsIgnoreCase(QS))
			return;
		else if(command.equalsIgnoreCase(PC))
			printCommands();
		else
			System.out.println("\nInvalid Command!");
	}

}

