using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CashPip : MonoBehaviour
{
    public int pipNum;

    public Sprite emptySprite;
    public Sprite fullSprite;
    public Sprite failSprite;

    private SpriteRenderer rend;
    private float showtime;


    void Start()
    {
        rend = GetComponent<SpriteRenderer>();
        rend.sprite = emptySprite;
        rend.enabled = false;

        showtime = Time.time + 6.5f + pipNum * 0.1f;
    }

    void Update()
    {
        if(!rend.enabled && Time.time > showtime)
        {
            ShowPip();
        }

        switch (Score.GetResult(pipNum))
        {
            case Score.ResultType.None:
                rend.sprite = emptySprite;
                break;
            case Score.ResultType.Success:
                if(rend.sprite == emptySprite)
                {
                    rend.sortingOrder = 10001 + pipNum;
                    transform.DOPunchScale(Vector3.one * 0.6f, 1.75f, 10, 0.5f);
                    transform.DOPunchRotation(new Vector3(0, 0, 20), 1f, 10, 0.8f);
                }
                rend.sprite = fullSprite;
                break;
            case Score.ResultType.Failure:
                if(rend.sprite == emptySprite)
                {
                    rend.sortingOrder = 10001 + pipNum;
                    transform.DOPunchScale(Vector3.one * 0.4f, 0.85f, 5, 0.4f);
                }
                rend.sprite = failSprite;
                break;
        }
    }

    private void ShowPip()
    {
        rend.enabled = true;
        transform.localScale = Vector3.zero;
        transform.DOScale(Vector3.one, 0.2f).SetEase(Ease.OutBack);
    }
}
