/*
 * OptionsScreen.fx
 */

package cookery;

import javafx.scene.Scene;
import javafx.stage.Stage;

import javafx.scene.control.CheckBox;
import javafx.scene.control.Slider;

import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

import javafx.scene.control.Button;
import javafx.scene.layout.HBox;

import cookery.control.ComboBox;

import cookery_db.database.Category;

import javafx.util.Properties;

import java.lang.NumberFormatException;


/**
 * A screen that allows the application settings to be adjusted by the user.
 * @author Nick Apperley
 */
public class OptionsScreen extends Stage{
    def CATEGORY = "category";
    def BACKUP_DB = "backupDb";
    def BACKUP_INTERVAL_KEY = "backupInterval";
    public var categories: Category[];
    public-init var controller: Controller;
    def appProperties: Properties = controller.performAction("Get application options", null)[0] as Properties;

    def backupDbChkBox = CheckBox{
        text: "Backup Database"
        selected: if(appProperties.get(BACKUP_DB).equals("true")){
            true;
        }
        else{
            false;
        }
    };
    def defaultCatLbl = Label{text: "Default Category"};
    def defaultCatCboBox = ComboBox{
        items: bind for(cat in categories){cat.getName()}
    };
    def backupIntervalLbl = Label{text: "Backup Interval"};
    def backupIntervalSlider = Slider{
        majorTickUnit: 1 snapToTicks: true min: 1 max: 30
        showTickLabels: true showTickMarks: true
    };
    def saveBtn = Button{
        text: "Save"
        action: function(){saveOptions(); close()}
    };
    def closeBtn = Button{
        text: "Close"
        action: function(){close()}
    };

    def mainLayout = VBox{
        content: [
            HBox{content: [defaultCatLbl, defaultCatCboBox]},
            backupDbChkBox,
            backupIntervalLbl,
            backupIntervalSlider,
            HBox{spacing: 5 content: [saveBtn, closeBtn] translateY: 25}
        ]
        translateX: 5
    };

    /**
     * Saves the application options to a properties file.
    */
    function saveOptions(): Void{
        if(backupDbChkBox.selected){
            controller.performAction("Start DB backups", Utility.doubleToInteger(backupIntervalSlider.value));
        }
        else
        {
            controller.performAction("Stop DB backups", null);
        }

        appProperties.put(CATEGORY, (defaultCatCboBox.getSelectedItem() as String));
        appProperties.put(BACKUP_DB, String.valueOf(backupDbChkBox.selected));
        appProperties.put(BACKUP_INTERVAL_KEY, String.valueOf(Utility.doubleToInteger(backupIntervalSlider.value)));
        controller.performAction("Save application options", appProperties)
    }

    init{
        // How often to make backups (in minutes).
        def DEFAULT_BACKUP_INTERVAL = 10;

        for(item in defaultCatCboBox.items){
            if(appProperties.get(CATEGORY).equals(item)){
                defaultCatCboBox.selectItem(indexof item);
                break;
            }
        }
        try {
            backupIntervalSlider.value = Integer.parseInt(appProperties.get(BACKUP_INTERVAL_KEY));
        } catch (ex: NumberFormatException) {
            backupIntervalSlider.value = DEFAULT_BACKUP_INTERVAL;
        }

        title = "Options";
        scene = Scene{
            width: 800
            height: 600
            content: [mainLayout]
        };
        onClose = function(){controller.closeScreen("Options")};
    }

}
