/*
 * author: mark joshwel
 * date: 10/8/2024
 * description: trigger to detect if the player landed in a specific area
 */

using System;
using UnityEngine;

/// <summary>
///     detection behaviour class
/// </summary>
public class AerialFaithLandingTrigger : MonoBehaviour
{
    /// <summary>
    ///     the first trigger that detects if the player has jumped out of the play area
    /// </summary>
    [SerializeField] private AerialFaithLeapTrigger linkedLeapTrigger1;

    /// <summary>
    ///     the second trigger that detects if the player has jumped out of the play area
    /// </summary>
    [SerializeField] private AerialFaithLeapTrigger linkedLeapTrigger2;

    /// <summary>
    ///     the third trigger that detects if the player has jumped out of the play area
    /// </summary>
    [SerializeField] private AerialFaithLeapTrigger linkedLeapTrigger3;

    /// <summary>
    ///     check if linkedLeapTrigger1 is set
    /// </summary>
    /// <exception cref="NullReferenceException">consequential exception</exception>
    private void Awake()
    {
        if (linkedLeapTrigger1 == null)
            throw new NullReferenceException("AerialFaithLandingTrigger: linkedLeapTrigger1 is not set");
    }

    /// <summary>
    ///     detect if player has entered the trigger
    /// </summary>
    /// <param name="other">colliding game object</param>
    private void OnTriggerEnter(Collider other)
    {
        // Debug.Log($"AerialFaithLandingTrigger: was hit by object with tag {other.tag}");

        // proceed if:
        //  - the colliding object is the player
        //  - any of the linked leap triggers are active, the 1st is always not null
        if (!other.CompareTag("Player")) return;
        if (!(linkedLeapTrigger1.isPlayerInAir ||
              (linkedLeapTrigger2 != null && linkedLeapTrigger2.isPlayerInAir) ||
              (linkedLeapTrigger3 != null && linkedLeapTrigger3.isPlayerInAir)))
            return;

        Debug.Log("AerialFaithLandingTrigger: player landed");
        linkedLeapTrigger1.isPlayerInAir = false;
        if (linkedLeapTrigger2 != null) linkedLeapTrigger2.isPlayerInAir = false;
        if (linkedLeapTrigger3 != null) linkedLeapTrigger3.isPlayerInAir = false;
    }
}