package main;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import audio.AudioPlayer;
import states.AppStateManager;
import states.MenuState;
import ui.NotificationManager;

public class AppPanel extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener {
	private static final long serialVersionUID = 1L;

	public static final int WIDTH = 800;
	public static final int HEIGHT = 650;

	private Thread thread;

	private BufferedImage img;
	private Graphics2D g2;

	private long startTime = System.nanoTime();
	private int fpsTarget = (int) (1000d / 60d);

	@Override
	public void addNotify() {
		super.addNotify();

		requestFocus();
		addKeyListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);

		setPreferredSize(new Dimension(WIDTH, HEIGHT));

		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		g2 = img.createGraphics();

		thread = new Thread(this, "Game Thread");
		thread.start();
	}

	@Override
	public void run() {

		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		
		AudioPlayer.sfx.put("item_collected", new AudioPlayer("/sfx/collect-item.wav", false));
		AudioPlayer.sfx.put("button_clicked", new AudioPlayer("/sfx/button_clicked.wav", false));
		AudioPlayer.sfx.put("white_hurt", new AudioPlayer("/sfx/got_hurt.wav", false));
		AudioPlayer.sfx.put("item_spawn", new AudioPlayer("/sfx/item_spawn.wav", false));
		AudioPlayer.sfx.put("cell_moved", new AudioPlayer("/sfx/cell_moved.wav", false));
		AudioPlayer.sfx.put("cell_explodes", new AudioPlayer("/sfx/cell_explodes.wav", false));
		AudioPlayer.sfx.put("cancer_splits", new AudioPlayer("/sfx/cancer_splits.wav", false));

		AppStateManager.setAppState(new MenuState());

		while (true) {
			long elapsed = (System.nanoTime() - startTime) / 1000000;
			if (elapsed > fpsTarget) {
				getGraphics().drawImage(img, 0, 0, WIDTH, HEIGHT, null);

				AppStateManager.getAppState().update();
				NotificationManager.getInstance().update();
				
				AppStateManager.getAppState().draw(g2);
				NotificationManager.getInstance().draw(g2);

				startTime = System.nanoTime();
			}
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		AppStateManager.getAppState().mouseMoved(e);
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {
		AppStateManager.getAppState().mousePressed(e);

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		AppStateManager.getAppState().mouseReleased(e);

	}

	@Override
	public void keyPressed(KeyEvent e) {
		AppStateManager.getAppState().keyPressed(e.getKeyCode());
	}

	@Override
	public void keyReleased(KeyEvent e) {
		AppStateManager.getAppState().keyReleased(e.getKeyCode());

		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			System.out.println("Gameloop successfully broke :)");
			System.exit(0);
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}
}
