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

/// <summary>
/// Logic block which deals with moving a character in a direction
/// </summary>
[CreateAssetMenu(menuName = "Major Project/Move Block")]
[System.Serializable]
public class Move : LogicBlock
{

    [SerializeField]
    protected Direction direction = Direction.Forward;

    #region Class Functions

    /// <summary>
    /// Implementation of BlockLogic, moves the player forward
    /// </summary>
    /// <param name="player">Player to move</param>
    protected override void BlockLogic(Character player, float animationTime)
    {
        player.Move(direction, animationTime);
    }

    /// <summary>
    /// Returns the block that the character will endUp on after they use this logic element
    /// </summary>
    /// <param name="startBlock">block character is on</param>
    /// <param name="layerMask">layers to ignore</param>
    /// <returns></returns>
    public override Block GetEndBlock(Block startBlock,Transform tranform,  LayerMask layerMask)
    {
        Vector3 wantedPosition = startBlock.position + direction.ToVector(tranform); // position wanted
        Block hit; //output of block detection
        Block retVal = startBlock; //Block we'll move to

        //If block at Position is walkable set it to moveTo
        if (Block.isBlockAtPosition(wantedPosition, 1, layerMask, out hit) && hit.isWalkable(layerMask))
        {
            retVal = hit;
        }
        //else if block down one is walkable
        else if (Block.isBlockAtPosition(wantedPosition + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
        {
            //and it isn't obstructed
            if (Block.isBlockAtPosition(wantedPosition + Vector3.up, 1, layerMask, out hit))
                retVal = hit;
        }

        return retVal;
    }

    public override void CopyToken(BlockToken token)
    {
        base.CopyToken(token);
        direction = ((DirectionToken)token).direction;
    }

    public override BlockToken ToToken(BlockToken token = null)
    {
        if (token == null)
            token = new DirectionToken(this);

        DirectionToken retVal = (DirectionToken) base.ToToken(token);
        retVal.direction = direction;

        return retVal;
    }

    #endregion Class Functions
}

[System.Serializable]
public class DirectionToken : BlockToken
{
    public Direction direction;

    public DirectionToken(LogicBlock block) : base(block) { }
}
