import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;

import javax.imageio.ImageIO;

public class Config {
    public static Config getInstance() {
        return instance;
    }

    public int getCellWidth() {
        return cellWidth;
    }

    public void setCellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
    }

    public int getCellHeight() {
        return cellHeight;
    }

    public void setCellHeight(int cellHeight) {
        this.cellHeight = cellHeight;
    }

    public int getGroundColumnCount() {
        return groundColumnCount;
    }

    public void setGroundColumnCount(int groundColumnCount) {
        this.groundColumnCount = groundColumnCount;
    }

    public int getGroundRowCount() {
        return groundRowCount;
    }

    public void setGroundRowCount(int groundRowCount) {
        this.groundRowCount = groundRowCount;
    }
    
    public int getNextShapesCount() {
        return nextShapesCount;
    }

    public void setNextShapesCount(int nextShapesCount) {
        this.nextShapesCount = nextShapesCount;
    }

    public Color getRandomColor() {
        return colors.get(random.nextInt(colors.size()));
    }

    public Image getRandomImage() {
        return images.get(random.nextInt(images.size()));
    }

    public Color getGridColor() {
        return gridColor;
    }
    
    public Color getBackgroundColor() {
        return backgroundColor;
    }
    
    public Image getGameOverImage() {
        return gameOverImage;
    }
    
    public Image getBackgroundImage() {
        return backgroundImage;
    }
    
    public BufferedImage getNumbersImage() {
        return numbersImage;
    }
    
    public int getNumberWidth() {
        return numberWidth;
    }
    
    public int getNumberHeight() {
        return numberHeight;
    }
    
    public int getDelay() {
        return delay;
    }
    
    public int getAcceleration() {
        return accelerattion;
    }
    
    public int getAccelerateStep() {
        return accelerateStep;
    }
    
    public boolean isBackgroundDrawn() {
        return backgroundDrawn;
    }
    
    public void setBackgroundDrawn(boolean backgroundDrawn) {
        this.backgroundDrawn = backgroundDrawn;
    }

    public static Image loadImage(String path) {
        //URL url = GamePanel.class.getClassLoader().getResource(path);
        //return (url == null) ? null : (new ImageIcon(url).getImage());
        
        // 加载非jar包中的图片时使用
        try {
            return ImageIO.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    public static BufferedImage loadBufferedImage(String path) {
        /*URL url = Config.class.getClassLoader().getResource(path);
        if (url == null) { return null; }
        
        try {
            return ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        
        // 加载非jar包中的图片时使用
        try {
            return ImageIO.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    public static int parseInt(String intStr, int defaultValue) {
        try {
            return Integer.parseInt(intStr);
        } catch (NumberFormatException e) {
        }
        
        return defaultValue;
    }
    
    private Config() {
        try {
            Properties props = new Properties();
            InputStream in = new FileInputStream("config.properties");
            props.load(in);
            
            // 小方块和Ground设置
            cellWidth = parseInt(props.getProperty("cellWidth"), 30);
            cellHeight = parseInt(props.getProperty("cellHeight"), 30);
            groundRowCount = parseInt(props.getProperty("groundRowCount"), 17);
            groundColumnCount = parseInt(props.getProperty("groundColumnCount"), 10);
            nextShapesCount = parseInt(props.getProperty("nextShapesCount"), 4);
            
            // 速度设置
            delay = parseInt(props.getProperty("delay"), 400);
            accelerattion = parseInt(props.getProperty("accelerattion"), 5);
            accelerateStep = parseInt(props.getProperty("accelerateStep"), 20);
            
            // 背景设置
            backgroundImage = loadImage(props.getProperty("backgroundImage"));
            backgroundDrawn = Boolean.parseBoolean(props.getProperty("backgroundDrawn"));
            gameOverImage = loadImage(props.getProperty("gameOverImage"));
            
            // 数字设置
            numbersImage = loadBufferedImage(props.getProperty("numbersImage"));
            numberWidth = parseInt(props.getProperty("numberWidth"), 12);
            numberHeight = parseInt(props.getProperty("numberHeight"), 20);
            
            // 主题设置
            images = new ArrayList<Image>();
            String themeDir = props.getProperty("themeDir");
            String[] themeImageNames = props.getProperty("themeImages").split(",");
            
            for (String name : themeImageNames) {
                Image temp = loadImage(themeDir + "/" + name.trim());
                if (temp != null) {
                    images.add(temp);
                }
            }
            
            // 方块的颜色
            colors = new ArrayList<Color>();
            colors.add(Color.BLUE);
            colors.add(Color.WHITE);
            colors.add(Color.GREEN);
            colors.add(Color.ORANGE);
            colors.add(Color.LIGHT_GRAY);
            colors.add(Color.PINK);
            colors.add(Color.MAGENTA);
        } catch (Exception e) {
            throw new ExceptionInInitializerError("No configuration file found.");
        }
    }

    private int cellWidth = 30; // 每个方块单元的宽度
    private int cellHeight = 30;// 每个方块单元的高度
    private int groundRowCount = 17; // Ground的行数
    private int groundColumnCount = 10; // Ground的列数
    private int nextShapesCount = 4;
    private int delay = 400; // 初始下落速度为400毫秒
    private int accelerattion = 5; // 加速度
    private int accelerateStep = 20; // 当得分增加10时，加速一次.

    private ArrayList<Color> colors; // 方块预定义的颜色
    private ArrayList<Image> images; // 方块预定义的图片
    
    private Image gameOverImage; // 游戏结束时显示的信息
    private Image backgroundImage;
    private boolean backgroundDrawn = true;
    
    private BufferedImage numbersImage;
    private int numberWidth = 12;
    private int numberHeight = 20;
    
    private Color gridColor = Color.BLACK; // 网格线的颜色
    private Color backgroundColor = Color.BLACK; // 网格线的颜色
    
    private Random random = new Random(System.currentTimeMillis());
    private static Config instance = new Config();
}
