/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mobireader.gui.ui;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Accordion;
import javafx.scene.control.AccordionBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBoxBuilder;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ChoiceBoxBuilder;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ContextMenuBuilder;
import javafx.scene.control.HyperlinkBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.ListView;
import javafx.scene.control.ListViewBuilder;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuBarBuilder;
import javafx.scene.control.MenuBuilder;
import javafx.scene.control.MenuButtonBuilder;
import javafx.scene.control.MenuItemBuilder;
import javafx.scene.control.PasswordField;
import javafx.scene.control.PasswordFieldBuilder;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressBarBuilder;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ProgressIndicatorBuilder;
import javafx.scene.control.RadioButton;
import javafx.scene.control.RadioButtonBuilder;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollBarBuilder;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPaneBuilder;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Separator;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.control.SplitMenuButtonBuilder;
import javafx.scene.control.SplitPane;
import javafx.scene.control.SplitPaneBuilder;
import javafx.scene.control.Tab;
import javafx.scene.control.TabBuilder;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPaneBuilder;
import javafx.scene.control.TableColumnBuilder;
import javafx.scene.control.TableView;
import javafx.scene.control.TableViewBuilder;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextAreaBuilder;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.control.TitledPane;
import javafx.scene.control.TitledPaneBuilder;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.control.ToolBarBuilder;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItemBuilder;
import javafx.scene.control.TreeView;
import javafx.scene.control.TreeViewBuilder;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.CircleBuilder;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.web.HTMLEditor;
import javafx.scene.web.HTMLEditorBuilder;
import javafx.scene.web.WebView;
import javafx.scene.web.WebViewBuilder;
import javafx.stage.Popup;
import javafx.stage.PopupBuilder;
import javafx.stage.Stage;
import mobireader.Book;
import mobireader.SqliteDatabaseHandler;
import mobireader.gui.model.GuiModel;

/**
 *
 * @author att
 */
public class AppMain extends Application {

  // A reference to the model
  GuiModel model = new GuiModel();
  
  Stage stage;
  CheckBox checkBox;
  ContextMenu contextMenu;
  Label htmlLabel;
  Popup alertPopup;
  Tab webViewTab;
  ArrayList<Book> myBooks;
  SqliteDatabaseHandler db = new SqliteDatabaseHandler("my_books.db");
  
  public static void main(String[] args) {
    Application.launch(args);
  }
    
  @Override
  public void start(final Stage primaryStage) throws ClassNotFoundException, SQLException {
    myBooks = db.getAllBooks();
    stage = primaryStage;
    Scene scene = SceneBuilder.create()
      .width(800)
      .height(600)
      .root(
        BorderPaneBuilder.create()
          .top(
            VBoxBuilder.create()
              .children(
                createMenus()
              )
              .build()
          )
          .center(createTabs())
          .build()
      )
      .build();
    
    stage.setScene(scene);
    stage.setTitle("Mobi reader");
    stage.show();
  }
  
  MenuBar createMenus() {
    MenuBar menuBar = MenuBarBuilder.create()
      .menus(
        MenuBuilder.create()
          .text("Edit")
          .items(
            MenuItemBuilder.create()
              .text("Cut")
              .build(),
            MenuItemBuilder.create()
              .text("Copy")
              .build(),
            MenuItemBuilder.create()
              .text("Paste")
              .build()
          )
          .build()
      )
      .build();
    
    return menuBar;
  }  
  
  TabPane createTabs() {
    final WebView webView;
    TabPane tabPane = TabPaneBuilder.create()
      .tabs(
        TabBuilder.create()
          .text("Your books")
          .content(createAddedBooksView())
          .closable(false)
          .build(),
        TabBuilder.create()
          .text("Add a book")
          .content(createScrollMiscDemoNode())
          .closable(false)
          .build()
      )
      .build();
    
    return tabPane;
  }  
  
  TreeItem createBookTreeItem (Book book)
  {
      return TreeItemBuilder.create()
              .value(book.getTitle())
              .children(
                TreeItemBuilder.create()
                  .value("Author: " + book.getAuthor().getName())
                  .children(
                  TreeItemBuilder.create()
                    .value(book.getAuthor().provideAllInfo())
                    .build()
                  )
                  .build(),
                TreeItemBuilder.create()
                  .value("Added: " + book.getTimestamp())
                  .build()
              )
              .build();
  }
  
  TreeItem createTreeViewOfBooksByAuthor(List<Book> books)
  {
      List<TreeItem> bookItems= new ArrayList<>();
      for (Book book : books)
      {
          bookItems.add(createBookTreeItem(book));
      }
      return TreeItemBuilder.create()
          .value("Root")
          .children(
              bookItems.get(0),
              bookItems.get(1),
              bookItems.get(2)
           )
          .build();
  }
  
  Node createAddedBooksView() {
    TreeView treeView = TreeViewBuilder.create()
      .minWidth(150)
      .showRoot(false)
      .editable(false)
      .root(
            createTreeViewOfBooksByAuthor(myBooks)
       )
      .build();
    
    ListView listView = ListViewBuilder.create()
      .items(model.listViewItems)
      .build();

    SplitPane splitPane = SplitPaneBuilder.create()
      .items(
        treeView,
        listView
      )
      .build();

    treeView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    treeView.getSelectionModel().selectedItemProperty()
                                .addListener(new ChangeListener() {
      @Override
      public void changed(ObservableValue observable, Object oldValue,
                          Object newValue) {
        TreeItem treeItem = (TreeItem)newValue;
        if (newValue != null && treeItem.isLeaf()) {
          model.listViewItems.clear();
          for (int i = 1; i <= 1; i++) {
            model.listViewItems.add(treeItem.getValue());
          }
        }
      }
    });
    
    return splitPane;
  }
  
  Node createScrollMiscDemoNode() {
    ScrollPane scrollPane = ScrollPaneBuilder.create()
      .hbarPolicy(ScrollPane.ScrollBarPolicy.NEVER)
      .vbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED)
      .onMousePressed(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent me) {
          if (me.getButton() == MouseButton.SECONDARY) {
            contextMenu.show(stage, me.getScreenX(), me.getScreenY());
          }
        }
      })
      .build();
    return scrollPane;
  }
  
  Popup createAlertPopup(String text) {
    Button okButton;
    alertPopup = PopupBuilder.create()
      .content(
        StackPaneBuilder.create()
          .children(
            RectangleBuilder.create()
              .width(300)
              .height(200)
              .arcWidth(20)
              .arcHeight(20)
              .fill(Color.LIGHTBLUE)
              .stroke(Color.GRAY)
              .strokeWidth(2)
              .build(),
              BorderPaneBuilder.create()
                .center(
                  htmlLabel = LabelBuilder.create()
                    .text(text)
                    .wrapText(true)
                    .maxWidth(280)
                    .maxHeight(140)
                    .build()
                )
                .bottom(
                  okButton = ButtonBuilder.create()
                    .text("OK")
                    .onAction(new EventHandler<ActionEvent>() {
                      @Override public void handle(ActionEvent e) {
                        alertPopup.hide();
                      }
                    })
                    .build() 
                )
                .build()
          )
          .build()
      )
      .build();
        
    BorderPane.setAlignment(okButton, Pos.CENTER);
    BorderPane.setMargin(okButton, new Insets(10, 0, 10, 0));
    return alertPopup;
  }  
}

