using System;
#if UNITY_EDITOR
using UnityEditor;

#endif

namespace UnityEngine.Tilemaps
{
	[Serializable]
	public class PipelineTile : TileBase
	{
		[SerializeField] public Sprite[] m_Sprites;

		public override void RefreshTile(Vector3Int location, ITilemap tileMap)
		{
			for (var yd = -1; yd <= 1; yd++)
			for (var xd = -1; xd <= 1; xd++)
			{
				var position = new Vector3Int(location.x + xd, location.y + yd, location.z);
				if (TileValue(tileMap, position))
				{
					tileMap.RefreshTile(position);
				}
			}
		}

		public override void GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData)
		{
			UpdateTile(location, tileMap, ref tileData);
		}

		private void UpdateTile(Vector3Int location, ITilemap tileMap, ref TileData tileData)
		{
			tileData.transform = Matrix4x4.identity;
			tileData.color = Color.white;

			var mask = TileValue(tileMap, location + new Vector3Int(0, 1, 0)) ? 1 : 0;
			mask += TileValue(tileMap, location + new Vector3Int(1, 0, 0)) ? 2 : 0;
			mask += TileValue(tileMap, location + new Vector3Int(0, -1, 0)) ? 4 : 0;
			mask += TileValue(tileMap, location + new Vector3Int(-1, 0, 0)) ? 8 : 0;

			var index = GetIndex((byte)mask);
			if (index >= 0 && index < m_Sprites.Length && TileValue(tileMap, location))
			{
				tileData.sprite = m_Sprites[index];
				tileData.transform = GetTransform((byte)mask);
				tileData.flags = TileFlags.LockTransform | TileFlags.LockColor;
				tileData.colliderType = Tile.ColliderType.Sprite;
			}
		}

		private bool TileValue(ITilemap tileMap, Vector3Int position)
		{
			var tile = tileMap.GetTile(position);
			return tile != null && tile == this;
		}

		private int GetIndex(byte mask)
		{
			switch (mask)
			{
				case 0: return 0;

				case 3:
				case 6:
				case 9:
				case 12: return 1;

				case 1:
				case 2:
				case 4:
				case 5:
				case 10:
				case 8: return 2;

				case 7:
				case 11:
				case 13:
				case 14: return 3;

				case 15: return 4;
			}

			return -1;
		}

		private Matrix4x4 GetTransform(byte mask)
		{
			switch (mask)
			{
				case 9:
				case 10:
				case 7:
				case 2:
				case 8:
					return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -90f), Vector3.one);

				case 3:
				case 14:
					return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -180f), Vector3.one);

				case 6:
				case 13:
					return Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, -270f), Vector3.one);
			}

			return Matrix4x4.identity;
		}

#if UNITY_EDITOR
		[MenuItem("Assets/Create/Pipeline Tile")]
		public static void CreatePipelineTile()
		{
			var path = EditorUtility.SaveFilePanelInProject("Save Pipeline Tile", "New Pipeline Tile", "asset", "Save Pipeline Tile", "Assets");

			if (path == "")
			{
				return;
			}

			AssetDatabase.CreateAsset(CreateInstance<PipelineTile>(), path);
		}
#endif
	}

#if UNITY_EDITOR
	[CustomEditor(typeof(PipelineTile))]
	public class PipelineTileEditor : Editor
	{
		private PipelineTile tile => target as PipelineTile;

		public void OnEnable()
		{
			if (tile.m_Sprites == null || tile.m_Sprites.Length != 5)
			{
				tile.m_Sprites = new Sprite[5];
			}
		}

		public override void OnInspectorGUI()
		{
			EditorGUILayout.LabelField("Place sprites shown based on the number of tiles bordering it.");
			EditorGUILayout.Space();

			EditorGUI.BeginChangeCheck();
			tile.m_Sprites[0] = (Sprite)EditorGUILayout.ObjectField("None", tile.m_Sprites[0], typeof(Sprite), false, null);
			tile.m_Sprites[2] = (Sprite)EditorGUILayout.ObjectField("One", tile.m_Sprites[2], typeof(Sprite), false, null);
			tile.m_Sprites[1] = (Sprite)EditorGUILayout.ObjectField("Two", tile.m_Sprites[1], typeof(Sprite), false, null);
			tile.m_Sprites[3] = (Sprite)EditorGUILayout.ObjectField("Three", tile.m_Sprites[3], typeof(Sprite), false, null);
			tile.m_Sprites[4] = (Sprite)EditorGUILayout.ObjectField("Four", tile.m_Sprites[4], typeof(Sprite), false, null);
			if (EditorGUI.EndChangeCheck())
			{
				EditorUtility.SetDirty(tile);
			}
		}
	}
#endif
}