/**
 * FlappyBird.java
 * Main game class
 *
 * @author  Paul Krishnamurthy
 */

package com.example.flappybird;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;

public class FlappyBird implements ActionListener {

	GamePanel game;
	Timer gameTimer;

	// Game setup constants
	public static final int WIDTH  = 375;
	public static final int HEIGHT = 667;
	private static final int DELAY = 12;

	// Fonts
	public static Font flappyFontBase;
	public static Font flappyFontReal;
	public static Font flappyScoreFont;
	public static Font flappyMiniFont;

	// Audios
	public static Audio audio = new Audio();

	// Textures
	public static HashMap<String, Texture> textures = new Sprites().getGameTextures();

	// Boolean to show dark or light screen
	public static boolean darkTheme;

    public FlappyBird () {
        JFrame frame = new JFrame("Flappy Bird");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT);

		String fontPath = "/res/fonts/flappy-font.ttf";

		// Try to load ttf file
		try {
			InputStream fontStream = this.getClass().getResourceAsStream(fontPath);
            assert fontStream != null;

            InputStream is = new BufferedInputStream(fontStream);
			flappyFontBase = Font.createFont(Font.TRUETYPE_FONT, is);

			// Header and sub-header fonts
			flappyScoreFont = flappyFontBase.deriveFont(Font.PLAIN, 50);
			flappyFontReal  = flappyFontBase.deriveFont(Font.PLAIN, 20);
			flappyMiniFont  = flappyFontBase.deriveFont(Font.PLAIN, 15);
		} catch (Exception ex) {
			// Exit is font cannot be loaded
			ex.printStackTrace();
			System.err.println("Could not load Flappy Font!");
			System.exit(-1);
		}

		// Game timer
		gameTimer = new Timer(DELAY, this);
		gameTimer.start();

		// Get current hour with Calendar
		// If it is past noon, use the dark theme
		Calendar cal = Calendar.getInstance();
		int currentHour = cal.get(Calendar.HOUR_OF_DAY);
		darkTheme = currentHour > 20 || currentHour < 6;
  
  		// Add Panel to Frame
		game = new GamePanel();
		frame.add(game);

		// Set game icon
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("res/img/icons.png"));

		frame.setResizable(false);
		frame.setVisible(true);
	}

	public void actionPerformed (ActionEvent e) {
		if (game != null && game.ready) {
			game.repaint();
		}
	}

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

