/*
 * Copyright 2015 Ryan Gilera.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.daytron.revworks.behaviour.login;

import com.github.daytron.revworks.MainUI;
import com.github.daytron.revworks.authentication.AccessControl;
import com.github.daytron.revworks.authentication.AuthenticationException;
import com.github.daytron.revworks.data.ErrorMsg;
import com.github.daytron.revworks.service.NoCurrentUserException;
import com.github.daytron.revworks.data.UserType;
import com.github.daytron.revworks.ui.dashboard.DashboardScreen;
import com.github.daytron.revworks.util.NotificationUtil;
import com.vaadin.data.Validator;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The login behaviour for the {@link LoginScreen}.
 *
 * @author Ryan Gilera
 */
public class LoginButtonListener implements Button.ClickListener {

    private static final long serialVersionUID = 1L;

    private final OptionGroup userTypeOptionGroup;
    private final TextField userField;
    private final PasswordField passwordField;
    private UserType userType;

    private final AccessControl userAccessControl;

    public LoginButtonListener(TextField textField, PasswordField passwordField,
            OptionGroup userType) {
        this.userTypeOptionGroup = userType;
        this.userField = textField;
        this.passwordField = passwordField;

        this.userAccessControl = MainUI.get().getAccessControl();
    }

    /**
     * Validates the userField and passwordField.
     *
     * @param event The event object created when the button is clicked
     */
    @Override
    public void buttonClick(Button.ClickEvent event) {

        try {
            userField.validate();
            passwordField.validate();

            verifyUserCredentials();
        } catch (Validator.InvalidValueException e) {
            userField.setValidationVisible(true);
            passwordField.setValidationVisible(true);

            Logger.getLogger(LoginButtonListener.class.getName()).log(Level.SEVERE, null, e);
            NotificationUtil.showError(
                    ErrorMsg.INVALID_INPUT_CAPTION.getText());
        }

    }

    private void verifyUserCredentials() {
        determinUserType();

        String userName = userField.getValue();
        String password = passwordField.getValue();

        try {
            // Retrieve the user upon authentication
            userAccessControl.signIn(userType, userName, password);

            // Verifies if this is the only login session for the current user
            // If this is second login session made by the user,
            // it terminates the old session and continue with this new session
            // created. Afterwards this session is recorded in the servlet.
            // See MainUIServlet documentation
            MainUI.MainUIServlet.saveUserSessionInfo(
                    userAccessControl.getPrincipalName(),
                    VaadinSession.getCurrent());
            MainUI.MainUIServlet.printSessions("user login");

            DashboardScreen tempUI = new DashboardScreen(
                    MainUI.get());
            UI.getCurrent().setContent(tempUI);

            Notification.show("Welcome "
                    + userAccessControl.getFirstName()
                    + "!",
                    Notification.Type.TRAY_NOTIFICATION);

        } catch (AuthenticationException ex) {
            Logger.getLogger(LoginButtonListener.class.getName()).log(Level.SEVERE, null, ex);
            NotificationUtil.showError(
                    ErrorMsg.SIGNIN_FAILED_CAPTION.getText(),
                    ex.getMessage());
        }

    }

    private void determinUserType() {
        String userTypeString = (String) userTypeOptionGroup.getValue();

        userType = (userTypeString.equalsIgnoreCase(UserType.STUDENT.getText()))
                ? UserType.STUDENT : UserType.LECTURER;
    }

}

@@ -3785,17 +3785,16 @@
 ogin%22);%0A
-%0A
         
@@ -3801,134 +3801,54 @@
     
-DashboardScreen tempUI = new DashboardScreen(%0A                    MainUI.get());%0A            UI.getCurrent().setContent(tempUI
+%0A            MainUI.get().showDashboardScreen(
 );%0A%0A

Move Dashboard object creation responsibility to MainUI to allow MainUI to set the state of the Navigator object