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


public class TweenHelper : MonoBehaviour
{
    // Start is called before the first frame update
    public float delay = 1;
    public LeanTweenType position_type, scale_type;
    public Vector3 startPos;
    public float position_time=1;
    public float scale_time=1;
    public Vector3 startScale;

     Vector3 defaultPos, defaultScale;

    [Header("Monitor data")]
    public Vector3 curPosition;
    public Vector3 curScale;

    void Awake(){
        defaultPos = transform.position;
        defaultScale=transform.localScale;


        if(position_type != LeanTweenType.notUsed){ transform.position = startPos;}
        transform.localScale = startScale;

    }
    void Start()
    {
        Intro();
    }

    public void Intro(){

        TweenManager.Add(this);
        StartCoroutine(start());
    }

    public void Outro(){
        if(position_type != LeanTweenType.notUsed){
            LeanTween.move(gameObject, startPos, position_time).setEase(position_type);
        }

        if(scale_type != LeanTweenType.notUsed){
            LeanTween.scale(gameObject, startScale, scale_time).setEase(scale_type);
        }
    }

    IEnumerator start(){
        yield return new WaitForSeconds(delay);
        
        LeanTween.move(gameObject, defaultPos, position_time).setEase(position_type);
        LeanTween.scale(gameObject, defaultScale, scale_time).setEase(scale_type);
    }


    void OnDrawGizmos(){
        if(position_type == LeanTweenType.notUsed){return;}

        curPosition = transform.position;
        curScale = transform.localScale;
        Gizmos.DrawLine(startPos, curPosition);
    }
    bool init = false;
    void OnValidation(){
        if(!init){
            init=true;
        }else{
            return;
        }
        startPos = transform.position;
        startScale = transform.localScale;
    }


    public void OutroAll(){
        TweenManager.OutroAll();
    }

}


public static class TweenManager{
    public static List<TweenHelper> Tweens{get; private set;}

    public static void Add(TweenHelper tween){
        if(Tweens == null){Tweens = new List<TweenHelper>();}
        if(Tweens.Contains(tween)){return;}
        Tweens.Add(tween);
    }

    public static void Outro(TweenHelper tween){
        tween.Outro();
        Tweens.Remove(tween);

    }

    public static void OutroAll(){
        foreach(TweenHelper tween in Tweens){
            if(tween == null){continue;}
            tween.Outro();
        }

        for(int i=Tweens.Count-1; i >= 0; i--){
            Tweens.RemoveAt(i);
        }
    }
}
