package main;

import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

import model.GameModel;
import model.GameStateManager;
import view.GameView;
import control.GameControl;

@SuppressWarnings("serial")
public class Window extends JFrame {

	// Window width and height
	public static final int WIDTH = 800;
	public static final int HEIGHT = 800;

	// Update and draw speed
	public static final int UPDATES_PER_SECOND = 30;
	public static final int FRAMES_PER_SECOND = 30;

	public Window() {
		// Open new window
		super("Bubble Trouble");
		setSize(WIDTH, HEIGHT);
		setResizable(false);
		setDefaultCloseOperation(3);

		// Start gamestate manager and MVC model
		GameStateManager gsm = new GameStateManager();
		GameModel model = new GameModel(gsm);
		GameView view = new GameView(model, gsm);
		GameControl control = new GameControl(view, model, gsm);

		// Keyboard
		addKeyListener(control);

		setContentPane(view);

		setVisible(true);

		// Start playing the theme song
		playAudio();
	}

	private void playAudio() {
		// Load theme song from resources and play
		Clip clip = null;
		try {
			clip = AudioSystem.getClip();
			AudioInputStream inputStream = AudioSystem.getAudioInputStream(Main.class.getResource("/resources/audio/soundtrack.wav"));
			clip.open(inputStream);
		} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
			clip.close();
		}

		// Loop the theme song
		clip.loop(Clip.LOOP_CONTINUOUSLY);
	}

}
