package com.teamlans.lepta.view.account;

import com.teamlans.lepta.entities.User;
import com.teamlans.lepta.service.user.UserService;
import com.vaadin.data.Validator;
import com.vaadin.data.validator.NullValidator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class allows users to change their account details.
 */
@SpringView(name = EditProfileView.VIEW_NAME)
public final class EditProfileView extends VerticalLayout implements View {

  public static final String VIEW_NAME = "EditProfile";
  @Autowired
  private UserService userService;

  private final User user = new User(); // TODO: get user

  private TextField nameField;
  private PasswordField oldPasswordField;
  private PasswordField newPasswordField;
  private PasswordField confirmNewPasswordField;

  public EditProfileView() {
    build();
  }

  private void build() {
    // TODO: write to database!
    Component title = buildTitle();
    addComponent(title);
    setComponentAlignment(title, Alignment.TOP_LEFT);

    final Component changers = buildChangers();
    addComponent(changers);
    setComponentAlignment(changers, Alignment.TOP_CENTER);
  }

  private Component buildTitle() {
    final Label title = new Label("Hello " + user.getName() + ".");
    title.setSizeUndefined();
    return title;
  }

  private Component buildChangers() {
    final Accordion accordion = new Accordion();
    accordion.setSizeUndefined();
    accordion.setWidth("70%");

    accordion.addTab(buildColorPicker(), "Select a new color");
    accordion.addTab(buildNameChanger(), "Change your username");
    accordion.addTab(buildPasswordForm(), "Change your password");

    return accordion;
  }

  private Component buildColorPicker() {
    HorizontalLayout container = new HorizontalLayout();

    Button darkBlue = new Button();
    container.addComponent(darkBlue);

    Button lightBlue = new Button();
    container.addComponent(lightBlue);

    Button green = new Button();
    container.addComponent(green);

    Button yellow = new Button();
    container.addComponent(yellow);

    Button orange = new Button();
    container.addComponent(orange);

    Button red = new Button();
    container.addComponent(red);

    return container;
  }

  private Component buildNameChanger() {
    final HorizontalLayout container = new HorizontalLayout();

    nameField = new TextField();
    nameField.setInputPrompt("Enter a new name");

    container.addComponent(nameField);
    container.addComponent(buildNameButton(nameField));

    return container;
  }

  private Button buildNameButton(TextField nameField) {
    final Button button = new Button("OK");
    button.addClickListener(new Button.ClickListener() {
      @Override
      public void buttonClick(Button.ClickEvent clickEvent) {
        String newName = nameField.getValue();
        if (!newName.isEmpty() && !newName.equals(user.getName())) {
          user.setName(newName);
          removeAllComponents();
          build();
        }
      }
    });
    return button;
  }

  private Component buildPasswordForm() {
    final VerticalLayout container = new VerticalLayout();

    oldPasswordField = new PasswordField("Old password:");
    container.addComponent(oldPasswordField);

    newPasswordField = new PasswordField("New password:");
    container.addComponent(newPasswordField);

    confirmNewPasswordField = new PasswordField("Confirm new password:");
    container.addComponent(confirmNewPasswordField);

    container.addComponent(buildPasswordButton());

    return container;
  }

  private Button buildPasswordButton() {
    final Button button = new Button("OK");
    button.addClickListener(new Button.ClickListener() {
      @Override
      public void buttonClick(Button.ClickEvent clickEvent) {
        // TODO: add listener
      }
    });
    return button;
  }

  @Override
  public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent) {
    // needed for View implementation
  }

}

@@ -132,90 +132,8 @@
 ce;%0A
-import com.vaadin.data.Validator;%0Aimport com.vaadin.data.validator.NullValidator;%0A
 impo
@@ -454,43 +454,8 @@
 el;%0A
-import com.vaadin.ui.Notification;%0A
 impo
@@ -979,173 +979,8 @@
 er%0A%0A
-  private TextField nameField;%0A  private PasswordField oldPasswordField;%0A  private PasswordField newPasswordField;%0A  private PasswordField confirmNewPasswordField;%0A%0A
   pu
@@ -1083,16 +1083,50 @@
 se!%0A    
+// TODO: add validators%0A    final 
 Componen
@@ -1940,24 +1940,60 @@
 rPicker() %7B%0A
+    // TODO: implement color picker%0A
     Horizont
@@ -2598,24 +2598,40 @@
 out();%0A%0A    
+final TextField 
 nameField = 
@@ -3413,19 +3413,22 @@
 );%0A%0A    
-old
+final 
 Password
@@ -3425,32 +3425,36 @@
 l PasswordField 
+old 
 = new PasswordFi
@@ -3505,16 +3505,30 @@
 nent(old
+);%0A%0A    final 
 Password
@@ -3532,23 +3532,16 @@
 ordField
-);%0A%0A   
  newPass
@@ -3536,37 +3536,32 @@
 ield newPassword
-Field
  = new PasswordF
@@ -3621,29 +3621,24 @@
 Password
-Field
 );%0A%0A    
 confirmN
@@ -3629,26 +3629,22 @@
 );%0A%0A    
-confirmNew
+final 
 Password
@@ -3645,24 +3645,34 @@
 sswordField 
+confirmed 
 = new Passwo
@@ -3743,23 +3743,9 @@
 firm
-NewPasswordFiel
+e
 d);%0A
@@ -3792,16 +3792,43 @@
 dButton(
+old, newPassword, confirmed
 ));%0A%0A   
@@ -3888,16 +3888,122 @@
 dButton(
+PasswordField old, PasswordField newPassword,%0A                                     PasswordField confirmed
 ) %7B%0A    
@@ -4189,29 +4189,373 @@
     
-// TODO: add listener
+if (old.getValue().equals(user.getPassword())) %7B%0A          if (newPassword.getValue().equals(confirmed.getValue())) %7B%0A            user.setPassword(newPassword.getValue());%0A            removeAllComponents();%0A            build();%0A          %7D else %7B%0A            // TODO: %22two different inputs%22%0A          %7D%0A        %7D else %7B%0A          // TODO: %22wrong password%22%0A        %7D
 %0A   

add (most of) password changer