/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package us.guihouse.projector.forms.controllers;

import us.guihouse.projector.scenes.SceneObserver;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import us.guihouse.projector.projection.ProjectionManager;
import us.guihouse.projector.scenes.BgImageScene;
import us.guihouse.projector.scenes.BrowserSubScene;
import us.guihouse.projector.scenes.ImageSubScene;
import us.guihouse.projector.scenes.ProjectionItemSubScene;
import us.guihouse.projector.scenes.TextSubScene;
import org.controlsfx.dialog.FontSelectorDialog;
/**
 * FXML Controller class
 *
 * @author guilherme
 */
public class WorkspaceController implements Initializable, SceneObserver {
    private SceneManager sceneManager;
    private GraphicsDeviceHelper graphicsHelper;
    private final List<ProjectionItemSubScene> items = new ArrayList<>(); 
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        graphicsHelper = new GraphicsDeviceHelper(projectionScreenMenu);
        initializeProjectionList();
        onCropBackgroundChanged();
    }    
    
    public void stop() {
        graphicsHelper.stop();
    }
    
    public void dispose() {
        graphicsHelper.dispose();
    }
    
    // ------------------------------
    // Menu
    // ------------------------------
    @FXML
    private CheckMenuItem cropBackgroundMenuItem;
    
    @FXML
    private RadioMenuItem singleLineProjectionMenuItem;
    
    @FXML
    private RadioMenuItem multilineProjectionMenuItem;
    
    @FXML
    private Menu projectionScreenMenu;
    
    @FXML
    private CheckMenuItem fullScreenCheckMenuItem;
    
    @FXML
    public void onRegisterManualMusic() { }
    
    @FXML
    public void onImportMusicFromWeb() { }
    
    @FXML
    public void onImportMusicFromTextFile() {}
    
    @FXML
    public void onOpenMusicList() {}
    
    @FXML
    public void onSelectBackgroundImageFile() {
        try {
            Scene changeBg = BgImageScene.createScene(graphicsHelper.getProjectionManager(), getSceneManager(), 800, 600);
            getSceneManager().goToScene(changeBg);
        } catch (IOException ex) {
            Logger.getLogger(WorkspaceController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @FXML
    public void onRemoveBackground() {
        graphicsHelper.getProjectionManager().setBackgroundImageFile(null);
    }
    
    @FXML
    public void onCropBackgroundChanged() {
        graphicsHelper.getProjectionManager().setCropBackground(cropBackgroundMenuItem.isSelected());
    }
    
    @FXML
    public void onChangeFont() {

    }
    
    @FXML
    public void onChangeFullScreen() {
        graphicsHelper.getProjectionManager().setFullScreen(fullScreenCheckMenuItem.isSelected());
    }
    
    @FXML
    public void onSingleLineProjection() {}
    
    @FXML
    public void onMultilineProjection() {}
    
    @FXML
    public void onHelpAbout() {}
    
    @FXML
    public void onHelpManual() {}
    
    // ------------------------------
    // Projection List
    // ------------------------------
    
    @FXML
    private ListView projectionListView;
    
    private boolean changingTitle = false;
    
    private void initializeProjectionList() {
        projectionListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        projectionListView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (changingTitle) { return; }
                
                if (newValue != null) {
                    int val = newValue.intValue();
                    if (val >= 0 && val < items.size()) {
                        setProjectionView(items.get(val));
                        return;
                    }
                }
                
                setProjectionView(null);
            }
        });
    }
    
    private void setProjectionView(ProjectionItemSubScene scene) {
        if (scene == null) {
            targetPane.getChildren().clear();
            return;
        }
        
        if (targetPane.getChildren().size() <= 0) {
            targetPane.getChildren().add(scene);
            return;
        }
        
        if (Objects.equals(targetPane.getChildren().get(0), scene)) {
            return;
        }
        
        targetPane.getChildren().clear();
        targetPane.getChildren().add(scene);
    }
    
    @FXML
    public void onAddMusic() {}
    
    @FXML
    public void onAddBrowser() {
        try {
            ProjectionItemSubScene created = BrowserSubScene.createScene(targetPane.getWidth(), targetPane.getHeight());
            created.setObserver(this);
            
            items.add(created);
            projectionListView.getItems().add("Página Web");
            projectionListView.getSelectionModel().select(projectionListView.getItems().size() - 1);
            
            created.initWithProjectionManager(graphicsHelper.getProjectionManager());
            created.widthProperty().bind(targetPane.widthProperty());
            created.heightProperty().bind(targetPane.heightProperty());
        } catch (IOException ex) {
            Logger.getLogger(WorkspaceController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @FXML
    public void onAddPicture() {
        try {
            ProjectionItemSubScene created = ImageSubScene.createScene(targetPane.getWidth(), targetPane.getHeight());
            created.setObserver(this);
            
            items.add(created);
            projectionListView.getItems().add("Nova imagem");
            projectionListView.getSelectionModel().select(projectionListView.getItems().size() - 1);
            
            created.initWithProjectionManager(graphicsHelper.getProjectionManager());
            created.widthProperty().bind(targetPane.widthProperty());
            created.heightProperty().bind(targetPane.heightProperty());
        } catch (IOException ex) {
            Logger.getLogger(WorkspaceController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @FXML
    public void onAddText() {
        try {
            ProjectionItemSubScene created = TextSubScene.createScene(targetPane.getWidth(), targetPane.getHeight());
            created.setObserver(this);
            
            items.add(created);
            projectionListView.getItems().add("Novo texto");
            projectionListView.getSelectionModel().select(projectionListView.getItems().size() - 1);
            
            created.initWithProjectionManager(graphicsHelper.getProjectionManager());
            created.widthProperty().bind(targetPane.widthProperty());
            created.heightProperty().bind(targetPane.heightProperty());
        } catch (IOException ex) {
            Logger.getLogger(WorkspaceController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // ------------------------------
    // Preview
    // ------------------------------
    
    @FXML
    private Canvas previewCanvas;
    
    @FXML
    private Pane targetPane;

    @Override
    public void titleChanged(ProjectionItemSubScene scene, String newTitle) {
        int index = items.indexOf(scene);
        if (index >= 0 && index < items.size()) {
            changingTitle = true;
            int selected = projectionListView.getSelectionModel().getSelectedIndex();
            projectionListView.getItems().set(index, newTitle);
            projectionListView.getSelectionModel().select(selected);
            changingTitle = false;
        }
    }

    public SceneManager getSceneManager() {
        return sceneManager;
    }

    public void setSceneManager(SceneManager sceneManager) {
        this.sceneManager = sceneManager;
    }
}

@@ -409,35 +409,8 @@
 ts;%0A
-import java.util.Optional;%0A
 impo
@@ -947,98 +947,8 @@
 ne;%0A
-import javafx.scene.text.Font;%0Aimport us.guihouse.projector.projection.ProjectionManager;%0A
 impo
@@ -1211,57 +1211,8 @@
 ne;%0A
-import org.controlsfx.dialog.FontSelectorDialog;%0A
 /**%0A

Fix erro de compilação