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


[CreateAssetMenu(menuName = "Major Project/Pick Ups/Shoot Block")]
[System.Serializable]
public class Shoot : LogicBlock
{
    [SerializeField]
    [Header("Direction to shoot in")]
    private Direction Direction;

    [SerializeField]
    [Header("Projectile to Shoot")]
    private Projectile Projectile;

    private string prefabName;

    public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
    {
        return startBlock;
    }


    protected override IEnumerator BlockLogic(Character player, float animationTime, bool useBlockDirection = false)
    {
        Projectile newProjectile = Instantiate(Projectile, player.transform.position, player.transform.rotation);
        Vector3 direction = Direction.ToVector(player.transform);

        yield return newProjectile.StartCoroutine(newProjectile.ProjectileLogic(player, animationTime, direction));

        Destroy(newProjectile.gameObject);
    }

    public override void CopyToken(BlockToken token)
    {
        base.CopyToken(token);

        prefabName = ((ShotToken)token).prefabName;
        Projectile = Resources.Load<GameObject>(((ShotToken)token).prefabName)?.GetComponent<Projectile>();

        Direction = ((ShotToken)token).direction;
    }

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

        Debug.Log(base.ToToken(token).GetType().Name);
        ShotToken retVal = (ShotToken)base.ToToken(token);
        retVal.direction = Direction;
        if (Projectile != null)
            retVal.prefabName = Projectile.gameObject.name;
        else
            retVal.prefabName = prefabName;

        return retVal;
    }

}

[System.Serializable]
public class ShotToken : BlockToken
{
    public Direction direction;
    public string prefabName;

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