/**********************************************************************************
* Blueprint Reality Inc. CONFIDENTIAL
* 2019 Blueprint Reality Inc.
* All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains, the property of
* Blueprint Reality Inc. and its suppliers, if any.  The intellectual and
* technical concepts contained herein are proprietary to Blueprint Reality Inc.
* and its suppliers and may be covered by Patents, pending patents, and are
* protected by trade secret or copyright law.
*
* Dissemination of this information or reproduction of this material is strictly
* forbidden unless prior written permission is obtained from Blueprint Reality Inc.
***********************************************************************************/

#if UNITY_STANDALONE_WIN
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BlueprintReality.MixCast
{
    /// <summary>
    /// The component responsible for driving the creation of frames by MixCast cameras
    /// </summary>
    public class ExpCameraScheduler : MonoBehaviour
    {
        public static event System.Action OnBeforeRender;

        protected Transform lastParent;

        private float nextRenderTime;
        private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();

        private Coroutine renderUsedCamerasRoutine, renderSpareCamerasRoutine;

        private void OnEnable()
        {
            renderUsedCamerasRoutine = StartCoroutine(RenderUsedCameras());
            renderSpareCamerasRoutine = StartCoroutine(RenderSpareCameras());

            nextRenderTime = Time.realtimeSinceStartup;
        }
        private void OnDisable()
        {
            StopCoroutine(renderUsedCamerasRoutine);
            StopCoroutine(renderSpareCamerasRoutine);
        }

        IEnumerator RenderUsedCameras()
        {
            while (isActiveAndEnabled && MixCast.Settings == null)
                yield return waitForEndOfFrame;

            while (isActiveAndEnabled)
            {
                if (Time.realtimeSinceStartup >= nextRenderTime)
                {
                    if (MixCastSdk.Active)
                    {
                        if (OnBeforeRender != null)
                            OnBeforeRender();

                        for (int i = 0; i < ExpCameraBehaviour.ActiveCameras.Count; i++)
                        {
                            ExpCameraBehaviour cam = ExpCameraBehaviour.ActiveCameras[i];
                            if (!string.IsNullOrEmpty(cam.cameraContext.Identifier) && SdkCameraUtilities.IsCameraRunningInRealtime(cam.cameraContext.Identifier))
                                cam.RenderScene();
                        }
                    }

                    while (Time.realtimeSinceStartup > nextRenderTime)
                        nextRenderTime += 1f / MixCast.Settings.global.targetFramerate;
                }

                yield return waitForEndOfFrame;
            }
        }

        IEnumerator RenderSpareCameras()
        {
            while (isActiveAndEnabled && MixCast.Settings == null)
                yield return waitForEndOfFrame;

            int lastSpareRenderedIndex = 0;
            while (isActiveAndEnabled)
            {
                if (MixCastSdk.Active && ExpCameraBehaviour.ActiveCameras.Count > 0)
                {
                    int startIndex = lastSpareRenderedIndex;
                    lastSpareRenderedIndex++;
                    while ((lastSpareRenderedIndex - startIndex) <= ExpCameraBehaviour.ActiveCameras.Count)
                    {
                        int camIndex = lastSpareRenderedIndex % ExpCameraBehaviour.ActiveCameras.Count;
                        if (!string.IsNullOrEmpty(ExpCameraBehaviour.ActiveCameras[camIndex].cameraContext.Identifier) && !SdkCameraUtilities.IsCameraRunningInRealtime(ExpCameraBehaviour.ActiveCameras[camIndex].cameraContext.Identifier))
                            break;
                        lastSpareRenderedIndex++;
                    }

                    if (lastSpareRenderedIndex - startIndex <= ExpCameraBehaviour.ActiveCameras.Count)
                        ExpCameraBehaviour.ActiveCameras[lastSpareRenderedIndex % ExpCameraBehaviour.ActiveCameras.Count].RenderScene();
                }

                for (int i = 0; i < MixCast.Settings.global.framesPerSpareRender; i++)
                {
                    yield return waitForEndOfFrame;
                }
            }
        }
    }
}
#endif
