package com.teamlans.lepta.view.bill;

import com.teamlans.lepta.entities.Bill;
import com.teamlans.lepta.service.bill.BillService;
import com.teamlans.lepta.view.LeptaNotification;
import com.teamlans.lepta.view.ProtectedVerticalView;
import com.teamlans.lepta.view.bill.edit.EditBillView;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;

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

import javax.annotation.PostConstruct;

@SpringView(name = NewBillsView.VIEW_NAME)
public class NewBillsView extends ProtectedVerticalView {

  public static final String VIEW_NAME = "Bills";

  @Autowired
  private BillService billService;

  private Table table;

  private BeanItemContainer<Bill> container;

  @PostConstruct
  void init() {
    setSpacing(true);
    addComponent(buildBillsTable());
    addComponent(buildAddBillButton());
    addComponent(buildEditBillButton());
    addComponent(buildDeleteBillButton());
  }

  private Table buildBillsTable() {
    table = new Table();
    container = new BeanItemContainer<>(Bill.class);
    container.addAll(billService.listBillsFor(getLeptaUi().getLoggedInUser()));
    table.setContainerDataSource(container);
    table.setVisibleColumns("name");
    table.setSelectable(true);
    return table;
  }


  private Button buildAddBillButton() {
    Button addButton = new Button("Add");
    addButton.addClickListener(
        event -> getUI().getNavigator().navigateTo(EditBillView.VIEW_NAME + "/" + -1));
    return addButton;
  }

  private Button buildEditBillButton() {
    Button editButton = new Button("Edit");
    editButton.addClickListener(clickEvent -> {
      Bill selectedBill = (Bill) table.getValue();
      int id = selectedBill.getId();
      getUI().getNavigator().navigateTo(EditBillView.VIEW_NAME + "/" + id);
    });
    return editButton;
  }

  private Button buildDeleteBillButton() {
    Button deleteButton = new Button("Delete");
    deleteButton.addClickListener(clickEvent -> {
      Bill selectedBill = (Bill) table.getValue();
      container.removeItem(selectedBill);
      billService.deleteBill(selectedBill);
      LeptaNotification.show("Bill has been deleted");
    });
    return deleteButton;
  }


  @Override
  public void enter(ViewChangeListener.ViewChangeEvent event) {
    // the view is constructed in the init() method()
  }
}

@@ -452,42 +452,150 @@
 .ui.
-Button;%0Aimport com.vaadin.ui.Table
+Alignment;%0Aimport com.vaadin.ui.Button;%0Aimport com.vaadin.ui.HorizontalLayout;%0Aimport com.vaadin.ui.Table;%0Aimport com.vaadin.ui.VerticalLayout
 ;%0A%0Ai
@@ -975,164 +975,1028 @@
 %0A%0A  
-@PostConstruct%0A  void init() %7B%0A    setSpacing(true);%0A    addComponent(buildBillsTable());%0A    addComponent(buildAddBillButton());%0A    addComponent(buildEdit
+private Button editButton;%0A%0A  private Button deleteButton;%0A%0A  @PostConstruct%0A  void init() %7B%0A    setSpacing(true);%0A    setMargin(true);%0A    VerticalLayout centerContainer = new VerticalLayout();%0A    centerContainer.setSpacing(true);%0A    centerContainer.setWidth(%2270%25%22);%0A    centerContainer.addComponent(buildBillsTable());%0A    HorizontalLayout buttonContainer = buildButtonContainer();%0A    centerContainer.addComponent(buttonContainer);%0A    addComponent(centerContainer);%0A    setComponentAlignment(centerContainer, Alignment.MIDDLE_CENTER);%0A  %7D%0A%0A  private HorizontalLayout buildButtonContainer() %7B%0A    HorizontalLayout buttonContainer = new HorizontalLayout();%0A    buttonContainer.setSizeFull();%0A    buttonContainer.setSpacing(true);%0A    buttonContainer.addComponent(buildAddBillButton());%0A    editButton = buildEditBillButton();%0A    buttonContainer.addComponent(editButton);%0A    buttonContainer.setComponentAlignment(editButton, Alignment.MIDDLE_CENTER);%0A    editButton.setEnabled(false);%0A    deleteButton = buildDelete
 Bill
@@ -2007,52 +2007,201 @@
 on()
-)
 ;%0A    
-addComponent(buildDeleteBillButton())
+buttonContainer.addComponent(deleteButton);%0A    buttonContainer.setComponentAlignment(deleteButton, Alignment.MIDDLE_RIGHT);%0A    deleteButton.setEnabled(false);%0A    return buttonContainer
 ;%0A  
@@ -2260,24 +2260,49 @@
 ew Table();%0A
+    table.setSizeFull();%0A
     containe
@@ -2531,24 +2531,146 @@
 able(true);%0A
+    table.addItemClickListener(event -%3E %7B%0A      editButton.setEnabled(true);%0A      deleteButton.setEnabled(true);%0A    %7D);%0A
     return t

Improve bill overview look [#5]