using System;
using UnityEngine;

[SerializableAttribute]
public sealed class SEInfo {
    [SerializeField, Header("ディレイ")]
    private float delay = default;
    public float Delay {
        get{ return delay; }
    }
    [SerializeField, Header("時間")]
    private float time = default;
    public float Time {
        get{ return time; }
    }
    [SerializeField, Header("間隔")]
    private float interval = default;
    public float Interval {
        get{ return interval; }
    }
    [SerializeField, Header("ファイル名")]
    private string filename = default;
    public string Filename {
        get{ return "Sounds/SE/" + filename; }
    }
    [SerializeField, Header("最大同時発音数")]
    private int maxCount = default;
    public int MaxCount {
        get{ return maxCount; }
    }
}

public class PlaySE : MonoBehaviour {

    [SerializeField, Header("SE情報")]
    private SEInfo[] seInfoArray = default;

    private bool isDestroyed = false;

    void OnEnable(){
        foreach(var info in seInfoArray){
            this.CallWaitForSeconds(info.Delay, () => {
                if(isDestroyed) return ;
                if(info.Time <= 0.0f){
                    SoundManager.Instance.PlaySE(info.Filename, info.MaxCount);
                }else{
                    this.CallTimer(info.Time, info.Interval, count => {
                        SoundManager.Instance.PlaySE(info.Filename, info.MaxCount);
                    });
                }
            });
        }
    }

    void OnDestroy(){
        isDestroyed = true;
    }
}
