import javax.sound.sampled.*;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

public class SpaceInvaders extends JFrame {
    public SpaceInvaders() {
        SpaceInvadersModel model = new SpaceInvadersModel();
        SpaceInvadersView view = new SpaceInvadersView(model);
        SpaceInvadersController controller = new SpaceInvadersController(model, view);

        JMenuBar menuBar = new JMenuBar();

        JMenu gameMenu = new JMenu("Game");
        JMenuItem playMenuItem = new JMenuItem("Play");
        JMenuItem pauseMenuItem = new JMenuItem("Pause");
        JMenuItem restartMenuItem = new JMenuItem("Restart");
        JMenu levels = new JMenu("Levels");
        JMenuItem nextLevel = new JMenuItem("Next Level");
        JMenuItem previousLevel = new JMenuItem("Previous Level");
        JMenu difficultyMenu = new JMenu("Difficulty");
        JMenuItem difficulty1MenuItem = new JMenuItem("1");
        JMenuItem difficulty2MenuItem = new JMenuItem("2");
        JMenuItem difficulty3MenuItem = new JMenuItem("3");
        JMenuItem quitMenuItem = new JMenuItem("Quit");

        JMenu saveLoad = new JMenu("Save/Load");
        JMenuItem saveItem = new JMenuItem("Save");
        JMenuItem loadItem = new JMenuItem("Load");

        gameMenu.add(playMenuItem);
        gameMenu.add(pauseMenuItem);
        gameMenu.add(restartMenuItem);
        difficultyMenu.add(difficulty1MenuItem);
        difficultyMenu.add(difficulty2MenuItem);
        difficultyMenu.add(difficulty3MenuItem);
        gameMenu.add(difficultyMenu);
        gameMenu.add(quitMenuItem);
        menuBar.add(gameMenu);
        levels.add(nextLevel);
        levels.add(previousLevel);
        saveLoad.add(saveItem);
        saveLoad.add(loadItem);
        menuBar.add(levels);
        menuBar.add(saveLoad);

        try {
            File soundFile = new File("resources/OST.wav");
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            throw new RuntimeException(e);
        }

        playMenuItem.addActionListener(e -> model.setRunning(true));

        pauseMenuItem.addActionListener(e -> model.setRunning(false));

        restartMenuItem.addActionListener(e -> {
            model.resetGameState();
            model.setRunning(true);
        });

        difficulty1MenuItem.addActionListener(e -> {
            for (Enemy enemy : model.getEnemies()) {
                enemy.setDx(0.5);
            }
            model.setScoreUnitIncrease(10);
        });

        difficulty2MenuItem.addActionListener(e -> {
            for (Enemy enemy : model.getEnemies()) {
                enemy.setDx(1.0);
            }
            model.setScoreUnitIncrease(15);
        });

        difficulty3MenuItem.addActionListener(e -> {
            for (Enemy enemy : model.getEnemies()) {
                enemy.setDx(2.0);
            }
            model.setScoreUnitIncrease(20);
        });

        quitMenuItem.addActionListener(e -> System.exit(1));

        nextLevel.addActionListener(e -> {
            model.setLevelCounter(model.getLevelCounter() + 1);
            model.nextLevel();
            model.setRunning(false);
        });

        previousLevel.addActionListener(e -> {
            model.setLevelCounter(model.getLevelCounter() - 1);
            model.previousLevel();
            model.setRunning(false);
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setJMenuBar(menuBar);
        setTitle("Space Invaders");
        setResizable(false);
        add(view);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        addKeyListener(controller);
    }

    public static void main(String[] args) {
        new SpaceInvaders();
    }
}
