/**********************************************************************************
* Blueprint Reality Inc. CONFIDENTIAL
* 2020 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.
***********************************************************************************/

using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;

namespace BlueprintReality.Interprocess.Textures
{
    public class SharedSurfaceConsumer : IDisposable
    {
        const string SurfaceIdFormat = "{0}[{1}]";

        private int consumerIndex;
        private SharedSurfaceReader[] Surfaces;
        private ulong readStartCount = 0;
        private SharedSurfaceReader NextReadSurface
        {
            get
            {
                return Surfaces[readStartCount % (ulong)Surfaces.Length];
            }
        }
        public SharedSurfaceReader CurrentReadSurface
        {
            get
            {
                if (readStartCount == 0)
                    return null;
                return Surfaces[(readStartCount - 1) % (ulong)Surfaces.Length];
            }
        }

        public int Size { get { return Surfaces.Length; } }

        public SharedSurfaceConsumer(string chainId)
        {
            int chainLength;
            consumerIndex = CreateSharedSurfaceConsumer(chainId, out chainLength);

            Surfaces = new SharedSurfaceReader[chainLength];
            for (int i = 0; i < Surfaces.Length; i++)
            {
                string frameId = string.Format(SurfaceIdFormat, chainId, i);
                Surfaces[i] = new SharedSurfaceReader(frameId);
            }
        }

        public void Dispose()
        {
            for (int i = 0; i < Surfaces.Length; i++)
                Surfaces[i].Dispose();
            Surfaces = null;
            DisposeSharedSurfaceConsumer(consumerIndex);
        }

        public void SignalAllSurfacesReadyForWrite()
        {
            for (int i = 0; i < Surfaces.Length; i++)
                Surfaces[i].SignalReadyForWrite();
        }

        public bool WaitUntilNextFrameReadyForRead(int timeout)
        {
            if (NextReadSurface.WaitUntilReadyForRead(timeout))
            {
                readStartCount++;
                return true;
            }
            return false;
        }

        public void SignalCurrentSurfaceReadyForWrite()
        {
            CurrentReadSurface.SignalReadyForWrite();
        }
        public void InsertFenceForLastReadCompletion()
        {
            if (!CurrentReadSurface.ReadInProgress)
                Debug.LogError("Recieved more read pings than sent out!");

            CurrentReadSurface.InsertFenceForReadCompletion();
        }

        [DllImport(MixCastInteropPlugin.DllName)]
        private static extern int CreateSharedSurfaceConsumer([MarshalAs(UnmanagedType.LPWStr)] string chainId, out int chainLength);
        [DllImport(MixCastInteropPlugin.DllName)]
        private static extern int DisposeSharedSurfaceConsumer(int consumerIndex);
    }
}
