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.Sound;

namespace Project.Actors
{
    public class EnemyDeathHandler : CoreBehaviour
    {
        public AudioClip deathClip;
        public bool dead = false;
        public float deathHangTime = 2f;

        private UnityTimer deathTimer;
        private new SpriteRenderer renderer;
        private new Collider collider;

        // Use this for initialization
        protected override void InnerAwake()
        {
            AddActionHandler<Woundable.DeadAction>(Die);
            this.deathTimer = new UnityTimer(this.deathHangTime, false);
            this.renderer = this.transform.RecursivelyFindChild("Visual").GetComponent<SpriteRenderer>();
            this.collider = GetComponent<Collider>();
        }

        // Update is called once per frame
        protected void LateUpdate()
        {
            if (this.dead)
            {
                this.deathTimer.Advance();
                this.renderer.color = new Color(this.renderer.color.r, this.renderer.color.g, this.renderer.color.b, 1 - (this.deathTimer.GetCurrent() / this.deathTimer.GetEnd()));
                this.collider.enabled = false;
            }
            if (this.deathTimer.IsComplete())
            {
                Destroy(this.gameObject);
            }
        }

        private void Die(Woundable.DeadAction action)
        {
            if (!this.dead)
            {
                Announce(new AudioVisualDirector.SoundAction(this.deathClip, this.transform.position, 1f));
                this.dead = true;
            }
        }
    }
}