//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using Revisor.VMS;
using Revisor.VMS.ClientSDK;
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
namespace LocalRecordExample
{
    //--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// This example demonstrates:
    ///   * how to connect to server remotely
    ///   * how to load camera list
    ///   * how to receive realtime video stream from camera
    ///   * how to record video to local file
    /// </summary>
    //--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    class Program
    {
        static void Main(string[] args)
        {
            using (var connection = new ConnectionWrapper())
            {
                connection.Connect("localhost", 11012, "admin", "");
                Console.WriteLine("Connected.");

                var configuration = connection.GetConfiguration();
                Console.WriteLine("Got server configuration. Camera count: " + configuration.Cameras.Count);

                var camera = configuration.Cameras.Find(c => !c.Disabled);

                if (camera == null)
                {
                    Console.WriteLine("Cannot find any enabled cameras. Disconnecting..");
                }
                else
                {
                    Console.WriteLine("Found camera: " + camera.Name);
                    ReceiveRealtimeVideo(connection, camera);
                }
            }

            Console.WriteLine("Press Enter to exit");
            Console.ReadLine();
        }

        private static void ReceiveRealtimeVideo(ConnectionWrapper connection, Camera camera)
        {
            var exportFileName = Path.Combine(
                Directory.GetCurrentDirectory(),
                string.Format("{0}.rsvid", 
                    DateTime.Now.ToString("yyyy_MM_dd-HH-mm-ss", CultureInfo.InvariantCulture)));

            Console.WriteLine("Trying to receive frames. Press any key to terminate..");
            Console.WriteLine("Writing to file: " + exportFileName);

            using (var lifeView = connection.CreateLifeView(camera, true, 30, false))
            using (var writer = new RSVidWriterWrapper(exportFileName))
            {
                int frameCount = 0;

                while (!Console.KeyAvailable)
                {
                    var frame = lifeView.GetFrame();

                    if (frame == null)
                    {
                        Thread.Sleep(20);
                        continue;
                    }

                    Console.WriteLine("Received frame. Format: " + frame.Format.ToString() + ", size=" + frame.GetLength());
                    frameCount++;

                    writer.Write(frame);
                }

                Console.WriteLine("Total frames received: " + frameCount);
            }
        }
    }
    //--------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
