package ctbrec.ui.settings;

import ctbrec.recorder.postprocessing.CreateContactSheet;
import ctbrec.recorder.postprocessing.PostProcessor;
import ctbrec.ui.settings.api.Category;
import ctbrec.ui.settings.api.Preferences;
import ctbrec.ui.settings.api.Setting;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.ColorPicker;
import javafx.scene.paint.Color;

public class CreateContactSheetPaneFactory extends AbstractPostProcessingPaneFactory {

    private SimpleStringProperty background;

    @Override
    public Preferences doCreatePostProcessorPane(PostProcessor pp) {
        SimpleStringProperty totalSize = new SimpleStringProperty(null, CreateContactSheet.TOTAL_SIZE, pp.getConfig().getOrDefault(CreateContactSheet.TOTAL_SIZE, "1920"));
        SimpleStringProperty padding = new SimpleStringProperty(null, CreateContactSheet.PADDING, pp.getConfig().getOrDefault(CreateContactSheet.PADDING, "4"));
        SimpleStringProperty cols = new SimpleStringProperty(null, CreateContactSheet.COLS, pp.getConfig().getOrDefault(CreateContactSheet.COLS, "8"));
        SimpleStringProperty rows = new SimpleStringProperty(null, CreateContactSheet.ROWS, pp.getConfig().getOrDefault(CreateContactSheet.ROWS, "7"));
        SimpleStringProperty filename = new SimpleStringProperty(null, CreateContactSheet.FILENAME, pp.getConfig().getOrDefault(CreateContactSheet.FILENAME, "contactsheet.jpg"));
        background = new SimpleStringProperty(null, CreateContactSheet.BACKGROUND, pp.getConfig().getOrDefault(CreateContactSheet.BACKGROUND, "0x333333"));
        properties.add(totalSize);
        properties.add(padding);
        properties.add(cols);
        properties.add(rows);
        properties.add(filename);
        properties.add(background);

        Setting backgroundSetting = Setting.of("", background, "Hexadecimal value of the background color for the space between the thumbnails");
        Preferences prefs = Preferences.of(new MapPreferencesStorage(),
                Category.of(pp.getName(),
                        Setting.of("Total Width", totalSize, "Total width of the generated contact sheet"),
                        Setting.of("Padding", padding, "Padding between the thumbnails"),
                        Setting.of("Columns", cols ),
                        Setting.of("Rows", rows),
                        Setting.of("File Name", filename),
                        Setting.of("Background", createColorPicker(background.get())),
                        backgroundSetting
                        )
                );

        try {
            // hide the background color input field, because we use a color picker instead
            backgroundSetting.getGui().setVisible(false);
        } catch (Exception e) {
            // hiding the background color input field didn't work, that's ok
        }

        return prefs;
    }

    private ColorPicker createColorPicker(String hexColor) {
        Color preselection = Color.web(hexColor);
        ColorPicker colorPicker = new ColorPicker(preselection);
        colorPicker.setOnAction(e -> background.set(colorPicker.getValue().toString()));
        return colorPicker;
    }
}
