using System;
using System.Runtime.CompilerServices;
using UniRx;
using UnityEngine;
using UnityEngine.EventSystems;

public class CornSeedlingView : MonoBehaviour
{
    [SerializeField] private GameObject seedlingStage1;
    [SerializeField] private GameObject seedlingStage2;
    [SerializeField] private GameObject seedlingStage3;
    [SerializeField] private GameObject seedlingStage4;
    [SerializeField] private GameObject growingArrowPrefab;
    [SerializeField] private Sprite seedlingSprite21;
    [SerializeField] private Sprite seedlingSprite22;
    [SerializeField] private Sprite seedlingSprite23;
    [SerializeField] private Sprite seedlingSprite24;
    [SerializeField] private Sprite seedlingSprite31;
    [SerializeField] private Sprite seedlingSprite32;
    [SerializeField] private Sprite seedlingSprite33;
    [SerializeField] private Sprite seedlingSprite34;
    private readonly int nextStageTriggerHash = Animator.StringToHash("NextStageTrigger");
    private readonly int buddingTriggerHash = Animator.StringToHash("BuddingTrigger");

    public void ResetView()
    {
        seedlingStage1.SetActive(false);
        seedlingStage2.SetActive(false);
        seedlingStage3.SetActive(false);
        seedlingStage4.SetActive(false);
    }

    public void SetRank(CornFieldRank rank)
    {
        switch (rank)
        {
            case CornFieldRank.Rank1:
                break;
            case CornFieldRank.Rank2:
                seedlingStage1.GetComponent<SpriteRenderer>().sprite = seedlingSprite21;
                seedlingStage2.GetComponent<SpriteRenderer>().sprite = seedlingSprite22;
                seedlingStage3.GetComponent<SpriteRenderer>().sprite = seedlingSprite23;
                seedlingStage4.GetComponent<SpriteRenderer>().sprite = seedlingSprite24;
                break;
            case CornFieldRank.Rank3:
                seedlingStage1.GetComponent<SpriteRenderer>().sprite = seedlingSprite31;
                seedlingStage2.GetComponent<SpriteRenderer>().sprite = seedlingSprite32;
                seedlingStage3.GetComponent<SpriteRenderer>().sprite = seedlingSprite33;
                seedlingStage4.GetComponent<SpriteRenderer>().sprite = seedlingSprite34;
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(rank), rank, null);
        }
    }

    public void SetView(SeedlingStage stage)
    {
        switch (stage)
        {
            case SeedlingStage.Stage0:
                break;
            case SeedlingStage.Stage1:
                seedlingStage1.SetActive(true);
                break;
            case SeedlingStage.Stage2:
                seedlingStage2.SetActive(true);
                break;
            case SeedlingStage.Stage3:
                seedlingStage3.SetActive(true);
                break;
            case SeedlingStage.Stage4:
                seedlingStage4.SetActive(true);
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    // 一段階成長するときアニメーションを再生して次の段階の素材に切り替え
    // アニメーション再生中0：15で次の段階の素材に切り替え、アニメーションは最後まで再生
    public void ChangeState(SeedlingStage stage)
    {
        switch (stage)
        {
            case SeedlingStage.Stage0:
                break;
            case SeedlingStage.Stage1:
                seedlingStage1.SetActive(true);
                seedlingStage1.GetComponent<Animator>().SetTrigger(buddingTriggerHash);
                break;
            case SeedlingStage.Stage2:
                seedlingStage1.GetComponent<Animator>().SetTrigger(nextStageTriggerHash);
                this.CallWaitForSeconds(.25f, () =>
                {
                    seedlingStage1.SetActive(false);
                    seedlingStage2.SetActive(true);
                });
                break;
            case SeedlingStage.Stage3:
                seedlingStage2.GetComponent<Animator>().SetTrigger(nextStageTriggerHash);
                this.CallWaitForSeconds(.25f, () =>
                {
                    seedlingStage2.SetActive(false);
                    seedlingStage3.SetActive(true);
                });
                break;
            case SeedlingStage.Stage4:
                seedlingStage3.GetComponent<Animator>().SetTrigger(nextStageTriggerHash);
                this.CallWaitForSeconds(.25f, () =>
                {
                    seedlingStage3.SetActive(false);
                    seedlingStage4.SetActive(true);
                });
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(stage), stage, null);
        }
    }

    public void EffectGrowth()
    {
        var arrow = Instantiate(growingArrowPrefab, transform);
        this.CallWaitForSeconds(0.5f, () => Destroy(arrow));
    }
}