using EQNetwork.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using EQNetwork;
using EQNetwork.Objects;
using EQNetwork.Controllers;
using EQNetwork.Utility;
using System.Collections.Generic;

namespace UnitTests
{


    /// <summary>
    ///This is a test class for Upload_ControllerTest and is intended
    ///to contain all Upload_ControllerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class Upload_ControllerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        static EQProcess _eq = null;
        static string _message = null;

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            _eq = new EQProcess("accesskey", "secret");

            _eq.SessionService.Create("BrianARice", "f00tball",
                (Session session, EQException ex) => // onComplete
                {
                    if (ex != null)
                        Assert.Fail(ex.Message);
                }
            );

            while (_eq.Session == null) System.Threading.Thread.Sleep(100);
        }

        [ClassCleanup()]
        public static void MyClassCleanup()
        {
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            _message = null;
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            while (_message == null)
                System.Threading.Thread.Sleep(100);
            Console.WriteLine(_message); Assert.AreEqual("success", _message);
        }

        #endregion

        /// <summary>
        ///A test for Upload_Controller
        ///</summary>
        [TestMethod()]
        public void Upload_Controller_Test()
        {
            ContentFile contentFile = new ContentFile();
            contentFile.Name = "EQUnitTest SmallOne";
            contentFile.CategoryList = new List<string>() { "Sports", "Music" };
            contentFile.CollectionId = 262;

            DateTime start = DateTime.Now;

            Upload_Controller upload_Controller = new Upload_Controller(_eq);
//            upload_Controller.Start(contentFile.Name, @"c:\projects\MedOne.mov",
            upload_Controller.Start(contentFile.Name, @"c:\projects\SmallOne.mp4",
                (UploadContext uploadContext, UploadContext.UploadState uploadState) => // onStateChange
                {
                    if (uploadState == EQNetwork.Objects.UploadContext.UploadState.CREATE_CONTENTFILE)
                    {
                        uploadContext.ContentFile = contentFile;
                    }
                    if (uploadState == UploadContext.UploadState.FINISHED)
                    {
                        DateTime end = DateTime.Now;
                        TimeSpan duration = end - start;
                        Console.WriteLine(duration.ToString());
                        upload_Controller.Cancel();
                        _message = "success";
                    }
                },
                (EQException ex) => // onError
                {
                    _message = ex.Message;
                }
            );
        }
    }
}
