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

namespace Gamelogic.Grids.Examples
{
	public enum CellType
	{
		Blue,
		Green,
		Yellow,
		Red,
		Black
	}

	public class CellTypeExtensions
	{
		public static CellType ChooseRandom()
		{
			return (CellType) Random.Range(0, 4);
		}
	}

	public enum CellFunction
	{
		Board,
		Piece
	}

	public class TrianglePuzzleCell : TileCell
	{
		private static readonly Dictionary<CellType, Color> PieceColors = new Dictionary<CellType, Color>
		{
			{CellType.Blue, ExampleUtils.Colors[8]},
			{CellType.Green, ExampleUtils.Colors[9]},
			{CellType.Yellow, ExampleUtils.Colors[10]},
			{CellType.Red, ExampleUtils.Colors[11]},
			{CellType.Black, Color.black}
		};

		private static readonly Dictionary<CellType, Color> BoardColors = new Dictionary<CellType, Color>
		{
			{CellType.Blue, ExampleUtils.Colors[4]},
			{CellType.Green, ExampleUtils.Colors[5]},
			{CellType.Yellow, ExampleUtils.Colors[6]},
			{CellType.Red, ExampleUtils.Colors[7]},
			{CellType.Black, Color.black}
		};

		public SpriteRenderer mainSprite;
		public GameObject rotationPivot;

		private CellType cellCellType;
		private CellFunction cellCellFunction;
		private Color mainColor;


		public new void Awake()
		{
			cellCellType = CellType.Blue;
			cellCellFunction = CellFunction.Piece;
			UpdateColor();
		}

		public CellType CellType
		{
			get { return cellCellType; }

			set
			{
				cellCellType = value;
				UpdateColor();
			}
		}

		public CellFunction CellFunction
		{
			get { return cellCellFunction; }

			set
			{
				cellCellFunction = value;
				UpdateColor();
			}
		}

		private void UpdateColor()
		{
			Color = (cellCellFunction == CellFunction.Piece) ? PieceColors[cellCellType] : BoardColors[cellCellType];
		}

		private new void OnClick()
		{
		}

		public override Color Color
		{
			get { return mainColor; }
			set
			{
				mainColor = value;
				__UpdatePresentation(true);
			}
		}

		public override Vector2 Dimensions
		{
			get { return mainSprite.sprite.bounds.size; }
		}

		public override void __UpdatePresentation(bool forceUpdate)
		{
			mainSprite.color = mainColor;
		}

		public override void SetAngle(float angle)
		{
			rotationPivot.transform.SetLocalRotationZ(angle);
		}

		public override void AddAngle(float angle)
		{
			rotationPivot.transform.SetLocalRotationZ(rotationPivot.transform.localEulerAngles.z + angle);
		}
	}
}