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

namespace Gamelogic.Words.Examples
{
	public class LetterCell : UIImageTextCell
	{
		public Image centerBubble;
		public Image[] bubbles;

		public Color unusedColor;
		public Color usedColor;

		private bool used;
		public bool Used
		{
			get
			{
				return used;
			}

			set
			{
				used = value;
			
				centerBubble.color = used ? usedColor : unusedColor;

				foreach (var bubble in bubbles)
				{
					bubble.color = used? usedColor : unusedColor;
				}
			}
		}

		private readonly Dictionary<FlatHexPoint, float> bubbleRotations =
			new Dictionary<FlatHexPoint, float>(new PointComparer<FlatHexPoint>())
			{
				{FlatHexPoint.South, 0},
				{FlatHexPoint.SouthEast, 60},
				{FlatHexPoint.NorthEast, 120},
				{FlatHexPoint.North, 180},
				{FlatHexPoint.NorthWest, 240},
				{FlatHexPoint.SouthWest, 300},
			};
	
		public void SetStateOn(IList<FlatHexPoint> neighbors)
		{
			int neighborCount = neighbors.Count();

			centerBubble.gameObject.SetActive(true);

			for (int i = 0; i < bubbles.Length; i++)
			{
				bubbles[i].gameObject.SetActive(i < neighborCount);

				if (i < neighborCount)
				{
					if (!bubbleRotations.ContainsKey(neighbors[i])) Debug.Log(neighbors[i]);
				
					bubbles[i].transform.SetLocalRotationZ(bubbleRotations[neighbors[i]]);
				}
			}
		}

		public void SetStateOn()
		{
			centerBubble.gameObject.SetActive(true);

			foreach (var image in bubbles)
			{
				image.gameObject.SetActive(false);
			}
		}

		public void SetStateOff()
		{
			centerBubble.gameObject.SetActive(false);

			for (int i = 0; i < bubbles.Length; i++)
			{
				bubbles[i].gameObject.SetActive(false);
			}
		}
	}
}