﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Arugula.Games
{


    public class CalloutTrigger : MonoBehaviour
    {
        public float duration = 3;
        public LayerMask mask;
        public Callout callout;
        float exitTime;

        private void OnTriggerEnter(Collider other)
        {
            if (Time.time > exitTime && IsValidLayer(other))
            {
                callout.Show();
                exitTime = Time.time + duration;
            }
        }

        private void OnTriggerStay(Collider other)
        {
            if (IsValidLayer(other))
                exitTime = Time.time + duration;
        }

        private void OnTriggerExit(Collider other)
        {

        }

        private void Update()
        {
            if (callout.Visible && Time.time > exitTime)
            {
                callout.Hide();
            }
        }

        bool IsValidLayer(Collider c)
        {
            int layer = c.attachedRigidbody ? c.attachedRigidbody.gameObject.layer : c.gameObject.layer;
            return ((1 << layer) & mask) > 0;
        }
    }
}