/**
 * GamePanel.java
 * Main game panel
 *
 * @author  Paul Krishnamurthy
 */

package com.example.flappybird;

import com.example.flappybird.states.GameState;
import com.example.flappybird.states.MenuState;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GamePanel extends JPanel implements KeyListener, MouseListener {
	// Textures
	private GameState gameState;

	// If game has loaded
	public boolean ready = false;

	// Store point when player clicks
	protected Point clickedPoint = new Point(-1, -1);

	// Background
	protected static int[] backgroundPosition = { 0, 435 };

	public GamePanel () {
		setGameState(new MenuState(this));

		// Make the panel focusable
		setFocusable(true);
		requestFocusInWindow();

		gameState.restart(); // Reset game variables

		// Input listeners
		addKeyListener(this);
		addMouseListener(this);
	}

	/**
	 * Sets the current game state.
	 * @param state New GameState
	 */
	public void setGameState(GameState state) {
		System.out.println("Switching to state: " + state.getClass().getName());
		this.gameState = state;
		repaint();
	}

	/**
	 * To start game after everything has been loaded
	 */
	@Override
	public void addNotify() {
		super.addNotify();
		requestFocusInWindow();
		ready = true;
	}

	public void moveBase(Graphics g) {
		// Set font and color
		g.setFont(FlappyBird.flappyFontReal);
		g.setColor(Color.white);

		// Only move screen if bird is alive
		if (GameState.gameBird.isAlive()) {
			// Move base
			// Moving base effect
			int baseSpeed = 2;
			backgroundPosition[0] = backgroundPosition[0] - baseSpeed < -435 ? 435 : backgroundPosition[0] - baseSpeed;
			backgroundPosition[1] = backgroundPosition[1] - baseSpeed < -435 ? 435 : backgroundPosition[1] - baseSpeed;
		}

		// Background
		g.drawImage(FlappyBird.darkTheme ? FlappyBird.textures.get("background2").getImage() :
				FlappyBird.textures.get("background1").getImage(), 0, 0, null);

		// Draw bird
		GameState.gameBird.paintBird(g);
	}

	/**
	 * Needs to be called differently based on screen
	 */
	public void paintBase(Graphics g) {
		// Moving base effect
		g.drawImage(FlappyBird.textures.get("base").getImage(), backgroundPosition[0], FlappyBird.textures.get("base").getY(), null);
		g.drawImage(FlappyBird.textures.get("base").getImage(), backgroundPosition[1], FlappyBird.textures.get("base").getY(), null);

	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		gameState.paintGameState(g);
	}

	//////////////////////
	// Keyboard actions //
	//////////////////////

	public void keyTyped (KeyEvent e) {}
	public void keyReleased (KeyEvent e) {}

	public void keyPressed(KeyEvent e) {
		System.out.println("Key pressed: " + e.getKeyCode());
		gameState.handleKeyboardEvent(e);
	}

	///////////////////
	// Mouse actions //
	///////////////////

	/**
	 * Checks if point is in rectangle
	 *
	 * @param r Rectangle
	 * @return Boolean if point collides with rectangle
	 */
	public boolean isTouching (Rectangle r) {
		return r.contains(clickedPoint);
	}

	public void mouseExited (MouseEvent e) {}
	public void mouseEntered (MouseEvent e) {}
	public void mouseReleased (MouseEvent e) {}
	public void mouseClicked (MouseEvent e) {}

	public void mousePressed(MouseEvent e) {
		clickedPoint = e.getPoint();
		System.out.println("Mouse clicked at: " + e.getPoint());
		gameState.handleMouseEvent(e);
	}
}

