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

public class AoERayHitLocationSnapshotController : MonoBehaviour
{
    public Transform aoeRayHitLocationSnapshot;
    public LayerMask movementMask;

    protected PhotonView photonView;

    Ray ray;
    protected RaycastHit hit;

    protected Vector3 targetPoint = new Vector3();

    bool isCasting;

    protected virtual void Awake()
    {
        photonView = GetComponentInParent<PhotonView>();
        aoeRayHitLocationSnapshot.parent = null;
    }

    private void OnEnable()
    {
        CastBarHandler.Instance.OnCastingStateChanged.AddListener(UpdateIsCastingState);
    }
    private void OnDisable()
    {
        CastBarHandler.Instance.OnCastingStateChanged.RemoveListener(UpdateIsCastingState);
    }

    protected virtual void Start()
    {
        if (!photonView.IsMine) this.enabled = false;
    }

    private void UpdateIsCastingState(bool isCasting)
    {
        this.isCasting = isCasting;

        if (isCasting)
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 100f, movementMask))
            {
                aoeRayHitLocationSnapshot.position = hit.point;
            }
        }
    }

    private void OnDestroy()
    {
        if (aoeRayHitLocationSnapshot != null)
            Destroy(aoeRayHitLocationSnapshot.gameObject);
    }
}
