package nz.ac.auckland.view;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
import nz.ac.auckland.application.VidevoxApplication;
import nz.ac.auckland.application.VidevoxPlayer;
import nz.ac.auckland.model.Project;
import nz.ac.auckland.model.VidevoxException;

public class RootLayoutController extends VIDEVOXController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RootLayoutController.class);

	public static final String PREVIEW = "preview";

	public static final String EDITOR = "editor";

	@FXML
	private Button _previewButton;

	@FXML
	private Button _editorButton;

	@FXML
	private Label _projectName;

	@Override
	public void setMainApp(VidevoxApplication app) {
		super.setMainApp(app);
		_projectName.setText(Project.getProject().getName());
	}

	@FXML
	private void selectVideo() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Please select a video file to work on");
		// Set visible extensions
		FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Video Files ", "*.mp4");
		fileChooser.getExtensionFilters().add(extFilter);
		File file;
		file = fileChooser.showOpenDialog(_application.getStage());
		if (file == null) {
			// User canceled selecting video
			return;
		}
		logger.debug("Video name: " + file.getName());
		// set the video
		try {
			VidevoxPlayer.getPlayer().setVideo(file);
		} catch (VidevoxException e) {
			VidevoxApplication.showExceptionDialog(e);
		}
		_application.reset();
	}
	
	@FXML
	private void save() {
		if (Project.getProject().getLocation() == null) {
			saveAs();
		} else {
			try {
				Project.getProject().toFile(Project.getProject().getLocation());
			} catch (VidevoxException e) {
				VidevoxApplication.showExceptionDialog(e);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@FXML
	private void saveAs() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("Save the project");
		// Set extension filter to only see .vvox project files
		FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Project file", "*.vvox");
		fileChooser.getExtensionFilters().add(extFilter);
		File file = fileChooser.showSaveDialog(_application.getStage());
		if (file == null) {
			return;
		}
		try {
			Project.getProject().toFile(file);
		} catch (VidevoxException e) {
			VidevoxApplication.showExceptionDialog(e);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void close() {
		_application.saveAndClose();
	}

	@FXML
	private void editorButtonAction() {
		setViewToggle(EDITOR);
	}

	@FXML
	private void previewButtonAction() {
		setViewToggle(PREVIEW);
	}

	public void setViewToggle(String option) {
		switch (option) {
		case PREVIEW:
			_editorButton.setDisable(false);
			_previewButton.setDisable(true);
			break;
		case EDITOR:
			_previewButton.setDisable(false);
			_editorButton.setDisable(true);
			break;
		default:
		}
	}
}

@@ -347,24 +347,66 @@
 voxPlayer;%0D%0A
+import nz.ac.auckland.model.ModelHelper;%0D%0A
 import nz.ac
@@ -2530,32 +2530,84 @@
 n.getStage());%0D%0A
+%09%09ModelHelper.enforceFileExtension(file, %22.vvox%22);%0D%0A
 %09%09if (file == nu

enforce ".vvox" extension on save