using UnityEngine;
using System.Collections;
using Framework.Entities;
using Framework.ActorComponents;
using Framework.StatSystem;
using Framework.Debugging;

public class FloatingMover : PhysicsMover 
{
    public Stat moveSpeed = new Stat(1f);
    public Stat turnSpeed = new Stat(1f);
    public float moveForce = 1f;
    public float stopForce = 1f;
    public float floatHeight = 1f;
    public float floatSpeed = 1f;

    protected override void InnerAwake()
    {
        base.InnerAwake();
        AddActionHandler<MoveAction>(Move);
        AddActionHandler<StopAction>(Stop);
        AddActionHandler<TurnAction>(Turn);
    }

    protected override void InnerUpdate()
    {
        base.InnerUpdate();
        RaycastHit hit;
        if(Physics.Raycast(this.transform.position, Vector3.down, out hit, 100, 1 << LayerMask.NameToLayer("Scene")))
        {
            float floatHeightDistance = Mathf.Abs((hit.point.y + this.floatHeight) - this.transform.position.y);
            //if (floatHeightDistance > 0.1)
            //{
                MoveToward(hit.point + (Vector3.up * this.floatHeight), this.floatSpeed + floatHeightDistance, moveForce * 10f, new Vector3(0, 1, 0));
                Draw.Axis(hit.point + (Vector3.up * this.floatHeight), Color.yellow);
            //}
        }
    }

    private void Move(MoveAction action)
    {
        RaycastHit hit;
        if (Physics.Raycast(action.target + (Vector3.up * 50), Vector3.down, out hit, 100, 1 << LayerMask.NameToLayer("Scene")))
        {
            MoveToward(hit.point + (Vector3.up * this.floatHeight), moveSpeed * 1f, moveForce * 10f);
        }
    }

    private void Stop(StopAction action)
    {
        Stop(stopForce * 10f);
    }

    private void Turn(TurnAction action)
    {
        TurnToward(action.target, this.turnSpeed * 1f, new Vector3(1, 0, 1));
    }
}
