package managedbean.medicalTest;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;

import TO.PatientTO;
import TO.QuestionTO;
import common.QuestionStatus;
import common.UserType;
import managedbean.common.ManagedBeanBase;
import managedbean.common.SessionUtils;

@Named("Questions")
@ViewScoped
public class QuestionsMBean extends ManagedBeanBase implements Serializable {

	private static final long serialVersionUID = 1L;

	private int userID;
	private UserType userType;
	private List<QuestionTO> pendingQuestions;
	protected String title;
	protected String message;
	protected String response;
	protected QuestionTO selected;

	public QuestionsMBean() {
	}

	@PostConstruct
	public void init() {
		// Inicialización de variables y propiedades van aquí.
		this.userType = SessionUtils.getUserType();
		this.userID = Integer.valueOf(SessionUtils.getUserId());
		this.selected = null;

		this.loadQuestions();
		this.printSelected();
	}
	
	private void printSelected() {
		System.out.println("Selected: ");
		System.out.println(this.selected);
	}

	public List<QuestionTO> getPendingQuestions() {
		return pendingQuestions;
	}

	public void setPendingQuestions(List<QuestionTO> value) {
		this.pendingQuestions = value;
	}
	/**
	 * Cargará las preguntas enviadas al médico o escritas por el paciente, esto irá
	 * en función del tipo de usuario en sesión
	 */
	private void loadQuestions() {
		this.printSelected();
		if (userType == UserType.FAMILY_DOCTOR) {
			// Listar las preguntas destinadas a él
			// El método disponible del API básico es referente al doctor
			this.pendingQuestions = getRemoteManagerMedicalTest().listAllPendingQuestions(userID);
		} else if (userType == UserType.PATIENT) {
			// Listar las preguntas realiadas por él
			// Para reutilizar la misma view, necesitaremos nuevos métodos para el API de
			// cara a recoger sus preguntas realizadas.
			this.pendingQuestions = getRemoteManagerMedicalTest().listAllMyQuestions(userID);
		}
	}

	public boolean getShowCreate() {
		return userType == UserType.PATIENT;
	}

	public boolean isShowCreate() {
		return this.getShowCreate();
	}

	public void setShowCreate() {
		//
	}
	
	public boolean getDisabledMessage() {
		if(this.selected != null) {
			return this.selected.getStatus() != QuestionStatus.NEW;
		}
		return false;
	}
	
	public boolean isDisabledMessage() {
		return this.getDisabledMessage();
	}
	
	public void setDisabledMessage(boolean bval) {
		// Nothing to do
	}

	public void onSelect(SelectEvent event) {
		this.printSelected();
		this.selected = (QuestionTO) event.getObject();
		this.printSelected();
	}

	public void onUnSelect(SelectEvent event) {
		this.selected = null;
	}
	
	public void setSelected(QuestionTO selected) {
		this.printSelected();
		this.selected = selected;
	}
	
	public QuestionTO getSelected() {
		return this.selected;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setResponse(String response) {
		this.response = response;
	}
	
	public void save() {
		this.printSelected();
		getRemoteManagerMedicalTest().answerQuestion(this.selected);
		this.init();
	}
	
	public void create() {
		this.printSelected(); 
		String template = "";
		this.selected = new QuestionTO();
		this.selected.setId(-1);
		this.selected.setTitle(template);
		this.selected.setMessage(template);
		this.selected.setResponse(template);
		this.selected.setStatus(QuestionStatus.NEW);
		this.selected.getPatient().setId(Integer.parseInt(SessionUtils.getloggedOnUser().getId()));
		this.selected.getPatient().setName(SessionUtils.getloggedOnUser().getName());
		this.printSelected();
	}

	public boolean getShowPanelDetail() {
		return this.selected != null;
	}

	public boolean isShowPanelDetail() {
		return this.getShowPanelDetail();
	}

	public void setShowPanelDetail() {
		//
	}

}
