using System;
using UnityEngine;

public class CornSeedlingStageView : MonoBehaviour
{
    private static readonly int Grow = Animator.StringToHash("Grow");
    private static readonly int Save = Animator.StringToHash("Save");
    [SerializeField] private Animator seedlingStage1;
    [SerializeField] private Animator seedlingStage2;
    [SerializeField] private Animator seedlingStage3;
    [SerializeField] private Animator seedlingStage4;
    
    public void SetView(SeedlingStage stage)
    {
        seedlingStage1.transform.SetLocalScale(stage == SeedlingStage.Stage1 ? 1 : 0);
        seedlingStage2.transform.SetLocalScale(stage == SeedlingStage.Stage2 ? 1 : 0);
        seedlingStage3.transform.SetLocalScale(stage == SeedlingStage.Stage3 ? 1 : 0);
        seedlingStage4.transform.SetLocalScale(stage == SeedlingStage.Stage4 ? 1 : 0);
    }
    
    // 一段階成長するときアニメーションを再生して次の段階の素材に切り替え
    // アニメーション再生中0：15で次の段階の素材に切り替え、アニメーションは最後まで再生
    public void ChangeState(SeedlingStage stage)
    {
        switch (stage)
        {
            case SeedlingStage.Stage0:
                break;
            case SeedlingStage.Stage1:
                SetView(stage);
                seedlingStage1.SetTrigger(Grow);
                break;
            case SeedlingStage.Stage2:
                seedlingStage1.SetTrigger(Save);
                this.CallWaitForSeconds(.25f, () =>
                {
                    SetView(stage);
                    seedlingStage2.SetTrigger(Grow);
                });
                break;
            case SeedlingStage.Stage3:
                seedlingStage2.SetTrigger(Save);
                this.CallWaitForSeconds(.25f, () =>
                {
                    SetView(stage);
                    seedlingStage3.SetTrigger(Grow);
                });
                break;
            case SeedlingStage.Stage4:
                seedlingStage3.SetTrigger(Save);
                this.CallWaitForSeconds(.25f, () =>
                {
                    SetView(stage);
                    seedlingStage4.SetTrigger(Grow);
                });
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(stage), stage, null);
        }
    }
}
