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.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() {
		model = new DefaultMenuModel();
		DefaultSubMenu subMenu;
		UserType tipoUsuario = null;

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

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

			// Administracion Sistema
			if (tipoUsuario == UserType.ADMINISTRATOR) {
				subMenu = new DefaultSubMenu("Administración del sistema", "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/ManageSpecialties", null));
				subMenu.addElement(new DefaultSeparator());
				subMenu.addElement(createMenuItem("Ver médicos de un CAP", "fa fa-medkit", "/systemAdmin/ManageSpecialties", null));
				subMenu.addElement(new DefaultSeparator());
				subMenu.addElement(createMenuItem("Añadir usuario Admin", "fa fa-user-secret", "/systemAdmin/ManageSpecialties", 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/VisitView", null));
				if (tipoUsuario == UserType.PATIENT || tipoUsuario == UserType.ADMINISTRATOR)
					subMenu.addElement(createMenuItem("Modificar fecha/hora", "fa fa-briefcase", "/visit/VisitView", null));
				if (tipoUsuario == UserType.PATIENT)
					subMenu.addElement(createMenuItem("Anular visita", "fa fa-remove", "/visit/VisitView", null));
				if (tipoUsuario == UserType.FAMILY_DOCTOR)
					subMenu.addElement(createMenuItem("Añadir resultado", "fa fa-align-left", "/visit/VisitView", 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/MedicalTests", 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 (SessionUtils.isLogedIn() == false) {
			subMenu.addElement(createMenuItem("Registro de usuario", "fa fa-user-plus", "/profile/RegisterUser", null));
//			subMenu.addElement(createMenuItem("Registro de paciente", "fa fa-user-plus", "/profile/AddPatient", null));
//			subMenu.addElement(createMenuItem("Registro de médico", "fa fa-user-md", "/profile/AddFamilyDoctor", null));
//			subMenu.addElement(createMenuItem("Registro de especialista", "fa fa-user-md", "/profile/AddSpecialistDoctor", 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);
			}
		}
	}

	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;
	}
}