using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DisplayCar : MonoBehaviour
{
    public Transform carPrefab;
    private GameObject turnSignalRight;
    private GameObject turnSignalLeft;

    private RectTransform carRect;
    private Text text;
    private Camera camera;
    private Vector2 direction = Vector2.zero;

    private Dictionary<string, Vector2> directions = new Dictionary<string, Vector2>
    {
        {"Right", new Vector2(1,0)},
        {"Left", new Vector2(-1,0)},
        {"Up", new Vector2(0,1)},
        {"Down", new Vector2(0,-1)}
    };
    
    private Dictionary<string, int> rotations = new Dictionary<string, int>
    {
        {"Right", 270},
        {"Left", 90},
        {"Up", 0},
        {"Down", 180}
    };
    
    public void CreateCar(RoadPoint roadPoint)
    {
        var canvas = GameObject.Find("Canvas").transform;
        var carObj = Instantiate(carPrefab, Vector3.zero, Quaternion.identity);
        carObj.name = "Car "+roadPoint.x+"x"+roadPoint.y;
        carObj.SetParent(canvas);
        carRect = carObj.GetComponent<RectTransform>();
        camera = carObj.GetComponentInChildren<Camera>();

        turnSignalRight = carObj.Find("TurnSignalRight").gameObject;
        turnSignalLeft = carObj.Find("TurnSignalLeft").gameObject;
        
        if (Constants.DEBUG_MODE)
        {
            var textGo = new GameObject("DebugText");
            textGo.transform.SetParent(carObj.transform);
            text = textGo.AddComponent<Text>();
            text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
            text.fontSize = 28;
            text.color = new Color32(255,255,255,255);
        }

        SetPosition(roadPoint);
    }

    public void SetInputFeedback(string directionName)
    {
        if (text != null)
            text.text = directionName;

        turnSignalRight.SetActive(false);
        turnSignalLeft.SetActive(false);

        if (directionName == "Right")
            turnSignalRight.SetActive(true);
        else if (directionName == "Left")
            turnSignalLeft.SetActive(true);
    }

    public void SetDirection(string directionName)
    {
        direction = directions[directionName];

        StartCoroutine(AnimateRotation(carRect, carRect.transform.localRotation, Quaternion.Euler(0, 0, rotations[directionName]), Constants.TURN_DURATION));
    }

    public void SetPosition(RoadPoint roadPoint)
    {
        var tileSize = Constants.TILE_SIZE;
        carRect.anchoredPosition = new Vector2(tileSize * (roadPoint.x + 0.5f), tileSize * (roadPoint.y + 0.5f));
    }

	public IEnumerator AnimateRotation(Component obj, Quaternion start, Quaternion end, float lerpTime = 0.5f)
	{
		float startTime = Time.time;
		float timeSinceStart = 0f;
		float percentage = 0f;

		while (true)
		{
			timeSinceStart = Time.time - startTime;
			percentage = timeSinceStart / lerpTime;

            obj.transform.localRotation = Quaternion.Lerp(start, end, percentage);

			if (percentage >= 1) break;

			yield return new WaitForEndOfFrame();
		}
	}
}