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 LordDeathHandler : CoreBehaviour
    {
        public AudioClip deathClip;
        public bool dead = false;
        public float deathHangTime = 2f;

        private UnityTimer deathTimer;
        private new MeshRenderer 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<MeshRenderer>();
            this.collider = GetComponent<Collider>();
        }

        // Update is called once per frame
        protected void LateUpdate()
        {
            if (this.dead)
            {
                this.deathTimer.Advance();
                this.renderer.material.color = new Color(this.renderer.material.color.r, this.renderer.material.color.g, this.renderer.material.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)
            {
                Debug.Log("DEATH!");
                Announce(new AudioVisualDirector.SoundAction(this.deathClip, this.transform.position, 10f));
                GameObject soundRingObject = (GameObject)Instantiate(Resources.Load("BurstRing"), this.transform.position + (Vector3.up * 0.5f), Quaternion.LookRotation(Vector3.up));
                soundRingObject.GetComponent<SoundRingEffect>().scale = 30f;
                soundRingObject.GetComponent<SoundRingEffect>().time = 5f;
                this.dead = true;
            }
        }
    }
}