package tetris;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class PreparePanel extends JPanel {
    private final ResourceManager res;
    private Runnable startCallback;

    public PreparePanel(Runnable startGame) {
        this.res = ResourceManager.getInstance();
        this.startCallback = startGame;
        setPreferredSize(new Dimension(510, 650));
        initControls();
    }

    private void initControls() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    res.stopPrepareMusic();
                    startCallback.run();
                }
            }
        });
        setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(res.getPrepareBackground(), 0, 0, getWidth(), getHeight(), this);

        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑", Font.BOLD, 24));
        String text = "按空格键开始游戏";
        int textWidth = g.getFontMetrics().stringWidth(text);
        g.drawString(text, (getWidth() - textWidth) / 2, getHeight() - 80);
    }
}