package com.teamlans.lepta.view.home.component;

import com.teamlans.lepta.view.account.EditProfileView;
import com.teamlans.lepta.view.account.LoginView;
import com.teamlans.lepta.view.home.HomeView;
import com.teamlans.lepta.view.bill.NewBillsView;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.themes.ValoTheme;

public class NavigationBar extends HorizontalLayout {

  public NavigationBar() {
    this.setSpacing(true);
    this.addStyleName("navbar");
    this.addComponent(createNavigationButton("Home", HomeView.VIEW_NAME));
    this.addComponent(createBillMenu());
    this.addComponent(createNavigationButton("Assign Bills", NewBillsView.VIEW_NAME));
    this.addComponent(createNavigationButton("Clear Debt", NewBillsView.VIEW_NAME));

    addProfileMenu();
  }

  private Button createNavigationButton(String caption, final String viewName) {
    Button button = new Button(caption);
    button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
    button.addStyleName("navbutton");
    // If you didn't choose Java 8 when creating the project, convert this to an anonymous listener class
    button.addClickListener(event -> getUI().getNavigator().navigateTo(viewName));
    return button;
  }

  private MenuBar createBillMenu() {
    MenuBar menuBar = new MenuBar();
    menuBar.addStyleName(ValoTheme.MENUBAR_BORDERLESS);
    menuBar.addStyleName("navmenu");
    MenuBar.MenuItem item = menuBar.addItem("My Bills", null, null);
    item.addItem("New Bills", event -> getUI().getNavigator().navigateTo(NewBillsView.VIEW_NAME));
    item.addItem("Assigned Bills",
        event -> getUI().getNavigator().navigateTo(NewBillsView.VIEW_NAME));
    item.addItem("Archive", event -> getUI().getNavigator().navigateTo(NewBillsView.VIEW_NAME));
    return menuBar;
  }


  private void addProfileMenu() {
    // TODO: get username, add icon, align right
    MenuBar menuBar = new MenuBar();

    MenuBar.MenuItem profileMenu = menuBar.addItem("Username", null, null);
    addSubMenuLink(profileMenu, "Edit profile", FontAwesome.COG, EditProfileView.VIEW_NAME);
    addSubMenuLink(profileMenu, "Log out", FontAwesome.SIGN_OUT, LoginView.VIEW_NAME);

    addComponent(menuBar);
  }

  private void addSubMenuLink(MenuBar.MenuItem item, String name, FontAwesome icon, String goal) {
    item.addItem(name, icon, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        goTo(goal);
      }
    });
  }

  private void goTo(String goal) {
    getUI().getNavigator().navigateTo(goal);
  }

}

@@ -42,16 +42,51 @@
 onent;%0A%0A
+import com.teamlans.lepta.LeptaUi;%0A
 import c
@@ -2107,39 +2107,24 @@
  null);%0A    
-addSubMenuLink(
 profileMenu,
@@ -2122,18 +2122,25 @@
 fileMenu
-, 
+.addItem(
 %22Edit pr
@@ -2168,417 +2168,448 @@
 OG, 
-EditProfileView.VIEW_NAME);%0A    addSubMenuLink(profileMenu, %22Log out%22, FontAwesome.SIGN_OUT, LoginView.VIEW_NAME);%0A%0A    addComponent(menuBar
+new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A        goTo(EditProfileView.VIEW_NAME);%0A      %7D%0A    %7D
 );%0A  
-%7D%0A%0A
   pr
-ivate void addSubMenuLink(MenuBar.MenuItem item, String name, FontAwesome icon, String goal) %7B%0A    item.addItem(name, icon, new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A        goTo(goal);%0A      %7D%0A    %7D
+ofileMenu.addItem(%22Log out%22, FontAwesome.SIGN_OUT, new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A        ((LeptaUi)getUI()).setLoggedInUser(null);%0A        goTo(LoginView.VIEW_NAME);%0A      %7D%0A    %7D);%0A%0A    addComponent(menuBar
 );%0A 

add logout (issue #3)