package org.leguan.ui.controller;

import java.util.function.Predicate;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;
import org.leguan.config.Configuration;
import org.leguan.config.ConfigurationService;
import org.leguan.ui.Mask;
import org.leguan.ui.MaskLoader;
import org.leguan.ui.UiUtils;
import org.leguan.ui.widget.Wizard;
import org.leguan.ui.widget.WizardPane;

@Slf4j
public class StartupWizard  {

  private MaskLoader maskLoader = new MaskLoader();



  public StartupWizard (ConfigurationService configurationService, Configuration configuration) {

    Wizard wizard = new Wizard();
    wizard.setFinish(new Predicate<Void>() {
      @Override public boolean test(Void voidType) {
        log.info("Save " + configuration.getVersionsystems().size() + " versionsystems and " + configuration.getBuildEnvs().size() +  " buildenvs in initial configuration");
        configurationService.set(configuration);
        return true;
      }
    });

    // First side: Welcome
    WizardPane<NewWelcomeController> page1 = new WizardPane(wizard);
    page1.setHeaderText("Welcome", "Configure LEGUAN for the first time.");
    Mask<NewWelcomeController> mask1 = maskLoader.load("newWelcome");
    page1.setContent(mask1.getParent());
    page1.setController(mask1.getController());
    mask1.getController().setConfiguration(configuration);
    wizard.addPage (page1);

    //Second side: Versionsystem
    WizardPane<NewVersionsystemController> page2 = new WizardPane(wizard);
    page2.setHeaderText("Configure versionsystem", "Configure were your projects live! You can add further versionsystems over Configuration -> Versionsystems afterwards");
    Mask<NewVersionsystemController> mask2 = maskLoader.load("newVersionsystem");
    mask2.getController().setConfiguration(configuration);
    page2.setContent(mask2.getParent());
    page2.setController(mask2.getController());
    wizard.addPage (page2);

    //Third side: Build environment
    WizardPane page3 = new WizardPane(wizard);
    page3.setHeaderText("Configure build environment", "Configure were your want to build your projects! You can add further build environments over Configuration -> Build Environments afterwards");
    Mask<NewBuildEnvironmentController> mask3 = maskLoader.load("newBuildEnvironment");
    mask3.getController().setConfiguration(configuration);
    page3.setContent(mask3.getParent());
    page3.setController(mask3.getController());
    wizard.addPage (page3);

    WizardPane page4 = new WizardPane(wizard);
    page4.setHeaderText("Finish", "You have done all initial configurations");
    Mask mask4 = maskLoader.load("newStepHelp");
    mask4.getController().setConfiguration(configuration);
    page4.setContent(mask4.getParent());
    wizard.addPage (page4);

    Stage wizardStage = new Stage(StageStyle.UNDECORATED);
    wizardStage.setScene(new Scene(wizard));
    UiUtils.applyCss(wizard.getScene());
    wizardStage.setMinWidth(1024);
    wizardStage.setMinHeight(600);
    wizardStage.showAndWait();
  }

}
