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 UnityEngine.UI;

namespace Project.View
{
    public class HurtFlashDisplay : CoreBehaviour
    {
        public Image hurtFlashImage;
        public float duration = 1f;
        private UnityTimer timer;

        // Use this for initialization
        protected override void InnerAwake()
        {
            AddGlobalActionHandler<PlayerInput.PlayerHurtAction>(this, Flash);
            this.hurtFlashImage = this.transform.RecursivelyFindChild("HurtFlash").GetComponent<Image>();
            this.timer = new UnityTimer(this.duration, true);
        }

        // Update is called once per frame
        protected override void InnerUpdate()
        {
            if (!this.timer.IsComplete())
            {
                this.timer.Advance();
                this.hurtFlashImage.color = new Color(1f, 1f, 1f, 1 - (this.timer.GetCurrent() / this.timer.GetEnd()));
            }
            else
            {
                this.hurtFlashImage.color = new Color(1f, 1f, 1f, 0f);
            }
        }

        private void Flash(PlayerInput.PlayerHurtAction action)
        {
            this.timer.Reset();
        }
    }
}