using UnityEngine;
using System;

namespace WS
{
    ///<summary>Voice数据</summary>
    public class VoiceEntity : WSComponent
    {
        /// <summary>类型</summary>
        public VoiceType Type;
        ///<summary>播放状态</summary>
        private VoiceState State;
        /// <summary>语音</summary>
        private AudioSource _source;
        /// <summary>语音</summary>
        public AudioSource Source { get { return _source; } }
        ///<summary>播放时间</summary>
        public float PlayTime;
        ///<summary>组件音量</summary>
        public float SourceVolume { set { Source.volume = value; } }
        ///<summary>是否在播放</summary>
        public bool isPlaying
        {
            get
            {
                if (Source == null)
                    return false;
                return Source.isPlaying;
            }
        }
        ///<summary>计时器</summary>
        private TimerEntity Timer;

        ///<summary>初始化数据</summary>
        public void StartDate(VoiceType type, AudioSource source, float playtime, Action oncallback)
        {
            Type = type;
            _source = source;
            PlayTime = playtime;
            Action action = () =>
            {
                if (!Source.loop)
                    VoiceManager.Instance.Stop(this);
                oncallback?.Invoke();
            };
            if (PlayTime <= 0)
            {
                PlayTime = source.clip.length;
                Timer = TimerComponent.Instance.Create(PlayTime, action, source.loop);
            }
            else
            {
                PlayTime = source.clip.length + PlayTime;
                Timer = TimerComponent.Instance.Create(PlayTime, action, source.loop, OnUpdate);
            }
        }

        ///<summary>初始化数据</summary>
        public void StartDate(VoiceType type, AudioSource source)
        {
            Type = type;
            _source = source;
        }

        public void OnUpdate(float playtime)
        {
            switch (State)
            {
                case VoiceState.Play:
                    if (playtime >= Source.clip.length)
                        Pause(false);
                    break;
                case VoiceState.Pause:
                    if (playtime >= PlayTime && Source.loop)
                        Play();
                    break;
                default:
                    break;
            }
        }

        ///<summary>播放</summary>
        public void Play()
        {
            if (State != VoiceState.Play)
            {
                State = VoiceState.Play;
                Source.Play();
                Timer.IsPause = false;
            }
        }

        ///<summary>暂停</summary>
        ///<param name="timerpause">计时器是否暂停</param>
        public void Pause(bool timerpause = true)
        {
            if (State != VoiceState.Pause)
            {
                State = VoiceState.Pause;
                Source.Pause();
                Timer.IsPause = timerpause;
            }
        }

        ///<summary>停止</summary>
        public void Stop()
        {
            if (State != VoiceState.Stop)
            {
                State = VoiceState.Stop;
                Source.Stop();
                if (Timer != null)
                    Timer.IsPause = true;
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            Type = VoiceType.Voice;
            State = VoiceState.None;
            PlayTime = 0;
            if (Source.loop)
                TimerComponent.Instance.Stop(Timer);
            Timer = null;
            if (Source != null)
                GamePoolManager.Instance.Recovery(Source.gameObject);
            _source = null;
        }
    }
}