package ctbrec.ui.settings;

import java.io.IOException;
import java.io.InputStream;

import ctbrec.Config;
import ctbrec.Settings;
import ctbrec.ui.controls.Dialogs;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class PlayerSettingsDialog extends Dialog<Exception> {

    private Scene parent;
    private Config config;
    private Settings settings;

    private TextField playerParams;
    private TextField maxResolution;

    public PlayerSettingsDialog(Scene parent, Config config) {
        this.parent = parent;
        this.config = config;
        this.settings = config.getSettings();

        initGui();
    }

    private void initGui() {
        setTitle("Player Settings");
        setHeaderText("Player Settings");
        getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        initModality(Modality.APPLICATION_MODAL);
        setResizable(true);
        InputStream icon = Dialogs.class.getResourceAsStream("/icon.png");
        Stage stage = (Stage) getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(icon));
        if (parent != null) {
            stage.getScene().getStylesheets().addAll(parent.getStylesheets());
        }

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        grid.add(new Label("Start parameters"), 0, 0);
        playerParams = new TextField(settings.mediaPlayerParams);
        grid.add(playerParams, 1, 0);
        getDialogPane().setContent(grid);
        GridPane.setFillWidth(playerParams, true);
        GridPane.setHgrow(playerParams, Priority.ALWAYS);

        Label l = new Label("Maximum resolution (0 = unlimited)");
        grid.add(l, 0, 1);
        maxResolution = new TextField(Integer.toString(settings.maximumResolutionPlayer));
        Tooltip tt = new Tooltip("video height, e.g. 720 or 1080");
        l.setTooltip(tt);
        maxResolution.setTooltip(tt);
        grid.add(maxResolution, 1, 1);
        getDialogPane().setContent(grid);
        GridPane.setFillWidth(maxResolution, true);
        GridPane.setHgrow(maxResolution, Priority.ALWAYS);

        Platform.runLater(playerParams::requestFocus);

        setResultConverter(dialogButton -> {
            try {
                if (dialogButton == ButtonType.OK) {
                    saveSettings();
                }
                return null;
            } catch (IOException e) {
                return e;
            }
        });
    }

    public void saveSettings() throws IOException {
        settings.mediaPlayerParams = playerParams.getText();
        String res = maxResolution.getText();
        if (res.matches("\\d+")) {
            int newRes = Integer.parseInt(maxResolution.getText());
            if (newRes != Config.getInstance().getSettings().maximumResolutionPlayer) {
                settings.maximumResolutionPlayer = newRes;
            }
        }
        config.save();
    }
}
