﻿using UnityEngine;
using Adverty5.Checks;

namespace Adverty5
{
    [RequireComponent(typeof(Camera))]
    public class AdvertyLooper : MonoBehaviour
    {
        /// <summary>
        /// Adding AdvertyViewabilityCheck to verify adplacement's visibility on camera
        /// </summary>
        protected void Awake()
        {
            if (!GetComponent<AdvertyViewabilityCheck>())
            {
                gameObject.AddComponent<AdvertyViewabilityCheck>();
            }
        }

        /// <summary>
        /// Regular Adverty tick on game thread
        /// </summary>
        protected void Update()
        {
            Adverty.Update();
        }

        /// <summary>
        /// Adverty tick on render thread
        /// </summary>
        protected void OnPostRender()
        {
            GL.IssuePluginEvent(AdvertyUnityPlugin.GetRenderUpdate(), AdvertyUnityPlugin.GetGraphicsEventID());
        }
    }
}
