﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss : MonoBehaviour {
	public GameObject whiteBlock;
	public GameObject[] blocks;

	public int maxRow = 20;
	public int maxCol = 10;

	int x0, y0;

	GameObject[,] board = null;
	GameObject x = null;

	float lastDown, speed = 1f;

	bool running = false, downning = false;

	Vector3 Board2Pos(int r, int c) {
		return new Vector3 (x0 + c, y0 + r, 0);
	}

	// khoi tao man hinh
	void initScene() {
		// tinh vi tri trai-duoi lam goc cua cac doi tuong
		x0 = -((maxCol + 2) / 2);
		y0 = -((maxRow + 2) / 2);

		// chuyen board ve goc trai-duoi
		transform.position = Board2Pos(0, 0);

		// tao mang doi tuong cac block
		board = new GameObject[maxRow + 2, maxCol + 2];

		// xoa trang toan bo mang
		for (int i = 0; i < maxRow + 2; i++)
			for (int j = 0; j < maxCol + 2; j++)
				board[i, j] = null;

		// cot
		for (int i = 0; i < maxRow + 2; i++) {
			// trai
			board[i, 0] = Instantiate(whiteBlock);
			board[i, 0].transform.position = Board2Pos(i, 0);
			// phai
			board[i, maxCol + 1] = Instantiate(whiteBlock);
			board[i, maxCol + 1].transform.position = Board2Pos(i, maxCol + 1);
		}
		// dong
		for (int j = 1; j <= maxCol; j++) {
			// duoi
			board[0, j] = Instantiate(whiteBlock);
			board[0, j].transform.position = Board2Pos(0, j);
			// tren
			board[maxRow + 1, j] = Instantiate(whiteBlock);
			board[maxRow + 1, j].transform.position = Board2Pos(maxRow + 1, j);
		}
	}

	bool notOK() {
		foreach (Transform t in x.transform) {
			int col = (int)(t.position.x + 0.1 - x0);
			int row = (int)(t.position.y + 0.1 - y0);
			if (board[row, col] != null)
				return true;
		}

		return false;
	}

	// dich sang trai
	bool left() {
		x.transform.position += Vector3.left;
		if (notOK()) {
			x.transform.position += Vector3.right;
			return false;
		}
		return true;
	}

	// dich sang phai
	bool right() {
		x.transform.position += Vector3.right;
		if (notOK()) {
			x.transform.position += Vector3.left;
			return false;
		}
		return true;
	}

	// xuong duoi
	bool down() {
		x.transform.position += Vector3.down;
		if (notOK()) {
			x.transform.position += Vector3.up;
			return false;
		}
		lastDown = Time.time;
		return true;
	}

	// xoay trai
	bool rotateLeft() {
		x.transform.Rotate(0, 0, 90);
		if (notOK()) {
			x.transform.Rotate(0, 0, -90);
			return false;
		}
		return true;
	}

	// xoay phai
	bool rotateRight() {
		x.transform.Rotate(0, 0, -90);
		if (notOK()) {
			x.transform.Rotate(0, 0, 90);
			return false;
		}
		return true;
	}

	bool fullRow(int r) {
		for (int j = 1; j <= maxCol; j++)
			if (board[r, j] == null)
				return false;
		return true;
	}

	void clearRow(int r) {
		for (int j = 1; j <= maxCol; j++)
			Destroy(board[r, j]);
		for (int i = r; i < maxRow; i++)
			for (int j = 1; j <= maxCol; j++) {
				board[i, j] = board[i + 1, j];
				if (board[i, j] != null)
					board[i, j].transform.position += Vector3.down;
			}
		for (int j = 1; j <= maxCol; j++)
			board[maxRow, j] = null;
	}

	void clearFullRow() {
		for (int i = 1; i <= maxRow; i++)
			while (fullRow(i))
				clearRow(i);
	}

	void printScr() {
		for (int i = maxRow + 1; i >= 0; i--) {
			string s = "";
			for (int j = 0; j <= maxCol + 1; j++)
				if (board[i, j] == null)
					s += " 0";
				else
					s += " 1";
			print(s);
		}
	}

	void landing() {
		while (x.transform.childCount > 0) {
			// lay block con
			Transform c = x.transform.GetChild(0);
			// dat block vao board
			int col = (int)(c.position.x + 0.1 - x0);
			int row = (int)(c.position.y + 0.1 - y0);
			board[row, col] = c.gameObject;
			// xoa block khoi khoi hien tai
			c.SetParent(transform);
		}
		// huy khoi hien tai
		Destroy(x);
		x = null;
		clearFullRow();
		printScr();
	}

	// khoi tiep theo
	void nextBlock() {
		if (x != null)
			landing();
		// tao ngau nhien mot khoi
		x = Instantiate(blocks [Random.Range(0, 6)], transform);
		x.transform.position = Board2Pos(maxRow - 2, maxCol / 2);
		// Game over
		if (notOK()) {
		}
	}

	// bat dau man choi
	void Start () {
		initScene();
		nextBlock();
		lastDown = Time.time;
	}
	
	// cap nhat trang thai
	void Update () {
		if (Input.GetKeyDown(KeyCode.LeftArrow))
			left();
		if (Input.GetKeyDown(KeyCode.RightArrow))
			right();
		if (Input.GetKeyDown(KeyCode.DownArrow))
			down();
		if (Input.GetKeyDown(KeyCode.UpArrow))
			rotateLeft();
		if (Input.GetKeyDown(KeyCode.End))
			rotateRight();
		if (downning || Input.GetKeyDown(KeyCode.Space))
			downning = down();
		if (lastDown + speed < Time.time) {
			if (!down()) {
				nextBlock();
				lastDown = Time.time;
			}
		}
	}
}