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

namespace Gamelogic.Grids.Examples
{
	public class Piece : GridBehaviour<FlatTriPoint>
	{
		public TrianglePuzzleCell cellPrefab;

		private PiecePoints piecePoints;
		private CellType cellType;

		private bool holdingCell = false;

		public TrianglePuzzleGame TrianglePuzzleGame { get; set; }

		public IEnumerable<FlatTriPoint> PiecePoints
		{
			get { return Grid; }
		}

		public CellType CellType
		{
			get { return cellType; }
			set
			{
				cellType = value;

				foreach (var point in Grid)
				{
					((TrianglePuzzleCell) Grid[point]).CellType = value;
				}
			}
		}

		public void Update()
		{
			if (holdingCell)
			{
				var newPos = ExampleUtils.ScreenToWorld(Input.mousePosition);
				newPos.z = -1;
				transform.position = newPos;
			}
		}

		public void OnClick(FlatTriPoint point)
		{
			if (!holdingCell)
			{
				holdingCell = true;
			}

			else
			{
				TrianglePuzzleGame.DropPiece(this);
				holdingCell = false;
			}
		}
	}
}