﻿using UnityEngine;

namespace JLGames.RocketDriver.Actions.Audio
{
    public class AudioSound
    {
        /// <summary>
        /// Basic settings - sound effect name
        /// 基础设置 - 音效名称
        /// Sound effect name (using path or unique identifier)
        /// 音效名称(使用路径 或 唯一标识符)
        /// </summary>
        public string Named = "";

        /// <summary>
        /// Audio source
        /// 音频源
        /// </summary>
        public AudioSource Source;

        /// <summary>
        /// Check is pausing.
        /// 是否暂停中
        /// </summary>
        private bool m_Pausing;

        /*
        //下载设置(协程相关)
        //是否下载中 标识
        public bool isLoading;
        public IEnumerator loadingCoroutine;
        //协程创建者
        public MonoBehaviour coroutineScript;
        */

        /// <summary>
        /// Attaching object
        /// 依附对象
        /// </summary>
        public Transform AttachTransform;

        #region Public Functions 属性型方法

        public bool IsValid => "" != Named && null != Source;
        public bool IsDestoryed => !IsValid;
        public bool IsPlaying => IsValid && !m_Pausing && Source.isPlaying;
        public bool CanPause => IsValid && !Source.ignoreListenerPause;
        public bool IsPaused => IsValid && m_Pausing && !Source.isPlaying;
        public bool IsStop => IsValid && !m_Pausing && !Source.isPlaying;
        public bool IsAttached => IsValid && null != AttachTransform;

        #endregion

        #region Public Functions 设置型方法

        public void SetVolume(float volume)
        {
            if (IsValid)
            {
                Source.volume = volume;
            }
        }

        public void SetMuted(bool isMuted)
        {
            if (IsValid)
            {
                Source.mute = isMuted;
            }
        }

        public void SetLooped(bool looped = true)
        {
            if (IsValid)
            {
                Source.loop = looped;
            }
        }

        public void SetPausable(bool pausable)
        {
            if (IsValid)
            {
                Source.ignoreListenerPause = !pausable;
            }
        }

        public void Set3D(bool is3D)
        {
            if (IsValid)
            {
                Source.spatialBlend = is3D ? 1 : 0;
            }
        }

        public void SetPosition(Vector3 position)
        {
            if (IsValid)
            {
                Source.transform.position = position;
            }
        }

        #endregion

        #region Public Functions 行为方法

        public void Play()
        {
            if (IsPaused || IsStop)
            {
                Source.Play();
                m_Pausing = false;
            }
        }

        public void Stop()
        {
            if (IsPlaying || IsPaused)
            {
                if (Source.isPlaying)
                {
                    Source.Stop();
                }

                m_Pausing = false;
            }
        }

        public void Pause()
        {
            if (IsPlaying && !IsStop && CanPause)
            {
                Source.Pause();
                m_Pausing = true;
            }
        }

        public void UnPause()
        {
            if (!IsPaused)
            {
                return;
            }

            Source.UnPause();
            m_Pausing = false;
        }

        public void Destory()
        {
            if (IsDestoryed)
            {
                return;
            }

            Object.Destroy(Source.gameObject);
            Named = "";
            Source = null;
            AttachTransform = null;
        }

        #endregion
    }
}