package com.example.flappybird.states;

import com.example.flappybird.FlappyBird;
import com.example.flappybird.GamePanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.net.URI;

public class MenuState extends GameState {
    public MenuState(GamePanel panel) {
        super(panel);
    }

    @Override
    public void paintGameState(Graphics g) {
        gamePanel.moveBase(g);
        gamePanel.paintBase(g);

        paintMenu(g);

        gameBird.menuFloat();
    }

    protected void paintMenu(Graphics g) {
        // Title
        g.drawImage(FlappyBird.textures.get("titleText").getImage(),
                FlappyBird.textures.get("titleText").getX(),
                FlappyBird.textures.get("titleText").getY(), null);

        // Buttons
        g.drawImage(FlappyBird.textures.get("playButton").getImage(),
                FlappyBird.textures.get("playButton").getX(),
                FlappyBird.textures.get("playButton").getY(), null);
        g.drawImage(FlappyBird.textures.get("leaderboard").getImage(),
                FlappyBird.textures.get("leaderboard").getX(),
                FlappyBird.textures.get("leaderboard").getY(), null);
        g.drawImage(FlappyBird.textures.get("rateButton").getImage(),
                FlappyBird.textures.get("rateButton").getX(),
                FlappyBird.textures.get("rateButton").getY(), null);

        // Credits :p
        paintCenteredText("Created by Paul Krishnamurthy", 600, g);
        g.setFont(FlappyBird.flappyMiniFont); // Change font
        paintCenteredText("www.PaulKr.com", 630, g);
    }

    /**
     * Draws a string centered based on given restrictions
     *
     * @param text String to be drawn
     * @param yPos Fixed y-coordinate
     */
    protected void paintCenteredText(String text, int yPos, Graphics g) {
        FontMetrics fm = g.getFontMetrics();

        // Calculate x-coordinate based on string length and width
        int x = (FlappyBird.WIDTH - fm.stringWidth(text)) / 2;
        g.drawString(text, x, yPos);
    }

    /**
     * Tries to open the review url in default web browser
     */
    protected void openReviewUrl() {
        try {
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().browse(new URI("http://paulkr.com"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Sorry could not open URL...");
        }

    }

    @Override
    public void handleKeyboardEvent(KeyEvent e) {
        int keyCode = e.getKeyCode();
        // Start game on 'enter' key
        if (keyCode == KeyEvent.VK_ENTER) {
            gamePanel.setGameState(new PlayState(gamePanel));
        }
    }

    @Override
    public void handleMouseEvent(MouseEvent e) {
        if (gamePanel.isTouching(FlappyBird.textures.get("playButton").getRect())) {
            gamePanel.setGameState(new PlayState(gamePanel));

        } else if (gamePanel.isTouching(FlappyBird.textures.get("leaderboard").getRect())) {
            // Dummy message
            JOptionPane.showMessageDialog(gamePanel,
                    "We can't access the leaderboard right now!",
                    "Oops!",
                    JOptionPane.ERROR_MESSAGE);
        }

        Rectangle rect = FlappyBird.textures.get("playButton").getRect();
        System.out.println("Texture Rect: x=" + rect.getX() + ", y=" + rect.getY() +
                ", width=" + rect.getWidth() + ", height=" + rect.getHeight());

        if (gamePanel.isTouching(FlappyBird.textures.get("rateButton").getRect())) {
            // Open website
            openReviewUrl();
        }
    }
}
