package tetris;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Tetris extends JFrame {
    private final ResourceManager resManager = ResourceManager.getInstance();
    private CardLayout cardLayout = new CardLayout();
    private JPanel container = new JPanel(cardLayout);

    public Tetris() {
        initUI();
        resManager.playPrepareMusic();
    }

    private void initUI() {
        setTitle("俄罗斯方块");
        setSize(510, 650);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        NextBlockPanel rightPanel = new NextBlockPanel();
        GamePanel gamePanel = new GamePanel(rightPanel);
        rightPanel.setGamePanel(gamePanel);

        PreparePanel preparePanel = new PreparePanel(() -> {
            cardLayout.show(container, "game");
            resManager.playBackgroundMusic();  // 只需调用这个方法即可
            gamePanel.requestFocusInWindow();
        });

        container.add(preparePanel, "prepare");
        container.add(createGameContainer(gamePanel, rightPanel), "game");

        add(container);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private JPanel createGameContainer(GamePanel gamePanel, NextBlockPanel rightPanel) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(gamePanel, BorderLayout.CENTER);
        panel.add(rightPanel, BorderLayout.EAST);
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Tetris::new);
    }
}