package com.haru.component.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.haru.component.Field;
import com.haru.component.Mino;

public class FieldPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	protected int unit = 20;// 1升目あたりのピクセル数
	protected Field field;




	public void setField(Field filed) {
		this.field = filed;
	}












	private Image minoImage;

	public FieldPanel() {
//		System.out.println(
//				getClass().getClassLoader().getResource("mino.png")
//		);
//		ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("mino.png"));

		ImageIcon icon = new ImageIcon("mino.png");
		minoImage = icon.getImage();

	}

	// nextFieldを追加すれば、Next用のパネルになる
	public void init(Field field) {
		this.field = field;

		int W = field.getWidth();
		int H = field.getHeight();
		setBackground(Color.WHITE);
		setSize(unit * W, unit * H);
	}

	public void paint( Graphics g ){
		super.paint(g);

		draw(g);
	}

	//---------------------------------------------------------------------//
	// filedの描画
	//---------------------------------------------------------------------//
	public void draw(Graphics g)	{
		int W = field.getWidth();
		int H = field.getHeight();

		// フィールドを埋めてから
		for (int x = 0; x < W; x ++) {
			for (int y = 0; y < H; y ++) {
				switch (field.getType(x, y)) {
				case Mino.T_EMPTY:
					g.setColor(Color.WHITE);
					break;
				case Mino.T_WALL:
					g.setColor(Color.LIGHT_GRAY);
					break;
				case Mino.T_FULL:
					g.setColor(Color.GREEN);
					break;
				case Mino.T_CURRENT:
					g.setColor(Color.CYAN);
					break;
				case Mino.T_DEBUG:
					g.setColor(Color.RED);
					break;
				case Mino.T_DOT:
					g.setColor(Color.BLACK);
					break;

				case Mino.T_I:
					g.setColor(Color.CYAN);
					break;
				case Mino.T_O:
					g.setColor(Color.YELLOW);
					break;
				case Mino.T_S:
					g.setColor(Color.GREEN);
					break;
				case Mino.T_Z:
					g.setColor(Color.RED);
					break;
				case Mino.T_L:
					g.setColor(Color.ORANGE);
					break;
				case Mino.T_J:
					g.setColor(Color.BLUE);
					break;
				case Mino.T_T:
					g.setColor(Color.MAGENTA);
					break;
				}

				g.fillRect(x*unit, y*unit, unit, unit);




				// Imageから読み込んで描画
				switch (field.getType(x, y)) {
				case Mino.T_I:
				case Mino.T_O:
				case Mino.T_S:
				case Mino.T_Z:
				case Mino.T_L:
				case Mino.T_J:
				case Mino.T_T:

					int cnt = field.getType(x, y) - Mino.T_I;

					final int SIZE = 14;


					g.drawImage(minoImage,
							x*unit, y*unit,
							x*unit + unit, y*unit + unit,
							cnt*SIZE, 0,
							cnt*SIZE + SIZE, SIZE,
							null);

					break;
				}


			}
		}
		// 線を引く
		g.setColor(Color.BLACK);
		for (int x = 0; x <= W; x ++) {	// 縦
			g.drawLine(x*unit, 0, x*unit, unit*H);
		}
		for (int y = 0; y <= H; y ++) {	// 横
			g.drawLine(0, y*unit, unit*W, y*unit);
		}
	}
}
