using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.Extensions;
using System.Linq;
using System.Collections.Generic;
using Framework.InteractionSystem;
using Project.World;
using Project.Utility;

namespace Project.Sound
{
    public class AudioVisualDirector : CoreBehaviour
    {
        public class SoundAction : CoreAction
        {
            public AudioClip clip;
            public Vector3 position;
            public float range;

            public SoundAction(Vector3 position, float range)
            {
                this.clip = null;
                this.position = position;
                this.range = range;
            }

            public SoundAction(AudioClip clip, Vector3 position, float range) : this(position, range)
            {
                this.clip = clip;
            }
        }

        // Use this for initialization
        protected override void InnerAwake()
        {
            ActionMediatorLocator.Get().Subscribe<SoundAction>(this);
            AddActionHandler<SoundAction>(PlayRingEffect);
        }

        // Update is called once per frame
        protected override void InnerUpdate()
        {

        }

        private void PlayRingEffect(SoundAction action)
        {
            if (action.clip != null)
            {
                //GetComponent<AudioSource>().pla
                AudioSource.PlayClipAtPoint(action.clip, action.position);
            }
            GameObject soundRingObject = (GameObject)Instantiate(Resources.Load("SoundRing"), action.position + (Vector3.up * 0.5f), Quaternion.LookRotation(Vector3.up)); // + (Vector3.forward * LayerConstants.VisualSoundLayerDepth)
            soundRingObject.GetComponent<SoundRingEffect>().scale = (action.range *2f);
            Announce(new WorldLoader.LoadFromPositionAction(action.position, action.range));
        }
    }
}