using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using I2.Loc;
using Com.LuisPedroFonseca.ProCamera2D;

public class ActionsRadial : MonoBehaviour {

    public RectTransform rectTransform;
    public RectTransform canvas;
    public List<ActionButtonRadial> buttons;
    public ActionButtonRadial actionButtonPrefab;
    public RadialLayout radialLayout;
    public GameObject descriptionPanel;
    public Localize descriptionLabel;
    public bool opened;
    public float openDuration = 0.15f;
    public AnimationCurve animationCurve;

    private Vector2 targetWorldPosition;
    Coroutine closeCoroutine;

    void Awake()
    {
        gameObject.SetActive(false);
        descriptionPanel.SetActive(false);
    }

    public void Open(List<ActionAsset> actions, Tile targetTile)
    {
        if (actions.Count == 0)
            return;

        if (closeCoroutine != null)
        {
            StopCoroutine(closeCoroutine);
            closeCoroutine = null;
        }
        
        Clear();
        opened = true;
        rectTransform.localScale = Vector3.zero;

        for (int i = 0; i < actions.Count; i++)
        {
            if (actions [i].availableState != GameController.Instance.floodState)
                continue;

            ActionButtonRadial newButton = Instantiate(actionButtonPrefab, radialLayout.transform);
            newButton.actionsRadial = this;
            newButton.transform.localScale = Vector3.one;
            buttons.Add(newButton);
            newButton.Init(actions [i], targetTile);
        }

        radialLayout.MinAngle = (buttons.Count - 1) * 55f;
        radialLayout.MaxAngle = 0;
        radialLayout.StartAngle = 90f + radialLayout.MinAngle * 0.5f;

        targetWorldPosition = targetTile.transform.position + new Vector3(0, 0.05f,0);
        GameController.Instance.ActivateSelectionOutline(true, targetWorldPosition);
        UpdatePosition();

        actionButtonPrefab.gameObject.SetActive(false);
        gameObject.SetActive(true);

        Go.to(rectTransform, openDuration, new GoTweenConfig().scale(1f, false).setEaseCurve(animationCurve));
    }

    public void Close()
    {
        closeCoroutine = StartCoroutine(CloseCoroutine());
    }

    IEnumerator CloseCoroutine()
    {
        opened = false;
        GameController.Instance.ActivateSelectionOutline(false);
        Go.to(rectTransform, openDuration, new GoTweenConfig().scale(0f, false).setEaseCurve(animationCurve));
        yield return new WaitForSeconds(openDuration);
        gameObject.SetActive(false);
        closeCoroutine = null;
    }

    void Clear()
    {
        while (buttons.Count > 0)
        {
            Destroy(buttons [0].gameObject);
            buttons.RemoveAt(0);
        }
    }

    void LateUpdate()
    {
        UpdatePosition();
    }

    void UpdatePosition()
    {
        rectTransform.anchoredPosition = new Vector2(
            ProCamera2D.Instance.GameCamera.WorldToViewportPoint(targetWorldPosition).x * canvas.sizeDelta.x,
            ProCamera2D.Instance.GameCamera.WorldToViewportPoint(targetWorldPosition).y * canvas.sizeDelta.y
        );
    }
}
