package eu.midnightdust.yaytris.ui;

import eu.midnightdust.yaytris.Settings;
import eu.midnightdust.yaytris.Translation;
import eu.midnightdust.yaytris.util.Difficulty;
import eu.midnightdust.yaytris.util.Language;

import javax.swing.*;
import java.awt.*;

import static eu.midnightdust.yaytris.Translation.t;
import static eu.midnightdust.yaytris.ui.TetrisUI.scale;
import static eu.midnightdust.yaytris.ui.TetrisUI.setFontScale;

public class SettingsMenu extends JPanel {
    final TetrisUI ui;

    SettingsMenu(int x, int y, int width, int height, TetrisUI ui) {
        this.ui = ui;
        this.setBounds(x, y, width, height);
        this.setLayout(null);

        this.add(new JLabel(t("settings.volume.music")));
        JSlider musicVolumeSlider = new JSlider(0, 100, Settings.musicVolume);
        musicVolumeSlider.addChangeListener(change -> {
            Settings.musicVolume = musicVolumeSlider.getValue();
            Settings.write();
        });
        this.add(musicVolumeSlider);

        this.add(new JLabel(t("settings.volume.sound")));
        JSlider soundVolumeSlider = new JSlider(0, 100, Settings.soundVolume);
        soundVolumeSlider.addChangeListener(change -> {
            Settings.soundVolume = soundVolumeSlider.getValue();
            Settings.write();
        });
        this.add(soundVolumeSlider);

        this.add(new JLabel(t("settings.window_scaling")));
        JSlider scaleSlider = new JSlider(100, 600, (int) (Settings.guiScale * 100));
        scaleSlider.addChangeListener(change -> {
            Settings.guiScale = scaleSlider.getValue() / 100f;
            Settings.write();
        });
        this.add(scaleSlider);

        this.add(new JLabel(t("settings.scale_speed")));
        JCheckBox speedCheckbox = new JCheckBox("Aktiviert", Settings.shouldScaleSpeed);
        speedCheckbox.addChangeListener(change -> {
            Settings.shouldScaleSpeed = speedCheckbox.isSelected();
            Settings.write();
        });
        this.add(speedCheckbox);

        this.add(new JLabel(t("settings.difficulty")));
        JComboBox<Difficulty> difficultySelector = new JComboBox<>(Difficulty.values());
        difficultySelector.setSelectedItem(Settings.difficulty);

        difficultySelector.addActionListener(action -> {
            Settings.difficulty = (Difficulty) difficultySelector.getSelectedItem();
            Settings.write();
        });
        this.add(difficultySelector);
        difficultySelector.setSize(difficultySelector.getBounds().width,scale(10));

        this.add(new JLabel(t("settings.language")));
        JComboBox<Language> languageSelector = new JComboBox<>(Language.values());
        languageSelector.setSelectedItem(Settings.language);

        languageSelector.addActionListener(action -> {
            Settings.language = (Language) languageSelector.getSelectedItem();
            Settings.write();
            Translation.load(Settings.language.locale);
            ui.openSettings(action);
        });
        this.add(languageSelector);
        languageSelector.setSize(languageSelector.getBounds().width,scale(10));

        JButton backButton = new JButton(t("ui.back"));
        backButton.addActionListener(ui::openMainMenu);
        this.add(backButton);
    }
    int labelAmount = 0;

    @Override
    public Component add(Component comp) {
        comp.setBounds(scale(60), scale(20+17*this.getComponentCount()-labelAmount*10), scale(100), scale(20));
        if (comp instanceof JLabel) {
            comp.setBounds(scale(60), scale(20+17*(this.getComponentCount())-labelAmount*10), scale(100), scale(7));
            labelAmount++;
        }
        if (comp instanceof JComponent) setFontScale((JComponent) comp);
        return super.add(comp);
    }
}
