package managedbean.common;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSeparator;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuElement;
import org.primefaces.model.menu.MenuModel;

import common.UserType;

/**
 * 
 * @author Marcos García Núñez (mgarcianun@uoc.edu)
 *
 */
@Named("menuView")
@RequestScoped
public class MenuMBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private MenuModel model;

	@PostConstruct
	public void init() {
		boolean loggedIn = SessionUtils.isLogedIn();

		model = new DefaultMenuModel();
		DefaultSubMenu subMenu;
		UserType tipoUsuario = null;
		DefaultMenuItem menuItem;

		model.addElement(createMenuItem("Home", "pi pi-home", "/home", null));

		if (loggedIn == true) {
			tipoUsuario = SessionUtils.getUserType();

			// Administracion Sistema
			if (tipoUsuario == UserType.ADMINISTRATOR) {
				subMenu = new DefaultSubMenu("Administración del sistema", "fa fa-wrench");

				subMenu.addElement(createMenuItem("Gestionar especialidades", "fa fa-file-text-o", "/systemAdmin/ManageSpecialties", null));
				subMenu.addElement(createMenuItem("Centros At. Primaria", "fa fa-hospital-o", "/systemAdmin/ManageHealthCareCenters", null));
				subMenu.addElement(new DefaultSeparator());
				subMenu.addElement(createMenuItem("Ver médicos de un CAP", "fa fa-medkit", "/systemAdmin/ListDoctorsByCenter", null));
				subMenu.addElement(new DefaultSeparator());
				subMenu.addElement(createMenuItem("Añadir usuario Admin", "fa fa-user-secret", "/systemAdmin/AddAdmin", null));

				model.addElement(subMenu);
			}

			// Visitas
			if (tipoUsuario == UserType.PATIENT || tipoUsuario == UserType.ADMINISTRATOR || tipoUsuario == UserType.FAMILY_DOCTOR) {
				subMenu = new DefaultSubMenu("Visitas", "fa fa-calendar");

				if (tipoUsuario == UserType.PATIENT)
					subMenu.addElement(createMenuItem("Solicitar nueva", "fa fa-calendar-o", "/visit/AddVisit", null));
				if (tipoUsuario == UserType.PATIENT || tipoUsuario == UserType.ADMINISTRATOR)
					subMenu.addElement(createMenuItem("Modificar fecha/hora", "fa fa-briefcase", "/visit/PatientVisitList", null));
				if (tipoUsuario == UserType.PATIENT)
					subMenu.addElement(createMenuItem("Anular visita", "fa fa-remove", "/visit/PatientVisitList", null));
				if (tipoUsuario == UserType.FAMILY_DOCTOR)
					subMenu.addElement(createMenuItem("Añadir resultado", "fa fa-align-left", "/visit/VisitList", null));
				if (tipoUsuario == UserType.FAMILY_DOCTOR)
					subMenu.addElement(createMenuItem("Ver agenda del día", "fa fa-repeat", "/visit/VisitList", null));

				model.addElement(subMenu);
			}

			// Pruebas médicas
			if (tipoUsuario == UserType.PATIENT || tipoUsuario == UserType.SPECIALIST_DOCTOR || tipoUsuario == UserType.FAMILY_DOCTOR) {
				subMenu = new DefaultSubMenu("Especialista", "fa fa-heartbeat");

				if (tipoUsuario == UserType.SPECIALIST_DOCTOR)
					subMenu.addElement(createMenuItem("Añadir prueba", "fa fa-server", "/medicaltest/MedicalTests", null));

				// Todos pueden consultar (P S F)
				subMenu.addElement(createMenuItem("Consultar prueba", "fa fa-search", "/medicaltest/MedicalTests", null));
				subMenu.addElement(new DefaultSeparator());

				if (tipoUsuario == UserType.SPECIALIST_DOCTOR) {
					subMenu.addElement(new DefaultSeparator());
					subMenu.addElement(createMenuItem("Gestionar imagen de una prueba", "fa fa-briefcase", "/medicaltest/MedicalTests", null));
				}

				if (tipoUsuario == UserType.PATIENT)
					subMenu.addElement(createMenuItem("Buscar especialista...", "fa fa-heartbeat", "/medicaltest/SearchSpecialist", null));

				model.addElement(subMenu);
			}

			// Preguntas médicas
			if (tipoUsuario == UserType.PATIENT || tipoUsuario == UserType.FAMILY_DOCTOR) {
				model.addElement(createMenuItem("Preguntas", "fa fa-comment-o", "/medicaltest/Questions", null));
			}
		}

		// Perfil
		subMenu = new DefaultSubMenu("Gestionar perfil", "fa fa-gears");
		if (loggedIn == false) {
			subMenu.addElement(createMenuItem("Registro de usuario", "fa fa-user-plus", "/profile/RegisterUser", null));
			model.addElement(subMenu);
		} else {
			if (tipoUsuario != UserType.ADMINISTRATOR) {
				subMenu.addElement(createMenuItem("Modificar datos personales", "fa fa-user", "/profile/UpdateProfile", null));
				subMenu.addElement(new DefaultSeparator());

				if (tipoUsuario == UserType.PATIENT) {
					subMenu.addElement(createMenuItem("Cambiar médico de familia", "fa fa-user-md", "/profile/ChangeFamilyDoctor", null));
					subMenu.addElement(new DefaultSeparator());
				}

				if (tipoUsuario == UserType.FAMILY_DOCTOR) {
					subMenu.addElement(createMenuItem("Cambiar CAP", "fa fa-h-square", "/profile/ChangePrimaryHealthCareCenter", null));
				}

				model.addElement(subMenu);
			}
		}

		// Opciones de menu a la derecha (login, logout, registrase y descripción del usuario logeado
		if (loggedIn == true) {
			menuItem = createMenuItem("Salir", "pi pi-sign-out", null, "#{loginView.logout}");
			menuItem.setContainerStyle("float: right;");
			model.addElement(menuItem);

			menuItem = createMenuItem("Bienvenido, " + SessionUtils.getUserDisplayName(), null, "/profile/UpdateProfile", null);
			menuItem.setContainerStyle("float: right;");
			model.addElement(menuItem);
		} else {
			menuItem = createMenuItem("Entrar", "pi pi-sign-in", null, null);
			menuItem.setOnclick("PF('dlgLogin').show();");
			menuItem.setContainerStyle("float: right;");
			model.addElement(menuItem);
			
			menuItem = createMenuItem("Registrarse", "pi pi-user-plus", "/profile/RegisterUser", null);
			menuItem.setContainerStyle("float: right;");
			model.addElement(menuItem);
			
			menuItem = createMenuItem("hola, invitado", null, null, null);
			menuItem.setContainerStyle("float: right;");
			model.addElement(menuItem);
		}
	}

	private DefaultMenuItem createMenuItem(String name, String icon, String outcome, String command) {
		DefaultMenuItem item = new DefaultMenuItem(name, icon);
		if (outcome != null)
			item.setOutcome(outcome);
		if (command != null)
			item.setCommand(command);

		return item;
	}

	public MenuModel getModel() {
		return model;
	}
}