package fr.monkhanny.dorfromantik.gui;

import fr.monkhanny.dorfromantik.Options;
import fr.monkhanny.dorfromantik.components.Title;
import fr.monkhanny.dorfromantik.listeners.*;
import fr.monkhanny.dorfromantik.utils.MusicPlayer;

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

public class SettingsPanel extends JPanel {

    private MainMenu mainMenu;
    private JFrame settingsFrame;

    public SettingsPanel(MainMenu mainMenu, JFrame settingsFrame) {
        this.mainMenu = mainMenu;
        this.settingsFrame = settingsFrame;

        initializeSettingsFrame();
        setupBackground();
        setupMainPanel();
    }

    private void initializeSettingsFrame() {
        settingsFrame.setMinimumSize(Options.MINIMUM_FRAME_SIZE);
    }

    private void setupBackground() {
        JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
        background.setLayout(new GridBagLayout());
        this.setLayout(new BorderLayout());
        this.add(background);
    }

    private void setupMainPanel() {
        JPanel mainPanel = createMainPanel();
        JLabel title = createTitle();

        mainPanel.add(title, createGridBagConstraints(0, 0, 2));
        mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 1, 1));

        // Musique Panel
        JSlider musicSlider = new JSlider(0, 100, Options.MUSIC_MUTED ? 0 : Options.MUSIC_VOLUME);
        JPanel musicPanel = createSoundPanel("Musique", musicSlider, new MusicVolumeChangeListener(musicSlider), new MuteCheckBoxListener("Musique"));
        mainPanel.add(musicPanel, createGridBagConstraints(0, 2, 1));

        mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 3, 1));

        // SFX Panel
        JSlider sfxSlider = new JSlider(0, 100, Options.SOUNDS_MUTED ? 0 : Options.SOUNDS_VOLUME);
        JPanel sfxPanel = createSoundPanel("SFX", sfxSlider, new SoundsVolumeChangeListener(sfxSlider), new MuteCheckBoxListener("SFX"));
        mainPanel.add(sfxPanel, createGridBagConstraints(0, 4, 1));

        // Close Button
        JButton closeButton = createCloseButton();
        mainPanel.add(closeButton, createGridBagConstraints(0, 5, 1));

        // Add to background
        ((JLabel) this.getComponent(0)).add(mainPanel);
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setOpaque(false);
        return mainPanel;
    }

    private Title createTitle() {
        Title title = new Title("Paramètres", 70, Color.WHITE);
        return title;
    }

    private GridBagConstraints createGridBagConstraints(int x, int y, int gridWidth) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = gridWidth;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(20, 30, 20, 30);
        return gbc;
    }

    private JPanel createSoundPanel(String labelText, JSlider volumeSlider, ChangeListener sliderChangeListener, MuteCheckBoxListener muteCheckBoxListener) {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.gridx = 0;

        JCheckBox muteCheckBox = new JCheckBox();
        muteCheckBox.setSelected(!("Musique".equals(labelText) ? Options.MUSIC_MUTED : Options.SOUNDS_MUTED));
        muteCheckBox.addActionListener(muteCheckBoxListener);

        panel.add(new JLabel(labelText), gbc);
        gbc.gridx++;
        panel.add(muteCheckBox, gbc);

        volumeSlider.setPreferredSize(new Dimension(200, 50));
        volumeSlider.setMajorTickSpacing(50);
        volumeSlider.setPaintTicks(true);
        volumeSlider.setPaintLabels(true);
        volumeSlider.setFont(new Font("Roboto", Font.PLAIN, 16));
        volumeSlider.addChangeListener(sliderChangeListener);

        gbc.gridx++;
        panel.add(createSliderPanel(volumeSlider), gbc);

        return panel;
    }

    private JPanel createSliderPanel(JSlider volumeSlider) {
        JPanel sliderPanel = new JPanel(new BorderLayout());
        sliderPanel.setOpaque(false);

        JLabel lowLabel = new JLabel("Low");
        lowLabel.setFont(new Font("Roboto", Font.PLAIN, 18));
        sliderPanel.add(lowLabel, BorderLayout.WEST);

        sliderPanel.add(volumeSlider, BorderLayout.CENTER);

        JLabel highLabel = new JLabel("High");
        highLabel.setFont(new Font("Roboto", Font.PLAIN, 18));
        sliderPanel.add(highLabel, BorderLayout.EAST);

        return sliderPanel;
    }

    private JButton createCloseButton() {
        JButton closeButton = new JButton("Retour");
        closeButton.setFont(new Font("Roboto", Font.BOLD, 24));
        closeButton.setForeground(Color.BLACK);
        closeButton.setBackground(Color.WHITE);
        closeButton.setOpaque(true);
        closeButton.setPreferredSize(new Dimension(75, 50));
        closeButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        closeButton.addActionListener(new CloseButtonListener(mainMenu, settingsFrame));
        return closeButton;
    }
}
