using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Pluto.Api;
using Sdaleo.Systems.Advantage;
using Sdaleo.Systems.SQLServer;
using Connector = PlutoServer.MSL.Connectors;

namespace PlutoServer.MSL.Test
{
    [TestFixture]
    public class MSLConnect
    {
        #region Test Connections

        /// <summary>
        /// Advantage Shared Test Credential
        /// </summary>
        public const string MEDISOFT_REGISTRATION = "Not Registered";
        public const string MEDISOFT_DATASET = "Medical Group (Tutorial Data)";
        public const string MEDISOFT_PRACTICE = "Happy Clinic";
        public AdvantageCredential AdvantageTestSharedCredential = new AdvantageCredential(@"\\10.97.156.4\Medidata\SharedData.add", "SharedDataUser", "AndPassword", AdvantageCredential.ServerType.REMOTE);

        /// <summary>
        /// SQLServer Shared Test Credential
        /// </summary>
        public const string LYTEC_REGISTRATION = "DEMO NOT FOR RESALE";
        public const string LYTEC_DATASET = "Lytec Tutorial";
        public const string LYTEC_PRACTICE = "Centertown Offices";
        public SQLServerCredential SQLServerTestSharedCredential = new SQLServerCredential("10.97.156.22", "LytecMD", "Lytec SharedData", "sa", "Clinical$1");

        #endregion

        [SetUp]
        public void Init()
        {
            MSLSpecific.Setup_Test_Logger();            
        }

        [Test]
        public void ProductSetupCompleted_Medisoft()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION);
            Assert.True(bSuccess);
        }

        [Test]
        public void ProductSetupCompleted_Lytec()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION);
            Assert.True(bSuccess);
        }

        [Test]
        public void MobileAboutFlow_Medisoft()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION);
            Assert.True(bSuccess);
            ApiKeyNPin a = m.MobileAboutDialogCalled(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION, MEDISOFT_PRACTICE, MEDISOFT_DATASET);
            Assert.NotNull(a);
            if (a != null)
            {
                Assert.IsNotNullOrEmpty(a.UserApiKey);
                Assert.IsNotNullOrEmpty(a.UserPin);
            }
        }

        [Test]
        public void MobileAboutFlow_Lytec()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION);
            Assert.True(bSuccess);
            ApiKeyNPin a = m.MobileAboutDialogCalled(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION, LYTEC_PRACTICE, LYTEC_DATASET);
            Assert.NotNull(a);
            if (a != null)
            {
                Assert.IsNotNullOrEmpty(a.UserApiKey);
                Assert.IsNotNullOrEmpty(a.UserPin);
            }
        }

        [Test]
        public void MobileAboutFlow_Medisoft30()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION);
            Assert.True(bSuccess);
            ApiKeyNPin a = m.MobileAboutDialogCalled(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION, MEDISOFT_PRACTICE, MEDISOFT_DATASET);
            Assert.NotNull(a);
            if (a != null)
            {
                Assert.IsNotNullOrEmpty(a.UserApiKey);
                Assert.IsNotNullOrEmpty(a.UserPin);
                if (!String.IsNullOrEmpty(a.UserApiKey) && !String.IsNullOrEmpty(a.UserPin))
                {
                    // Check to make sure 30 times that the userapikey never changes
                    string apiKey = a.UserApiKey;
                    for (int i = 0; i < 30; ++i)
                    {
                        bSuccess = m.ProductSetupCompleted(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION);
                        Assert.True(bSuccess);
                        System.Threading.Thread.Sleep(50);

                        a = m.MobileAboutDialogCalled(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_REGISTRATION, MEDISOFT_PRACTICE, MEDISOFT_DATASET);
                        Assert.IsNotNullOrEmpty(a.UserApiKey);
                        Assert.IsNotNullOrEmpty(a.UserPin);

                        // change check!
                        Assert.True(a.UserApiKey == apiKey);
                        System.Threading.Thread.Sleep(50);
                    }
                }
            }
        }

        [Test]
        public void MobileAboutFlow_Lytec30()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.ProductSetupCompleted(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION);
            Assert.True(bSuccess);
            ApiKeyNPin a = m.MobileAboutDialogCalled(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION, LYTEC_PRACTICE, LYTEC_DATASET);
            Assert.NotNull(a);
            if (a != null)
            {
                Assert.IsNotNullOrEmpty(a.UserApiKey);
                Assert.IsNotNullOrEmpty(a.UserPin);
                if (!String.IsNullOrEmpty(a.UserApiKey) && !String.IsNullOrEmpty(a.UserPin))
                {
                    // Check to make sure 30 times that the userapikey never changes
                    string apiKey = a.UserApiKey;
                    for (int i = 0; i < 30; ++i)
                    {
                        bSuccess = m.ProductSetupCompleted(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION);
                        Assert.True(bSuccess);
                        System.Threading.Thread.Sleep(50);

                        a = m.MobileAboutDialogCalled(SQLServerTestSharedCredential.ConnectionString, LYTEC_REGISTRATION, LYTEC_PRACTICE, LYTEC_DATASET);
                        Assert.IsNotNullOrEmpty(a.UserApiKey);
                        Assert.IsNotNullOrEmpty(a.UserPin);

                        // change check!
                        Assert.True(a.UserApiKey == apiKey);
                        System.Threading.Thread.Sleep(50);
                    }
                }
            }
        }

        [Test]
        public void PracticeNameChangeOccured_Medisoft()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.PracticeNameChangeOccured(AdvantageTestSharedCredential.ConnectionString, MEDISOFT_PRACTICE, MEDISOFT_DATASET);
            Assert.True(bSuccess);
        }

        [Test]
        public void PracticeNameChangeOccured_Lytec()
        {
            MSLSpecific m = new MSLSpecific();
            bool bSuccess = m.PracticeNameChangeOccured(SQLServerTestSharedCredential.ConnectionString, LYTEC_PRACTICE, LYTEC_DATASET);
            Assert.True(bSuccess);
        }

    }
}
