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 float telegraphDelay;

    private GameObject instantiatedArea;
    private NetworkedAreaOfEffect networkedAreaOfEffect;

    Camera cam;
    Ray ray;
    RaycastHit hit;

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

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

        if (cam == null)
            cam = Camera.main;

        ray = cam.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 100f, movementMask))
        {
            instantiatedArea = PhotonNetwork.Instantiate("Abilities/" + aoePrefab.name, hit.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.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.Init();
    }
}
