/*
 * IngredientQuantityScreen.fx
 */

package cookery;

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

import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import cookery_db.database.RecipeIngredient;

import cookery.control.ComboBox;

import cookery_db.database.QuantityUnit;

import javafx.geometry.VPos;

import java.lang.NumberFormatException;


/**
 * A screen that allows the quantity of a specific ingredient to be adjusted by the user.
 * @author Nick Apperley
 */
public class IngredientQuantityScreen extends Stage{
    public-init var controller: Controller;
    public-init var ingredient: RecipeIngredient;
    public var onIngredientQuantityChanged: function(iqse: IngredientQuantityScreenEvent);
    def quantityUnits: QuantityUnit[] = (controller.performAction("All quantity units", null) as QuantityUnit[]);
    def saveBtn = Button{
        text: "Save"
        action: saveIngredientQuantity
    };
    def closeBtn = Button{
        text: "Close"
        action: function(){close()}
    };
    def ingredientLbl = Label{text: "Ingredient:"};
    def ingredientName = Label{text: ingredient.getIngredient1().getName()};
    def amountLbl = Label{text: "Amount:"};
    def amountTxt = TextBox{text: String.valueOf(ingredient.getQuantity())};
    def unitLbl = Label{text: "Unit:"};
    def unitCboBox = ComboBox{
        items:
        for(unit in quantityUnits){
            unit.getUnit()
        }
    };

    def mainLayout = VBox{
        translateX: 5
        content: [
        HBox{
            nodeVPos: VPos.CENTER
            content: [ingredientLbl, ingredientName]
        },
        HBox{
            nodeVPos: VPos.CENTER
            content: [amountLbl, amountTxt]
        },
        HBox{
            nodeVPos: VPos.CENTER
            content: [unitLbl, unitCboBox]
        },
        HBox{
            content: [saveBtn, closeBtn]
            spacing: 5
            translateY: 25
        }]
    };

    /**
     * Checks to see if the quantity (the amount) is valid.
     @return True if the quantity is valid.
    */
    function quantityValid(): Boolean{
        var validQuantity = true;

        try{
            Double.parseDouble(amountTxt.text);
        }
        catch(ex: NumberFormatException){
            validQuantity = false;
        }
        return validQuantity;
    }

    /**
     * Saves all changes made to the quantity of the ingredient.
    */
    function saveIngredientQuantity(): Void{
        def quantityUnit: QuantityUnit = (controller.performAction("Get quantity unit", unitCboBox.getSelectedItem())[0] as QuantityUnit);

        if(quantityValid()){
            ingredient.setQuantity(Double.parseDouble(amountTxt.text));
            ingredient.setQuantityunit(quantityUnit);
            onIngredientQuantityChanged(IngredientQuantityScreenEvent{});
            close();
        }
    }

    init{
        width = 800;
        height = 600;
        scene = Scene{content: [mainLayout]};
    }

    postinit{
        for(item in unitCboBox.items){
            if(item == ingredient.getQuantityunit().getUnit()){
                unitCboBox.selectItem(indexof item);
                break;
            }
        }
    }
}

/**
 * An event that represents any changes that have been made
 * on the ingredient quantity screen.
*/
public class IngredientQuantityScreenEvent{
    public-read def changedIngredient: RecipeIngredient = ingredient;
}

