package tetris;

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ResourceManager {
    private static ResourceManager instance;
    private final Map<Integer, Image> blockImages = new HashMap<>();
    private Clip backgroundMusic;
    private Image prepareBackground;
    private Clip prepareMusic;
    private Image gameBackground; // 新增游戏背景图
    private Clip clearSound;  // 新增消除音效

    private ResourceManager() {
        loadResources();
    }

    public static ResourceManager getInstance() {
        if (instance == null) instance = new ResourceManager();
        return instance;
    }

    private void loadResources() {
        loadBlockImages();
        loadPrepareResources();
        loadGameResources();
        loadBackgroundMusic();
        loadClearSound();  // 新增加载消除音效
    }

    // 新增消除音效加载方法
    private void loadClearSound() {
        try (InputStream is = getClass().getResourceAsStream("/sounds/clear.wav")) {
            if (is != null) {
                System.out.println("成功加载消除音效");  // 调试输出
                AudioInputStream audioIn = AudioSystem.getAudioInputStream(is);
                clearSound = AudioSystem.getClip();
                clearSound.open(audioIn);
            } else {
                System.err.println("消除音效文件未找到");  // 错误提示
            }
        } catch (Exception e) {
            showError("消除音效加载失败: " + e.getMessage());
        }
    }

    // 新增消除音效播放方法
    public void playClearSound() {
        if (clearSound != null) {
            clearSound.stop();
            clearSound.setFramePosition(0);
            clearSound.start();
        }
    }

    private void loadGameResources() {
        try {
            ImageIcon gameBg = new ImageIcon(getClass().getResource("/images/game_bg.jpg")); // 假设游戏背景图路径为 /images/game_bg.jpg
            gameBackground = gameBg.getImage();
        } catch (Exception e) {
            showError("游戏背景资源加载失败: " + e.getMessage());
        }
    }

    private void loadPrepareResources() {
        try {
            ImageIcon prepareBg = new ImageIcon(getClass().getResource("/images/prepare_bg.jpg"));
            prepareBackground = prepareBg.getImage();

            InputStream is = getClass().getResourceAsStream("/sounds/prepare.wav");
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(is);
            prepareMusic = AudioSystem.getClip();
            prepareMusic.open(audioIn);
        } catch (Exception e) {
            showError("准备资源加载失败: " + e.getMessage());
        }
    }

    private void loadBlockImages() {
        try {
            for (int i = 0; i < 7; i++) {
                String path = "/images/blocks/block_" + i + ".png";
                ImageIcon icon = new ImageIcon(getClass().getResource(path));
                if (icon.getImageLoadStatus() != MediaTracker.COMPLETE) {
                    showError("图片加载失败: " + path);
                    continue;
                }
                blockImages.put(i, icon.getImage());
            }
        } catch (Exception e) {
            showError("图片加载异常: " + e.getMessage());
        }
    }

    private void loadBackgroundMusic() {
        try (InputStream is = getClass().getResourceAsStream("/sounds/background.wav")) {
            if (is == null) {
                showError("音乐文件未找到");
                return;
            }
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(is);
            backgroundMusic = AudioSystem.getClip();
            backgroundMusic.open(audioIn);
            backgroundMusic.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (Exception e) {
            showError("音乐播放失败: " + e.getMessage());
        }
    }

    public Image getBlockImage(int type) {
        return blockImages.getOrDefault(type % 7, blockImages.get(0));
    }

    public void playBackgroundMusic() {
        if (backgroundMusic != null) {
            stopMusic(); // 确保停止所有正在播放的音乐
            backgroundMusic.setFramePosition(0);
            backgroundMusic.start();
        }
    }

    public void stopMusic() {
        // 同时停止背景音乐和准备音乐
        if (backgroundMusic != null && backgroundMusic.isRunning()) {
            backgroundMusic.stop();
        }
        stopPrepareMusic(); // 新增：同时停止准备音乐
    }

    private void showError(String msg) {
        JOptionPane.showMessageDialog(null, msg, "资源错误", JOptionPane.ERROR_MESSAGE);
    }

    public Image getPrepareBackground() {
        return prepareBackground;
    }

    public void playPrepareMusic() {
        if (prepareMusic != null) {
            prepareMusic.loop(Clip.LOOP_CONTINUOUSLY);
        }
    }

    public void stopPrepareMusic() {
        if (prepareMusic != null && prepareMusic.isRunning()) {
            prepareMusic.stop();
        }
    }

    public Image getGameBackground() {
        return gameBackground;
    }
}