package ch.zhaw.prog2.fxmlcalculator;

import ch.zhaw.prog2.fxmlcalculator.IsObservable;
import ch.zhaw.prog2.fxmlcalculator.IsObserver;
import ch.zhaw.prog2.fxmlcalculator.ValueHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * Adds observable functionality to one Value Handler.
 * The decorator uses the original methods of the WordModel-object.
 * @author bles
 *
 */
public class ValueHandlerDecorator implements IsObservable {
    private final ValueHandler valueHandler;
    private List<IsObserver> listener = new ArrayList<>();

    public ValueHandlerDecorator(ValueHandler valueHandler) {
        this.valueHandler = valueHandler;
    }

    @Override
    public void addListener(IsObserver observer) {
        listener.add(observer);
    }

    @Override
    public void removeListener(IsObserver observer) {
        listener.remove(observer);
    }

    public void checkValuesAndCalculateResult(String initialAmount, String returnInPercent, String annualCost, String numberOfYears) {
        valueHandler.checkValuesAndCalculateResult(initialAmount, returnInPercent, annualCost, numberOfYears);
        informListener();
    }

    public void showHelp(String text) {
        valueHandler.setResultBound(text);
        informListener();
    }

    public void clearResult(){
        valueHandler.clearResult();
        informListener();
    }

    public void informListener() {
        for(IsObserver observer : listener) {
            observer.update();
        }
    }

}
