using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LDAIMovementFlyingHunter : TM_MovementComponent
{
    private GameManager theGame;
    private LDCameraManager mainCamera;

    public LDGameActor myOwningActor;

    public float AccelerationPerSecond;
    public float MaxFlightSpeed;

    public float MinDistanceFromGoalForKeepAway;

    public float RangeToTargetInWhichToSlowDown;
    private float fModifiedMaxSpeed;
    private float fModifiedAcceleration;
    private bool bInSlowedApproachState;

    public virtual void Awake()
    {
        theGame = GameManager.GetGame();
        mainCamera = Camera.main.GetComponent<LDCameraManager>();

    }

    // Update is called once per frame
    public virtual void Update()
    {
        CalculateMoveThisFrame();


    }

    //This will add an impulse for us to get towards the goal
    public override void SetGoalPosition(Vector2 vGoal)
    {
        Vector2 vTowardsGoal = vGoal - (Vector2)myOwningActor.GetTargetingPosition();

        if (vTowardsGoal.sqrMagnitude < MinDistanceFromGoalForKeepAway * MinDistanceFromGoalForKeepAway)
        {
            vTowardsGoal *= -1.0f;
        }

        //should we slow down?
        fModifiedAcceleration = AccelerationPerSecond;
        fModifiedMaxSpeed = MaxFlightSpeed;

        if (RangeToTargetInWhichToSlowDown != 0f)
        {
            float fSlowingRange = RangeToTargetInWhichToSlowDown;

            //If we're already slowed down, speed up later 
            if (bInSlowedApproachState)
            {
                fSlowingRange *= 1.5f;
            }
            if (vTowardsGoal.sqrMagnitude < fSlowingRange * fSlowingRange)
            {
                fModifiedAcceleration = AccelerationPerSecond * 0.1f;
                fModifiedMaxSpeed = MaxFlightSpeed * 0.2f;
                bInSlowedApproachState = true;
            }
            else
            {
                bInSlowedApproachState = false;
            }
        }

        vTowardsGoal.Normalize();

        vTowardsGoal *= fModifiedAcceleration;

        //if the goal is suddenly behind me, hit the brakes and reverse faster
        if (Vector2.Dot(vTowardsGoal, _vImpulse) < 0f)
        {
            //vTowardsGoal *= 1.5f;
        }

        AddImpulse(vTowardsGoal,0f);

        

    }


    public override void CalculateMoveThisFrame()
    {
        //Apply whatever impulses we have, those are towards the target usually
        base.CalculateMoveThisFrame();

        //cap the impulses at a certain speed
        if (_vImpulse.sqrMagnitude > (fModifiedMaxSpeed * fModifiedMaxSpeed))
        {
            Vector2 vShrunk = _vImpulse.normalized;
            vShrunk *= fModifiedMaxSpeed * 0.98f;
            _vImpulse = vShrunk;
        }
    }

    public void OnTriggerEnter2D(Collider2D hitObject)
    {
        OnHitObject(hitObject.gameObject);
    }

    public void OnTriggerStay2D(Collider2D hitObject)
    {
        OnHitObject(hitObject.gameObject);
    }

    protected bool OnHitObject(GameObject hitObject)
    {
        //we're in the same layer, let's sort of move away from each other
        if (hitObject.layer == gameObject.layer)
        {
            //This is using transform, not vGamePos, but that's ok because they are both
            //the same distance relatively
            Vector2 vTowardsOther = hitObject.transform.position - transform.position;

            vTowardsOther.Normalize();

            if (vTowardsOther == Vector2.zero)
            {
                vTowardsOther = Random.insideUnitCircle;
            }

            vTowardsOther *= -fModifiedAcceleration;
            AddImpulse(vTowardsOther, 0f);

            TM_MovementComponent otherMover = hitObject.GetComponent<TM_MovementComponent>();
            if (otherMover != null)
            {
                otherMover.AddImpulse(vTowardsOther * -1.0f, 0f);
            }
        }
        return true;
    }


}
