using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "ProjectileAbility", menuName = "RiftMayhem/AbilitySystem/Abilities/Projectile Ability", order = 0)]
public class ProjectileAbility : BaseAbility
{
    public GameObject projectilePrefab;

    public float projectileSpeed;
    public float lifeSpan;
    public bool canPierce;
    public bool canHitSelf;


    private GameObject instantiatedProjectile;
    private NetworkedProjectile networkedProjectile;

    public override void Execute(PhotonView user, Taggable userTag)
    {
        base.Execute(user, userTag);

        Debug.Log($"Player {user.name} casted {this.name} and spent {manaCost} mana.");

        instantiatedProjectile = PhotonNetwork.Instantiate("Abilities/" + projectilePrefab.name, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.position, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.rotation);

        networkedProjectile = instantiatedProjectile.GetComponent<NetworkedProjectile>();

        networkedProjectile.speed = projectileSpeed;
        networkedProjectile.owner = user;
        networkedProjectile.ownerTag = userTag;
        networkedProjectile.ability = this;
        networkedProjectile.lifeSpan = lifeSpan;
        networkedProjectile.canPierce = canPierce;
        networkedProjectile.canHitSelf = canHitSelf;

        networkedProjectile.Init();
    }

    public override void Execute(PhotonView user, Taggable userTag, Vector3 point)
    {
        base.Execute(user, userTag, point);

        Debug.Log($"Player {user.name} casted {this.name} and spent {manaCost} mana.");

        instantiatedProjectile = PhotonNetwork.Instantiate("Abilities/" + projectilePrefab.name, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.position, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.rotation);

        networkedProjectile = instantiatedProjectile.GetComponent<NetworkedProjectile>();

        networkedProjectile.speed = projectileSpeed;
        networkedProjectile.owner = user;
        networkedProjectile.ownerTag = userTag;
        networkedProjectile.ability = this;
        networkedProjectile.lifeSpan = lifeSpan;
        networkedProjectile.canPierce = canPierce;
        networkedProjectile.canHitSelf = canHitSelf;

        networkedProjectile.Init();
    }

    public override void Execute(PhotonView user, Taggable userTag, Transform target)
    {
        base.Execute(user, userTag, target);

        Debug.Log($"Player {user.name} casted {this.name} and spent {manaCost} mana.");

        instantiatedProjectile = PhotonNetwork.Instantiate("Abilities/" + projectilePrefab.name, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.position, user.GetComponentInChildren<ProjectileSpawnLocationController>().transform.rotation);

        networkedProjectile = instantiatedProjectile.GetComponent<NetworkedProjectile>();

        networkedProjectile.speed = projectileSpeed;
        networkedProjectile.owner = user;
        networkedProjectile.ownerTag = userTag;
        networkedProjectile.ability = this;
        networkedProjectile.lifeSpan = lifeSpan;
        networkedProjectile.canPierce = canPierce;
        networkedProjectile.canHitSelf = canHitSelf;

        networkedProjectile.Init();
    }
}
