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

namespace Project.Effects
{
    public class ScreenShakeEffect : CoreBehaviour
    {
        public class ScreenShakeAction : CoreAction {}

        public float duration;
        private UnityTimer timer;
        public bool on;
        private Vector3 initialPosition;

        protected override void InnerAwake()
        {
            AddGlobalActionHandler<ScreenShakeAction>(this, ShakeScreen);
        }

        // Use this for initialization
        protected override void InnerStart()
        {
            this.timer = new UnityTimer(this.duration, false);
            this.initialPosition = this.transform.parent.position - this.transform.position;
        }

        // Update is called once per frame
        protected override void InnerUpdate()
        {
            if(this.on)
            {
                this.timer.Advance();
                this.transform.position = Vector3.Lerp(this.transform.position, this.initialPosition + (Vector3.right * Mathf.Sin(this.timer.GetCurrent() * 180f)), Time.deltaTime * 100f);
            }
            if(this.timer.IsComplete())
            {
                this.transform.position = this.initialPosition;
                this.on = false;
                this.timer.Reset();
            }
        }

        private void ShakeScreen(ScreenShakeAction action)
        {
            this.on = true;
        }
    }
}