using System.Collections.Generic;
using UnityEngine;

namespace Gamelogic.Grids.Examples
{
	public class DirectionalCell : SpriteCell
	{
		private readonly Dictionary<PointyHexPoint, Color> colorDict = new Dictionary<PointyHexPoint, Color>
		{
			{PointyHexPoint.NorthEast, ExampleUtils.Colors[0]},
			{PointyHexPoint.NorthWest, ExampleUtils.Colors[1]},
			{PointyHexPoint.West, ExampleUtils.Colors[3]},

			{PointyHexPoint.SouthWest, ExampleUtils.Colors[4]},
			{PointyHexPoint.SouthEast, ExampleUtils.Colors[5]},
			{PointyHexPoint.East, ExampleUtils.Colors[7]},
			{PointyHexPoint.Zero, Color.white}
		};

		private PointyHexPoint direction = PointyHexPoint.Zero;

		public bool IsGoal { get; private set; }

		public void SetGoalNode()
		{
			direction = PointyHexPoint.Zero;
			IsGoal = true;
			Color = Color.white;
			name = "Goal";
			FrameIndex = 1;
		}

		public void SetStartNode()
		{
			direction = PointyHexPoint.Zero;
			Color = Color.white;
			name = "Start";
			FrameIndex = 0;
		}

		public void SetDecisionNode()
		{
			direction = PointyHexPoint.Zero;
			name = "Decision";
			Color = ExampleUtils.Colors[2];
			FrameIndex = 2;
		}

		public void IncDirection()
		{
			if (direction == PointyHexPoint.East)
			{
				SetDirection(PointyHexPoint.NorthEast);
			}
			else if (direction == PointyHexPoint.NorthEast)
			{
				SetDirection(PointyHexPoint.NorthWest);
			}
			else if (direction == PointyHexPoint.NorthWest)
			{
				SetDirection(PointyHexPoint.West);
			}
			else if (direction == PointyHexPoint.West)
			{
				SetDirection(PointyHexPoint.SouthWest);
			}
			else if (direction == PointyHexPoint.SouthWest)
			{
				SetDirection(PointyHexPoint.SouthEast);
			}
			else if (direction == PointyHexPoint.SouthEast)
			{
				SetDirection(PointyHexPoint.East);
			}
		}

		public void SetDirection(PointyHexPoint newDirection)
		{
			name = "Arrows";
			transform.SetRotationZ(0);
			direction = newDirection;

			if (direction == PointyHexPoint.East)
			{
				transform.RotateAroundZ(0);
			}
			else if (direction == PointyHexPoint.NorthEast)
			{
				transform.RotateAroundZ(60);
			}
			else if (direction == PointyHexPoint.NorthWest)
			{
				transform.RotateAroundZ(120);
			}
			else if (direction == PointyHexPoint.West)
			{
				transform.RotateAroundZ(180);
			}
			else if (direction == PointyHexPoint.SouthWest)
			{
				transform.RotateAroundZ(240);
			}
			else if (direction == PointyHexPoint.SouthEast)
			{
				transform.RotateAroundZ(300);
			}
			FrameIndex = 3;
			UpdateColor();
		}

		public void UpdateColor()
		{
			Color = colorDict[direction];
		}

		public void SetRandomDirection()
		{
			var newDirection = new PointyHexPoint();

			switch (Random.Range(0, 6))
			{
				case 0:
					newDirection = PointyHexPoint.East;
					break;
				case 1:
					newDirection = PointyHexPoint.NorthEast;
					break;
				case 2:
					newDirection = PointyHexPoint.NorthWest;
					break;
				case 3:
					newDirection = PointyHexPoint.West;
					break;
				case 4:
					newDirection = PointyHexPoint.SouthWest;
					break;
				case 5:
					newDirection = PointyHexPoint.SouthEast;
					break;
			}

			SetDirection(newDirection);
		}

		public PointyHexPoint Direction
		{
			get { return direction; }
			set { direction = value; }
		}
	}
}