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

namespace UnitTests
{


    /// <summary>
    ///This is a test class for ContentFileServiceTest and is intended
    ///to contain all ContentFileServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class UserServiceTest
    {
        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); Console.WriteLine(_message); Assert.AreEqual("success", _message);
        }

        #endregion


        /// <summary>
        ///A test for User_Purchase
        ///</summary>
        [TestMethod()]
        public void User_PurchaseTest()
        {
            string receiptData = "{  \"signature\" = \"AiEYv/fCbraqE9w910oZRAseUMPz4j5060L/zgxzk/gWbKHLfJbLeTEd+IC2KO3DFvCbx8X9rMzu6CPBZ/1/dnLauuEujYOwfaTTYlMrkDePVgdle67IoDd4PSzGULnXyVuZIRSb5OQGlQ2IHdIl3Jk3i7Snqjh9aLD79ohmy+XmAAADVzCCA1MwggI7oAMCAQICCGUUkU3ZWAS1MA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNVBAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmM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";  \"environment\" = \"Sandbox\";  \"pod\" = \"100\";  \"signing-status\" = \"0\"; }";

            PurchaseContext purchaseContext = new PurchaseContext();
            purchaseContext.ProductCode = "ProAccess_15G_1YR";
            purchaseContext.PaymentType = PurchaseContext.PaymentTypeNames.Apple.ToString();
            purchaseContext.UUID = "EQUnitTest";
            purchaseContext.Price = 69.95f;
            purchaseContext.ReceiptData = Base64.Encode(receiptData);
            purchaseContext.PriceLocale = "en_US@currency=USD";
            purchaseContext.CurrencyCode = "USD";
            purchaseContext.CountryCode = "US";
            purchaseContext.IsSandbox = true;

            _eq.UserService.Purchase(purchaseContext, (PurchaseTransaction purchaseTransaction, EQException ex) => // onComplete
            {
                if (ex == null)
                {
                    if (purchaseTransaction.Products.Count > 0)
                        _message = "success";
                    else
                        _message = "UserService.Purchase products.Count == 0";
                }
                else
                    _message = ex.Message;
            });
        }
    }
}
