package game.player1.one;


import static game.constant.GameConstant.CONTINUOUS_ROTATE;
import static game.constant.GameConstant.DOWN_CNT;
import static game.constant.GameConstant.NEXT_MAX;
import static java.awt.event.KeyEvent.VK_DOWN;
import static java.awt.event.KeyEvent.VK_LEFT;
import static java.awt.event.KeyEvent.VK_RIGHT;
import static java.awt.event.KeyEvent.VK_UP;
import static java.awt.event.KeyEvent.VK_X;
import static java.awt.event.KeyEvent.VK_Z;

import com.haru.component.GameField;
import com.haru.component.Mino;
import com.haru.component.NextNextField;
import com.haru.component.TetraMino;
import com.haru.component.gui.MainPanel;
import com.haru.input.KeyBind;
import com.haru.input.KeyBuffer;
import com.haru.player.Player;
import com.haru.util.ArrayRandom;

public class GameThreadOne extends GameThread {

	private KeyBuffer buffer;

	public GameThreadOne(MainPanel mainPanel) {
		super(mainPanel);

		KeyBind keyBind = new KeyBind(VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_Z, VK_X);
		buffer = new KeyBuffer(keyBind, CONTINUOUS_ROTATE);
		mainPanel.getBoardPanel().addKeyListener(buffer);
		mainPanel.getBoardPanel().requestFocus();
	}

	public GameThreadOne(MainPanel mainPanel, KeyBuffer buffer) {
		super(mainPanel);
		this.buffer = buffer;
	}


	//--------------------------------------------------------------------------//
	//--------------------------------------------------------------------------//
	private Player player;
	private GameField gameField;		// mainPanel内に隠蔽されているけど、get()を定義していない
	private NextNextField nextField;


	//--------------------------------------------------------------------------//
	// ネクスト用
	//--------------------------------------------------------------------------//
	private ArrayRandom r =new ArrayRandom( new int[] {
				Mino.T_I,
				Mino.T_O,
				Mino.T_S,
				Mino.T_Z,
				Mino.T_L,
				Mino.T_J,
				Mino.T_T
			}
		);
	private int next[] = new int[NEXT_MAX];
	private void next(NextNextField nextField) {

		// 前のmapの情報を消す
		for (int i = 0; i < next.length; i++)
			nextField.putBlock(i, Mino.T_EMPTY, TetraMino.getBlock(next[i]));


		// ネクストを新たに取得
		for (int i = 0; i < next.length - 1; i++)
			next[i] = next[i+1];
		next[next.length - 1] = r.getNext();

		for (int i = 0; i < next.length; i++)
			nextField.putBlock(i, next[i], TetraMino.getBlock(next[i]));
	}


	@Override
	public void gameinit() {
		// フィールドの準備
		gameField = new GameField();
		nextField = new NextNextField();
		getMainPanel().init(gameField, nextField);

		// プレイヤーの定義
		player = new Player(gameField);

		// ネクストの準備
		for (int i = 0; i < next.length; i++) {
			next[i] = r.getNext();
		}
		// ブロックの出現
		player.setCur( TetraMino.getBlock(next[0]) );
		next(nextField);

	}


	@Override
	protected boolean gameloop(int loop) {

		// プレイヤーの入力を読み取る
		int ret[] = buffer.read();

		// ブロックの位置を更新
		boolean isUpdate = player.update(ret, (loop % DOWN_CNT == 0));

		// 位置が確定した場合
		if (!isUpdate) {

			// 次のブロックを準備する
			player.setCur( TetraMino.getBlock( next[0] ) );
			next(nextField);
			loop = 0;
		}


		// DRAW
		getMainPanel().repaint();

		// デバッグ用
		if (buffer.f_enter) {
			buffer.f_enter = false;	// なくすとおもろい
			next(nextField);
			getMainPanel().repaint();
		}


		return true;
	}

	@Override
	protected void gameend() {

	}





	//--------------------------------------------------------------------------//
	// infoの状態確認
	public void disp(int[][] info) {
		int[][] masu = new int[5][5];
		// 中心
		masu[2][2] = 1;
		for (int i = 0; i < 3; i++) {
			int x = info[i][0] + 2;
			int y = info[i][1] + 2;
			masu[y][x] = 1;
		}
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.printf("%2d", masu[i][j]);
			}
			System.out.println();
		}
		System.out.println();
	}



}
