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 Framework.ActorComponents;
using Project.Sound;
using Project.Player;

namespace Project.Actors
{
    public class EnemyChaseMover : PhysicsMover
    {
        public float approachSpeed = 3f;
        public float approachForce = 1f;
        public float attackSpeed = 20f; 
        public float attackForce = 20f;
        public float attackFrequency = 3f;
        public float attackDistance = 3f;
        public AudioClip angryClip; 
        private ActorClassifier target;
        private UnityTimer attackTimer;
        private bool freeze = false;
        private Transform visual;
        private Vector3 startScale;
        private new Rigidbody rigidbody;
        

        // Use this for initialization
        protected override void InnerAwake()
        {
            AddActionHandler<BasicEnemySensor.ExposeTargetAction>(SetTarget);
            AddActionHandler<BasicEnemySensor.ClearTargetAction>(ClearTarget);
            AddActionHandler<EnemySongResponder.ExposeFreeze>(SetFreeze);

            this.attackTimer = new UnityTimer(this.attackFrequency, true);
            this.visual = this.transform.RecursivelyFindChild("Visual");
            this.startScale = this.visual.localScale;
            this.rigidbody = GetComponent<Rigidbody>();
        }

        // Update is called once per frame
        protected override void InnerUpdate()
        {
            if (this.freeze)
            {
                Stop(50f);
                return;
            }
            // Only unfrozen stuff
            if (this.target != null)
            {
                float targetDistance = Vector3.Distance(this.target.transform.position, this.transform.position);
                if (targetDistance > this.attackDistance)
                {
                    MoveToward(this.target.transform.position, this.approachSpeed, this.approachForce);
                }
                else
                {
                    if (this.attackTimer.IsComplete())
                    {
                        //Debug.Log("LEAP ATTACK.");
                        this.attackTimer.Reset();
                        Announce(new AudioVisualDirector.SoundAction(this.angryClip, this.transform.position, 2f));
                        LeapToward(this.target.transform.position, this.attackForce);
                    }
                }

                if (this.target.transform.position.x - this.transform.position.x > 0)
                {
                    this.visual.localScale = this.startScale;
                }
                else
                {
                    this.visual.localScale = new Vector3(-this.startScale.x, this.startScale.y, this.startScale.z);
                }

            }
            if (!this.attackTimer.IsComplete())
            {
                this.attackTimer.Advance();
            }
        }

        protected void OnCollisionEnter(Collision collision)
        {
            GameObject other = collision.collider.gameObject;
            other.Interact(new Woundable.HurtAction(this, this.attackForce, this.transform.position));
        }

        private void SetTarget(BasicEnemySensor.ExposeTargetAction action)
        {
            this.target = action.target;
        }

        private void ClearTarget(BasicEnemySensor.ClearTargetAction action)
        {
            this.target = null;
        }

        private void SetFreeze(EnemySongResponder.ExposeFreeze action)
        {
            this.freeze = action.freeze;
        }
    }
}