using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.Extensions;
using System.Linq;
using System.Collections.Generic;
using Framework.InteractionSystem;
using Framework.Utility;
using Project.Player;
using Project.Actors;
using Project.Sound;

namespace Project.Constructs
{
    public class KillFieldBehaviour : SongResponder
    {
        public float holdTime;
        private UnityTimer holdTimer;
        private bool on;
        private new BoxCollider collider;
        private new MeshRenderer renderer;

        // Use this for initialization
        protected override void InnerAwake()
        {
            AddActionHandler<PowerMelody.SongEffectAction>(Activate);
            this.holdTimer = new UnityTimer(this.holdTime, false);
            this.collider = GetComponent<BoxCollider>();
            this.collider.enabled = false;
        }

        // Update is called once per frame
        protected override void InnerUpdate()
        {
            if(this.on) 
            {
                this.holdTimer.Advance();
            }
            if (this.holdTimer.IsComplete())
            {
                this.on = false;
                TrapOff();
                this.holdTimer.Reset();
            }
        }

        private void Activate(PowerMelody.SongEffectAction action)
        {
            if (Vector3.Distance(this.transform.position, action.position) < 2f)
            {
                GameObject soundRingObject = (GameObject)Instantiate(Resources.Load("BurstRing"), this.transform.position + (Vector3.up * 0.5f), Quaternion.LookRotation(Vector3.up));
                soundRingObject.GetComponent<SoundRingEffect>().scale = 15f;
                this.on = true;
                TrapOn();
            }
        }

        protected void OnTriggerEnter(Collider collider)
        {
            GameObject other = collider.gameObject;
            other.Interact(new Woundable.HurtAction(this, 0f, this.transform.position));
        }

        private void TrapOn()
        {
            this.collider.enabled = true;
        }

        private void TrapOff()
        {
            this.collider.enabled = false;
        }
    }
}