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

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

public class NavigationBar extends HorizontalLayout {

  public NavigationBar() {
    setSizeUndefined();
    setWidth("100%");
    setSpacing(true);
    addStyleName("navbar");

    MenuBar mainMenu = buildMainMenu();
    addComponent(mainMenu);
    setComponentAlignment(mainMenu, Alignment.TOP_LEFT);

    MenuBar userMenu = buildUserMenu();
    addComponent(userMenu);
    setComponentAlignment(userMenu, Alignment.TOP_RIGHT);
  }

  private MenuBar buildMainMenu() {
    MenuBar menuBar = new MenuBar();
    menuBar.addStyleName(ValoTheme.MENUBAR_BORDERLESS);
    menuBar.addStyleName("navmenu");

    menuBar.addItem("Home", FontAwesome.HOME, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        goTo(HomeView.VIEW_NAME);
      }
    });

    MenuBar.MenuItem myBills = menuBar.addItem("My Bills", null);
    myBills.addItem("New Bills", null, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        goTo(NewBillsView.VIEW_NAME);
      }
    });
    myBills.addItem("Assigned Bills", null, null);
    myBills.addItem("Archive", null, null);

    menuBar.addItem("Assign Bills", null, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        goTo(NewBillsView.VIEW_NAME);
      }
    });

    menuBar.addItem("ClearDebt", null, null);

    return menuBar;
  }

  private MenuBar buildUserMenu() {
    MenuBar menuBar = new MenuBar();
    menuBar.addStyleName(ValoTheme.MENUBAR_BORDERLESS);
    menuBar.addStyleName("navmenu");

    LeptaUi ui = ((LeptaUi) UI.getCurrent());

    MenuBar.MenuItem account = menuBar.addItem(ui.getLoggedInUser().getName(), null, null);
    account.addItem("Edit profile", FontAwesome.COG, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        goTo(EditProfileView.VIEW_NAME);
      }
    });
    account.addItem("Log out", FontAwesome.SIGN_OUT, new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem menuItem) {
        ui.setLoggedInUser(null);
      }
    });

    return menuBar;
  }

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

}

@@ -1086,115 +1086,16 @@
 ME, 
-new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A       
+event -%3E
  goT
@@ -1111,39 +1111,24 @@
 w.VIEW_NAME)
-;%0A      %7D%0A    %7D
 );%0A%0A    Menu
@@ -1228,115 +1228,16 @@
 ll, 
-new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A       
+event -%3E
  goT
@@ -1257,39 +1257,24 @@
 w.VIEW_NAME)
-;%0A      %7D%0A    %7D
 );%0A    myBil
@@ -1406,115 +1406,16 @@
 ll, 
-new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A       
+event -%3E
  goT
@@ -1439,31 +1439,16 @@
 EW_NAME)
-;%0A      %7D%0A    %7D
 );%0A%0A    
@@ -1878,115 +1878,16 @@
 OG, 
-new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A       
+event -%3E
  goT
@@ -1914,31 +1914,16 @@
 EW_NAME)
-;%0A      %7D%0A    %7D
 );%0A    a
@@ -1974,115 +1974,16 @@
 UT, 
-new MenuBar.Command() %7B%0A      @Override%0A      public void menuSelected(MenuBar.MenuItem menuItem) %7B%0A       
+event -%3E
  ui.
@@ -2007,23 +2007,8 @@
 ull)
-;%0A      %7D%0A    %7D
 );%0A%0A

use lambda expressions for navbar