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

public class IngameUIManager : SingletonBehaviour<IngameUIManager>
{
	public RectTransform dragUI;

	public GameObject characterStatusUI;
	public Image[] skillUIs = new Image[4];

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

	public void EnableDragUI()
	{
		dragUI.gameObject.SetActive(true);
		dragUI.sizeDelta = Vector2.zero;
	}

	public void DisableDragUI()
	{
		dragUI.gameObject.SetActive(false);
	}

	public void UpdateDragUI(Vector2 startPoint, Vector2 endPoint)
	{
		startPoint = Camera.main.WorldToViewportPoint(startPoint);
		endPoint = Camera.main.WorldToViewportPoint(endPoint);

		dragUI.anchorMin = dragUI.anchorMax =  (startPoint + endPoint) / 2;

		Vector2 size = endPoint - startPoint;
		RectTransform rect = GetComponent<RectTransform>();

		size.x = Mathf.Abs(rect.sizeDelta.x * size.x);
		size.y = Mathf.Abs(rect.sizeDelta.y * size.y);
		dragUI.sizeDelta = size;
	}

	public void OnCharacterButtonClicked(int index)
	{
		PlayerController.inst.SelectCharacter(index);
	}

	public void EnableCharacterStatusUI(PlayableCharacter character)
	{
		characterStatusUI.SetActive(true);
		for(int i = 0; i < 4; i++)
		{
			skillUIs[i].sprite = character.Skills[i].SkillImage;
			UpdateSkillUI(character.Skills[i]);
		}
	}

	public void DisableCharacterStatusUI()
	{
		characterStatusUI.SetActive(false);
	}

	public void UpdateSkillUI(Skill skill)
	{
		Image skillUI = skillUIs[skill.Tier];
		skillUI.GetComponentInChildren<Text>().text = skill.RemainedCooltime.ToString("F1");
		skillUI.fillAmount = skill.CooltimeRatio;
	}
}
