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

public class NetworkedChanneling : MonoBehaviour
{
    [Header("Visuals")]
    [SerializeField] protected GameObject visuals;
    [Header("Physics:")]
    [SerializeField] protected LayerMask abilityHitLayer;

    [Header("Set by Code")]
    public PhotonView photonView;
    public PhotonView owner;
    public Taggable ownerTag;
    public ChanneledAbility ability;
    public float duration;
    public bool canHitSelf;
    public bool followUser;
    public float radius;

    public bool channeling = false;

    private float endTime;

    protected PhotonView possibleTarget;
    protected Taggable target;
    protected List<Taggable> targets = new List<Taggable>();

    protected Collider[] hits;

    protected virtual void Awake()
    {
        photonView = GetComponent<PhotonView>();
    }

    public virtual void Init(ref Coroutine channelingCoroutine)
    {
        if (photonView.IsMine)
        {
            photonView.RPC(nameof(RPC_RemoteInit), RpcTarget.Others, AbilityIndexer.Instance.Abilities.IndexOf(ability));

            channeling = true;

            channelingCoroutine = StartCoroutine(ExecuteChanneling());

            if (followUser)
                StartCoroutine(FollowUser());

            StartCoroutine(SelfDestruct());
        }
    }

    IEnumerator FollowUser()
    {
        while (Time.time < endTime)
        {
            this.transform.position = owner.transform.position;
            yield return new WaitForEndOfFrame();
        }

        visuals.SetActive(false);
    }

    [PunRPC]
    protected virtual void RPC_RemoteInit(int abilityIndex)
    {
        ability = (ChanneledAbility)AbilityIndexer.Instance.Abilities[abilityIndex];
    }

    [PunRPC]
    private void RPC_DisableVisuals()
    {
        visuals.SetActive(false);
    }

    IEnumerator ExecuteChanneling()
    {
        visuals.SetActive(true);

        endTime = Time.time + duration;
        while (Time.time < endTime)
        {
            yield return new WaitForSeconds(0.5f);
            CheckSurroundings();
        }
    }

    public void DisableVisuals()
    {
        if (photonView.IsMine)
            photonView.RPC(nameof(RPC_DisableVisuals), RpcTarget.All);
    }

    protected virtual void CheckSurroundings()
    {
        if (!channeling) return;

        hits = Physics.OverlapSphere(this.transform.position, radius, abilityHitLayer);
        targets.Clear();
        foreach (Collider collider in hits)
        {
            Debug.Log("hit collider " + collider.name);

            possibleTarget = collider.GetComponentInParent<PhotonView>();
            if (possibleTarget != null)
                if (possibleTarget == owner && !canHitSelf) continue;

            target = collider.GetComponentInParent<Taggable>();

            Debug.Log("hit collider, Got taggable: " + target.name);

            if (target == null) continue;

            Debug.Log("hit collider, targetTag: " + target.targetTag.name);

            foreach (TargetTag tag in ability.targettingTags)
            {
                Debug.Log("hit collider, ability.Tags: " + tag);
            }
            if (!target.IsValidTarget(ability.targettingTags)) continue;

            Debug.Log("hit collider, added target: " + target.name);
            targets.Add(target);
        }

        foreach (BaseEffect effect in ability.abilityEffects)
        {
            effect.ApplyEffect(ownerTag, targets);
        }
    }

    protected IEnumerator SelfDestruct()
    {
        yield return new WaitForSeconds(duration + 1f);

        PhotonNetwork.Destroy(photonView);
    }

    protected void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, radius);
    }
}
