package app.controllers;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import app.events.CopyEvent;
import app.events.CutEvent;
import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent;
import app.events.OpenFileEvent;
import app.events.OpenLinkInBrowserEvent;
import app.events.OpenProjectEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
import app.events.SaveFileEvent;
import app.events.ThemeChangedEvent;
import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent;
import app.service.DialogBoxes;
import app.service.FileOperations;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;

/**
 * A FXML controller that controls the menubar component of the UI
 */
public class MenubarController implements Initializable, Controller {

  private EventBus eventBus;

  @FXML
  private MenuBar menubar;

  @FXML
  private ToggleGroup languageToggleGroup;

  @FXML
  private ToggleGroup themeToggleGroup;

  @Override
  public void initialize(URL url, ResourceBundle resourceBundle) {
  }

  @Override
  public void setEventBus(EventBus eventBus) {
    this.eventBus = eventBus;
    this.eventBus.register(this);
  }

  /* ------------------------------------------------------------------------ */
  /* CREATE FILE/DIRECTORY */
  /* ------------------------------------------------------------------------ */
  @FXML
  private void handleNewFile() {
    this.eventBus.post(new OpenFileEvent(Optional.empty()));
  }

  @FXML
  private void handleNewFolder() {

  }

  /* ------------------------------------------------------------------------ */
  /* OPEN FILE/PROJECT */
  /* ------------------------------------------------------------------------ */

  /**
   * Handles whenever the Open File button is pressed in the menubar
   */
  @FXML
  public void handleOpenFile() {
    Stage stage = (Stage) menubar.getScene().getWindow();

    try {
      File file = FileOperations.openFileWithDialog(stage);

      this.eventBus.post(new OpenFileEvent(Optional.ofNullable(file.toPath())));
    } catch (FileNotFoundException e) {
      DialogBoxes.showErrorMessage("File not found!");
    }
  }

  /**
   * Handles whenever the Open Project button is pressed in the menubar
   */
  @FXML
  private void handleOpenProject() {
    Stage stage = (Stage) menubar.getScene().getWindow();

    try {
      File dir = FileOperations.openFolderWithDialog(stage);

      this.eventBus.post(new OpenProjectEvent(Optional.of(dir.toPath())));
    } catch (FileNotFoundException e) {}
  }

  /* ------------------------------------------------------------------------ */
  /* SAVE FILE */
  /* ------------------------------------------------------------------------ */

  /**
   * Handles whenever the Save button is pressed in the menubar
   */
  @FXML
  private void handleSaveFile() {
    this.eventBus.post(new SaveFileEvent());
  }

  /**
   * Handles whenever the Save as button is pressed in the menubar
   */
  @FXML
  private void handleSaveAsFile() {
    this.eventBus.post(new SaveFileEvent(false));
  }

  /**
   * Handles whenever the programming language is changed from the menubar.
   */
  @FXML
  private void handleLanguageChange(ActionEvent event) {
    this.eventBus.post(new LanguageChangedEvent(((RadioMenuItem) event.getSource()).getText()));
  }

  /**
   * Handles whenever the wraptext togglebutton is pressed in the menubar
   */
  @FXML
  private void handleToggleWraptext(ActionEvent event) {
    var isSelected = ((CheckMenuItem) event.getSource()).selectedProperty().get();
    this.eventBus.post(new ToggleWrapTextEvent(isSelected));
  }

  /**
   * Handles whenever the theme is changed from the menubar
   */
  @FXML
  private void handleThemeChange(ActionEvent event) {
    this.eventBus.post(new ThemeChangedEvent(((RadioMenuItem) event.getSource()).getText()));
  }

  /**
   * Handles whenever the exit button is pressed in the menubar
   */
  @FXML
  private void handleExitApplication(ActionEvent event) {
    this.eventBus.post(new ExitApplicationEvent());
  }

  /* ------------------------------------------------------------------------ */
  /* EDIT */
  /* ------------------------------------------------------------------------ */

  /**
   * Handles whenever the undo button is pressed in the menubar
   */
  @FXML
  private void handleUndo(ActionEvent event) {
    this.eventBus.post(new UndoEvent());
  }

  /**
   * Handles whenever the redo button is pressed in the menubar
   */
  @FXML
  private void handleRedo(ActionEvent event) {
    this.eventBus.post(new RedoEvent());
  }

  /**
   * Handles whenever the copy button is pressed in the menubar
   */
  @FXML
  private void handleCopy(ActionEvent event) {
    this.eventBus.post(new CopyEvent());
  }

  /**
   * Handles whenever the cut button is pressed in the menubar
   */
  @FXML
  private void handleCut(ActionEvent event) {
    this.eventBus.post(new CutEvent());
  }

  /**
   * Handles whenever the paste button is pressed in the menubar
   */
  @FXML
  private void handlePaste(ActionEvent event) {
    this.eventBus.post(new PasteEvent());
  }

  /**
   * Handles whenever the Toggle Comment button is pressed in the menubar
   */
  @FXML
  private void handleToggleComment(ActionEvent event) {
    this.eventBus.post(new ToggleCommentEvent());
  }

  /* ------------------------------------------------------------------------ */
  /* ABOUT */
  /* ------------------------------------------------------------------------ */

  /**
   * Handles whenever the About button is pressed in the menubar
   */
  @FXML
  private void handleAbout(ActionEvent event) {
    String aboutLink = "https://gitlab.stud.idi.ntnu.no/oysteikt/tdt4100-project-2021v/-/blob/master/README.md";
    this.eventBus.post(new OpenLinkInBrowserEvent(aboutLink));
  }

  /* ------------------------------------------------------------------------ */
  /* SUBSCRIPTIONS */
  /* ------------------------------------------------------------------------ */

  /**
   * Updates menubuttons whenever the language is changed
   */
  @Subscribe
  private void handle(LanguageChangedEvent event) {
    this.languageToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast)
        .filter(t -> t.getId().equals("toggle" + event.getLanguage())).findFirst().orElseThrow().setSelected(true);
  }

  /**
   * Updates menubuttons whenever the theme is changed
   */

  @Subscribe
  private void handle(ThemeChangedEvent event) {
    this.themeToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast)
        .filter(t -> t.getId().equals("toggle" + event.getTheme().replace(" ", "_"))).findFirst().orElseThrow().setSelected(true);

  }

}
