/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.server.requestprocessor;

import hu.astrid.debgen.authentication.domain.DebGenUser;

import javax.servlet.http.HttpServletRequest;

/**
 * @author inagy
 */
public class ActionParameter {

    private String parameterName;
    private String errorMessage;
    private Throwable exception;

    public ActionParameter() {
    }

    public ActionParameter(String parameterName) {
        this.parameterName = parameterName;
    }

    public ActionParameter(String parameterName, String errorMessage) {
        this.parameterName = parameterName;
        this.errorMessage = errorMessage;
    }

    public ActionParameter(String parameterName, Exception exception) {
        this.parameterName = parameterName;
        this.errorMessage = exception.getMessage();

        this.exception = exception;
    }

    public ActionParameter(String parameterName, String errorMessage, Exception exception) {
        this.parameterName = parameterName;
        this.errorMessage = errorMessage;
        this.exception = exception;
    }

    public String getParameterName() {
        return parameterName;
    }

    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    public String getAction() {
        return this.parameterName;
    }

    public static boolean in(String element, String... array) {
        for (String s : array) {
            if (s.equals(element)) {
                return true;
            }
        }
        return false;
    }

    public String getScope() {
        //TODO: rh a metodus hivasa rengeteg tomb letrehozasat indikalhatja, amelyek konstansket kiemelhetoek lennenek
        if (in(this.getAction(), new String[]{"init"})) {
            return "init";
        } else if (in(this.getAction(), new String[]{"autofillfromtaj", "autofillfromname"})) {
            return "autofillform";
        } else if (in(this.getAction(), new String[]{"generatepdf", "print", "sendreprint", "reprint", "printrequest"})) {
            return "print";
        } else if (in(this.getAction(), new String[]{"generatepdfmodule", "moduleprint", "sendreprintmodule", "reprintmodule", "printmodule"})) {
            return "printmodule";
        } else if (in(this.getAction(), new String[]{"autocompletefmrtests", "autocompletehypotests", "autocompleteachotests",
                "autocompletefishtests", "autocompleteindication", "autocompletesendername", "autocompletesendercode",
                "autocompletediagnosis", "autocompletebnocode", "autocompletename", "autocompletetaj",
                "autocompletecode"})) {
            return "autocomplete";
        } else if (in(this.getAction(), new String[]{"confirmpassword", "cancelpassword", "sendreminder"})) {
            return "passwordreminder";
        } else if (in(this.getAction(), new String[]{"login", "logout", "changepassword", "savelanguage"})) {
            return "auth";
        } else if (in(this.getAction(), new String[]{"createpatient",
                "savepatient", "listpatients", "searchpatient", "showpatient", "findpatient",
                "deletepatient"})) {
            return "patientmanager";
        } else if (in(this.getAction(), new String[]{"showrequests","createmodule",
                    "savemodule","createtemplate","savetemplate","listtemplates",
                    "showmodule","deletemodule","showtemplate","deletetemplate","attachkariogrammodule","deletekariogram","displaykariogram"})) {
            return "modulemanager";
        } else if (in(this.getAction(), new String[]{"editrequest", "showrequest", "saverequest",
                "createrequest"})) {
            return "requestmanager";
        } else if (in(this.getAction(), new String[]{"editrequest","showrequest","saverequest",
                    "createrequest","deletesampletype","savesampletype","listsampletypes","showsample"})) {
            return "samplemanager";
        } else if (in(this.getAction(), new String[]{"listusers", "createuser", "saveuser", "enableuser",
                "disableuser", "showuser", "changeuserpassword", "saveuserpassword", "deleteuser"})) {
            return "useradmin";
        } else {
            return "default";
        }
    }

    public Throwable getException() {
        return exception;
    }

    public void setException(Throwable exception) {
        this.exception = exception;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public static ActionParameter getInvalidPageActionParameter(Exception ex) {
        return new ActionParameter("invalidpage", ex);
    }

    public static ActionParameter getMustChangePasswordActionParameter() {
        return new ActionParameter("chgangepassword");
    }

    public static ActionParameter getInformationActionParameter() {
        return new ActionParameter("showinformation");
    }

    static ActionParameter mainPage() {
        return new ActionParameter("mainpage");
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getScope() + ":" + getAction() + " - ");
        if (getErrorMessage() != null) {
            sb.append("Error message: " + getErrorMessage() + " - ");
        }
        if (getException() != null) {
            sb.append("Exception message: " + getException().getMessage());
        }
        return sb.toString();
    }

    public static DebGenUser getUser(HttpServletRequest request) {
        try {
            DebGenUser user = (DebGenUser) request.getSession().getAttribute("user");
            return user;
        } catch (Exception ex) {
            return null;
        }
    }

    public static ActionParameter getActionParameter(HttpServletRequest request) {
        try {
            ActionParameter action = (ActionParameter) request.getAttribute("actionParameter");
            return action;
        } catch (Exception ex) {
            return null;
        }
    }
}
