package br.com.teletronic.ativos.ui.application.views;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.teletronic.ativos.ui.vaadin.support.spring.locale.VaadinMessageSource;
import br.com.teletronic.ativos.ui.vaadin.support.spring.navigation.VaadinView;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope("prototype")
@VaadinView(MainView.NAME)
public class MainView extends Panel implements View {

	private static final long serialVersionUID = 1L;
	public static final String NAME = "";

	@Autowired
	private VaadinMessageSource messageSource;

	CssLayout root = new CssLayout();
	VerticalLayout loginLayout;

	@PostConstruct
	public void PostConstruct() {
		setSizeFull();
		setContent(root);
		root.addStyleName("root");
		root.setSizeFull();

		Label bg = new Label();
		bg.setSizeUndefined();
		bg.addStyleName("login-bg");
		root.addComponent(bg);

		buildLoginView(false);
	}

	private void buildLoginView(boolean exit) {
		if (exit) {
			root.removeAllComponents();
		}

		addStyleName("login");

		loginLayout = new VerticalLayout();
		loginLayout.setSizeFull();
		loginLayout.addStyleName("login-layout");
		root.addComponent(loginLayout);

		final CssLayout loginPanel = new CssLayout();
		loginPanel.addStyleName("login-panel");

		HorizontalLayout labels = new HorizontalLayout();
		labels.setWidth("100%");
		labels.setMargin(true);
		labels.addStyleName("labels");
		loginPanel.addComponent(labels);

		Label welcome = new Label(
				messageSource.getMessage("ativos.ui.login.label.welcome"));
		welcome.setSizeUndefined();
		welcome.addStyleName("h4");
		labels.addComponent(welcome);
		labels.setComponentAlignment(welcome, Alignment.MIDDLE_LEFT);

		Label title = new Label(
				messageSource.getMessage("ativos.ui.application.shortname"));
		title.setSizeUndefined();
		title.addStyleName("h2");
		title.addStyleName("light");
		labels.addComponent(title);
		labels.setComponentAlignment(title, Alignment.MIDDLE_RIGHT);

		HorizontalLayout fields = new HorizontalLayout();
		fields.setSpacing(true);
		fields.setMargin(true);
		fields.addStyleName("fields");

		final TextField username = new TextField(
				messageSource.getMessage("ativos.ui.login.label.username"));
		username.focus();
		fields.addComponent(username);

		final PasswordField password = new PasswordField(
				messageSource.getMessage("ativos.ui.login.label.password"));
		fields.addComponent(password);

		final Button signin = new Button(
				messageSource.getMessage("ativos.ui.login.button.signin"));
		signin.addStyleName("default");
		fields.addComponent(signin);
		fields.setComponentAlignment(signin, Alignment.BOTTOM_LEFT);

		final ShortcutListener enter = new ShortcutListener(messageSource.getMessage("ativos.ui.login.button.signin"),
				KeyCode.ENTER, null) {
			@Override
			public void handleAction(Object sender, Object target) {
				signin.click();
			}
		};

		signin.addClickListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				if (username.getValue() != null
						&& username.getValue().equals("")
						&& password.getValue() != null
						&& password.getValue().equals("")) {
					signin.removeShortcutListener(enter);
					// buildMainView();
				} else {
					if (loginPanel.getComponentCount() > 2) {
						// Remove the previous error message
						loginPanel.removeComponent(loginPanel.getComponent(2));
					}
					// Add new error message
					Label error = new Label(messageSource.getMessage("ativos.ui.login.invalid"),
							ContentMode.HTML);
					error.addStyleName("error");
					error.setSizeUndefined();
					error.addStyleName("light");
					// Add animation
					error.addStyleName("v-animate-reveal");
					loginPanel.addComponent(error);
					username.focus();
				}
			}
		});

		signin.addShortcutListener(enter);

		loginPanel.addComponent(fields);

		loginLayout.addComponent(loginPanel);
		loginLayout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);

	}

	@Override
	public void enter(ViewChangeListener.ViewChangeEvent event) {
	}
}
