using Godot;
using System;
using Godot.Collections;
using lettergodot;

public class MainBoard : TileMap
{
	public Dictionary<Vector2, GameManager.FieldType> Points = new Dictionary<Vector2, GameManager.FieldType>();
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		SetupBonusTiles();
		DrawLettersOnBonusTiles();
	}

	private void DrawLettersOnBonusTiles()
	{
		// For every bonus tile, draw a label on it
		// 'DW" for DoubleWord
		// 'TW' for TripleWord
		// 'DL' for DoubleLetter
		// 'TL' for TripleLetter
		foreach (var point in Points)
		{
			var position = point.Key;
			var fieldType = point.Value;
			var label = new Label();
			// make label text bold
			label.AddFontOverride("font", new DynamicFont { FontData = new DynamicFontData { FontPath = "res://Roboto-Bold.ttf" } });
			label.AddColorOverride("font_color", new Color(0, 0, 0));
			switch (fieldType)
			{
				case GameManager.FieldType.DoubleWord:
					label.Text = "DW";
					break;
				case GameManager.FieldType.TripleWord:
					label.Text = "TW";
					break;
				case GameManager.FieldType.DoubleLetter:
					label.Text = "DL";
					break;
				case GameManager.FieldType.TripleLetter:
					label.Text = "TL";
					break;
			}
			
			label.RectPosition = new Vector2(position.x * 64 + 20, position.y * 64 + 25);
			AddChild(label);
		}
	}

	private void SetupBonusTiles()
	{
		Points.Add(new Vector2(5, 1), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(9, 1), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(7, 3), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(1, 5), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(13, 5), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(3, 7), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(11, 7), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(1, 9), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(13, 9), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(7, 11), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(5, 13), GameManager.FieldType.DoubleWord);
		Points.Add(new Vector2(9, 13), GameManager.FieldType.DoubleWord);

		Points.Add(new Vector2(3, 0), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(11, 0), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(0, 3), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(14, 3), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(0, 11), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(14, 11), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(3, 14), GameManager.FieldType.TripleWord);
		Points.Add(new Vector2(11, 14), GameManager.FieldType.TripleWord);

		Points.Add(new Vector2(2, 1), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(12, 1), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(1, 2), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(4, 2), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(10, 2), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(13, 2), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(2, 4), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(6, 4), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(8, 4), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(12, 4), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(4, 6), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(10, 6), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(4, 8), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(10, 8), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(2, 10), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(6, 10), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(8, 10), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(12, 10), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(1, 12), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(4, 12), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(10, 12), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(13, 12), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(2, 13), GameManager.FieldType.DoubleLetter);
		Points.Add(new Vector2(12, 13), GameManager.FieldType.DoubleLetter);

		Points.Add(new Vector2(6, 0), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(8, 0), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(3, 3), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(11, 3), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(5, 5), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(9, 5), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(0, 6), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(14, 6), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(0, 8), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(14, 8), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(5, 9), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(9, 9), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(3, 11), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(11, 11), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(6, 14), GameManager.FieldType.TripleLetter);
		Points.Add(new Vector2(8, 14), GameManager.FieldType.TripleLetter);
	}

	public override void _Input(InputEvent @event)
	{
		// when user clicks on a tile
		// print tile position 
		if (@event is InputEventMouseButton eventMouseButton && eventMouseButton.Pressed)
		{
			var tilePosition = WorldToMap(GetGlobalMousePosition());
			GD.Print(tilePosition);
		}
	}
}
