package com.github.neunkasulle.chronocommand.view;

import com.ejt.vaadin.loginform.LoginForm;
import com.github.neunkasulle.chronocommand.control.LoginControl;
import com.vaadin.external.org.slf4j.Logger;
import com.vaadin.external.org.slf4j.LoggerFactory;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ValoTheme;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by Janze on 20.01.2016.
 */
public class LoginView extends LoginForm implements View {
    private Logger log = LoggerFactory.getLogger(LoginView.class);

    protected CheckBox rememberMe;
    protected Label authInfoMissing;
    protected Label authenticationFailed;
    protected PasswordField passwordField;


    @Override
    protected Component createContent(TextField usernameField, PasswordField passwordField, Button loginButton) {
        VerticalLayout layout = new VerticalLayout();
        layout.setSpacing(true);

        Label label = new Label("Please log in");
        label.setStyleName(ValoTheme.LABEL_H1);
        label.setSizeUndefined();
        layout.addComponent(label);
        layout.setComponentAlignment(label, Alignment.MIDDLE_CENTER);


        usernameField.setCaption("Username");
        layout.addComponent(usernameField);
        layout.setComponentAlignment(usernameField, Alignment.MIDDLE_CENTER);

        this.passwordField = passwordField;
        passwordField.setCaption("Password");
        layout.addComponent(passwordField);
        layout.setComponentAlignment(passwordField, Alignment.MIDDLE_CENTER);

        rememberMe = new CheckBox("Remember me");
        layout.addComponent(rememberMe);
        layout.setComponentAlignment(rememberMe, Alignment.MIDDLE_CENTER);

        layout.addComponent(loginButton);
        layout.setComponentAlignment(loginButton, Alignment.MIDDLE_CENTER);

        authInfoMissing = new Label("Please provide username and password!");
        authInfoMissing.setStyleName(ValoTheme.LABEL_FAILURE);
        authInfoMissing.setSizeUndefined();
        authInfoMissing.setVisible(false);
        layout.addComponent(authInfoMissing);
        layout.setComponentAlignment(authInfoMissing, Alignment.MIDDLE_CENTER);

        authenticationFailed = new Label("Username or password incorrect");
        authenticationFailed.setStyleName(ValoTheme.LABEL_FAILURE);
        authenticationFailed.setSizeUndefined();
        authenticationFailed.setVisible(false);
        layout.addComponent(authenticationFailed);
        layout.setComponentAlignment(authenticationFailed, Alignment.MIDDLE_CENTER);

        Button forgotPassword = new Button("Forgot password");
        forgotPassword.setStyleName(ValoTheme.BUTTON_LINK);
        forgotPassword.addClickListener(this::forgotPasswordClicked);
        layout.addComponent(forgotPassword);
        layout.setComponentAlignment(forgotPassword, Alignment.MIDDLE_CENTER);

        // DEBUG
        HorizontalLayout links = new HorizontalLayout();
        layout.addComponent(links);

        Button main = new Button("Main");
        main.addClickListener(event -> {
            getUI().getNavigator().navigateTo(MainUI.MAINVIEW);
        });
        links.addComponent(main);

        Button createUser = new Button("Create User");
        createUser.addClickListener(event -> {
            getUI().getNavigator().navigateTo(MainUI.CREATEUSERVIEW);
        });
        links.addComponent(createUser);

        Button timesheet = new Button("Timesheet");
        timesheet.addClickListener(event -> {
            getUI().getNavigator().navigateTo(MainUI.TIMESHEETVIEW);
        });
        links.addComponent(timesheet);

        Button messages = new Button("Messages");
        messages.addClickListener(event -> {
            getUI().getNavigator().navigateTo(MainUI.MESSAGEVIEW);
        });
        links.addComponent(messages);

        Button settings = new Button("Usersettings");
        settings.addClickListener(event -> {
            getUI().getNavigator().navigateTo(MainUI.SETTINGSVIEW);
        });
        links.addComponent(settings);

        return layout;
    }

    public void loginClicked(LoginEvent loginEvent) {
        log.info("User: {} Password: {} Remember Me: {}", new Object[]{loginEvent.getUserName(), loginEvent.getPassword(), rememberMe.getValue()});

        if (loginEvent.getUserName().isEmpty() || loginEvent.getPassword().isEmpty()) {
            authenticationFailed.setVisible(false);
            authInfoMissing.setVisible(true);
            return;
        }
        authInfoMissing.setVisible(false);

        boolean result = LoginControl.getInstance().login(loginEvent.getUserName(), loginEvent.getPassword(), rememberMe.getValue());
        if (result || loginEvent.getUserName().equals("dummy")) {
            authenticationFailed.setVisible(false);
            getUI().getNavigator().navigateTo(MainUI.MAINVIEW);
        } else {
            authenticationFailed.setVisible(true);
            passwordField.clear();
        }
    }

    public void forgotPasswordClicked(Button.ClickEvent clickEvent) {
        log.info("User clicked on \"Forgot password\"");
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        addLoginListener(this::loginClicked);
        setSizeFull();
    }
}

@@ -154,16 +154,90 @@
 ontrol;%0A
+import com.github.neunkasulle.chronocommand.model.ChronoCommandException;%0A
 import c
@@ -4786,24 +4786,25 @@
     
-boolean result =
+try %7B%0A           
  Log
@@ -4913,74 +4913,8 @@
 ));%0A
-        if (result %7C%7C loginEvent.getUserName().equals(%22dummy%22)) %7B%0A
     
@@ -5039,12 +5039,39 @@
   %7D 
-else
+catch(ChronoCommandException e)
  %7B%0A 

Fix compile error because of changed api