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

[CreateAssetMenu(fileName = "AoEAbility", menuName = "RiftMayhem/AbilitySystem/Abilities/Area of Effect Ability", order = 0)]
public class AreaOfEffectAbility : BaseAbility
{
    public GameObject aoePrefab;
    public LayerMask movementMask;

    public float lifeSpan;
    public float radius;
    public bool shouldResizeVisuals;
    public float telegraphDelay;
    public bool canHitSelf;
    public bool spawnUnderUser;

    protected GameObject instantiatedArea;
    private NetworkedAreaOfEffect networkedAreaOfEffect;

    protected Camera cam;
    protected Ray ray;
    protected RaycastHit hit;

    protected void Awake()
    {
        cam = Camera.main;
    }

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

        if (spawnUnderUser)
        {
            instantiatedArea = PhotonNetwork.Instantiate("Abilities/" + aoePrefab.name, user.transform.position, Quaternion.identity);

            networkedAreaOfEffect = instantiatedArea.GetComponent<NetworkedAreaOfEffect>();

            networkedAreaOfEffect.owner = user;
            networkedAreaOfEffect.ownerTag = userTag;
            networkedAreaOfEffect.ability = this;
            networkedAreaOfEffect.radius = radius;
            networkedAreaOfEffect.shouldResizeVisuals = shouldResizeVisuals;
            networkedAreaOfEffect.telegraphDelay = telegraphDelay;
            networkedAreaOfEffect.lifeSpan = lifeSpan;
            networkedAreaOfEffect.canHitSelf = canHitSelf;

            networkedAreaOfEffect.Init();
            return;
        }


        instantiatedArea = PhotonNetwork.Instantiate("Abilities/" + aoePrefab.name, user.GetComponentInChildren<AoERayHitLocationSnapshotController>().aoeRayHitLocationSnapshot.position, Quaternion.identity);

        networkedAreaOfEffect = instantiatedArea.GetComponent<NetworkedAreaOfEffect>();

        networkedAreaOfEffect.owner = user;
        networkedAreaOfEffect.ownerTag = userTag;
        networkedAreaOfEffect.ability = this;
        networkedAreaOfEffect.radius = radius;
        networkedAreaOfEffect.telegraphDelay = telegraphDelay;
        networkedAreaOfEffect.lifeSpan = lifeSpan;
        networkedAreaOfEffect.canHitSelf = canHitSelf;

        networkedAreaOfEffect.Init();
    }

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

        instantiatedArea = PhotonNetwork.Instantiate("Abilities/" + aoePrefab.name, point, Quaternion.identity);

        networkedAreaOfEffect = instantiatedArea.GetComponent<NetworkedAreaOfEffect>();

        networkedAreaOfEffect.owner = user;
        networkedAreaOfEffect.ownerTag = userTag;
        networkedAreaOfEffect.ability = this;
        networkedAreaOfEffect.radius = radius;
        networkedAreaOfEffect.telegraphDelay = telegraphDelay;
        networkedAreaOfEffect.lifeSpan = lifeSpan;
        networkedAreaOfEffect.canHitSelf = canHitSelf;

        networkedAreaOfEffect.Init();
    }

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

        instantiatedArea = PhotonNetwork.Instantiate("Abilities/" + aoePrefab.name, target.position, Quaternion.identity);

        networkedAreaOfEffect = instantiatedArea.GetComponent<NetworkedAreaOfEffect>();

        networkedAreaOfEffect.owner = user;
        networkedAreaOfEffect.ownerTag = userTag;
        networkedAreaOfEffect.ability = this;
        networkedAreaOfEffect.radius = radius;
        networkedAreaOfEffect.telegraphDelay = telegraphDelay;
        networkedAreaOfEffect.lifeSpan = lifeSpan;
        networkedAreaOfEffect.canHitSelf = canHitSelf;

        networkedAreaOfEffect.Init();
    }
}
