package sample;

import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ChoiceBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;

import java.sql.SQLException;
import java.util.ArrayList;

public class Controller {
    private static final String CLASS_NAME = Controller.class.getName();
    private DataHandler dataHandler = null;

    @FXML
    public ChoiceBox<ChoiceValue> fxChoiceBox;
    public Canvas fxCanvas;

    @FXML
    public void initialize() {
        try {
            dataHandler = new DataHandler("sebse.de", 3306, "db_vorlesung", "db_vorlesung", "db_vorlesung");
            ArrayList<ChoiceValue> choices = dataHandler.getPresentations();
            fxChoiceBox.getItems().addAll(choices);
        } catch (SQLException e) {
            Log.severe(CLASS_NAME, "SQL-Fehler!");
            e.printStackTrace();
        }


        GraphicsContext context = fxCanvas.getGraphicsContext2D();

        fxChoiceBox.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> observableValue, Number number, Number t1) -> {

            if (dataHandler != null) {
                try {
                    HallDimens dimens = dataHandler.getHallDimentions(fxChoiceBox.getItems().get(t1.intValue()).getKey());
                    Log.finer(CLASS_NAME, dimens != null);
                    if (dimens != null) {
                        Log.finer(CLASS_NAME, dimens.toString());

                        double width = fxCanvas.getWidth()/(dimens.getWidth()+2);
                        double height = fxCanvas.getHeight()/(dimens.getHeight()+4);

                        context.setFill(Color.LAVENDER);
                        context.fillRect(0, 0, fxCanvas.getWidth(), fxCanvas.getHeight());

                        context.setStroke(Color.BLACK);
                        context.setFill(Color.BLACK);
                        for (ChairIterator iter = dataHandler.getChairIterator(fxChoiceBox.getItems().get(t1.intValue()).getKey()); iter.hasNext(); ) {
                            Chair chair = iter.next();
                            if (chair.isUsed())
                                context.fillRect(width*chair.getPos(), height*chair.getRow(), width, height);
                            else
                                context.strokeRect(width*chair.getPos(), height*chair.getRow(), width, height);
                        }

                        context.fillRect(width, height*(dimens.getHeight()+2), width*dimens.getWidth(), height);
                        context.setStroke(Color.RED);
                        context.setTextAlign(TextAlignment.CENTER);
                        context.strokeText("Leinwand", fxCanvas.getWidth()/2, height*(dimens.getHeight()+3));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });

        context.setFill(Color.LAVENDER);
        context.fillRect(0, 0, fxCanvas.widthProperty().doubleValue(), fxCanvas.heightProperty().doubleValue());
    }
}
