using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.DataAccessLayer;
using Foo.DataAccessLayer.DataTypes;
using DataAccessLayer.TestData;
using System.Threading;
using System.IO;

namespace DataAccessLayer.Tests
{
    /// <summary>
    /// Use this class to do a ThreadStressTest on the DB
    /// Launch this class via CommandLine app and Call RunStressTest()
    /// </summary>
    internal static class StressTest
    {
        const int THREAD_SLEEP_EACH_ITERATION = 5;

        /// <summary>
        /// Thread1 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc1()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread1 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread1 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread2 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc2()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread2 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread2 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread3 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc3()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread3 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread3 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread4 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc4()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread4 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread4 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread5 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc5()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread5 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread5 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread3 - Test ArtifactGroup Reading
        /// </summary>
        static private void ThreadProc6()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread6 - Error : Workspacenames is wrong length");
                    continue;
                }

                foreach (string workspacename in workspacenames)
                {
                    ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                    {
                        Console.WriteLine("Thread6 - Error : ArtifactGroup is Empty");
                    }
                }
                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread7 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc7()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread7 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[0], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread7 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[0], artifactItem))
                            Console.WriteLine("Thread7 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread8 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc8()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread8 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[1], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread8 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[1], artifactItem))
                            Console.WriteLine("Thread8 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread9 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc9()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread9 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[2], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread9 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[2], artifactItem))
                            Console.WriteLine("Thread9 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread10 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc10()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread10 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[0], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread10 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[0], artifactItem))
                            Console.WriteLine("Thread10 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread11 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc11()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread11 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[1], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread11 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[1], artifactItem))
                            Console.WriteLine("Thread11 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread12 - Test Artifact part of a Workspace
        /// </summary>
        static private void ThreadProc12()
        {
            while (true)
            {
                string[] workspacenames = Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending);
                if (workspacenames.Length <= 1)
                {
                    Console.WriteLine("Thread12 - Error : Workspacenames is wrong length");
                    continue;
                }

                ArtifactItem[] artifactItems = Data.Artifacts.GetAllArtifactsForWorkspace(workspacenames[2], SortOrderForArtifacts.Ascending);
                if (DataTypeValidation.IsEmptyArtifactItemGroup(artifactItems))
                {
                    Console.WriteLine("Thread12 - Error : ArtifactGroup is Empty");
                }
                else
                {
                    foreach (ArtifactItem artifactItem in artifactItems)
                    {
                        if (!Data.Artifacts.IsArtifactPartOfWorkspace(workspacenames[2], artifactItem))
                            Console.WriteLine("Thread12 - Error: Artifact / Workspace Validation Error");
                    }
                }

                Thread.Sleep(THREAD_SLEEP_EACH_ITERATION);
            }
        }

        /// <summary>
        /// Thread13 - Create a Write Artifact Thread! (Let's see what happens)
        /// </summary>
        static private void ThreadProc13()
        {
            //while (true)
            //{
            //    bool bSuccess = false;

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document1", "Document1.docx", "ProjectNotes");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document2", "Document2.doc", "ProjectNotes");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document3", "Document3.docx", "Office");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document4", "Document4.doc", "Presentations");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document5", "Document5.docx", "Presentations");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document6", "Document6.docx", "Presentations");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Document7", "Document7.docx", "Office");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Doris", "Doris.xlsx", "ProjectNotes");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Doris2", "Doris2.docx", "ProjectNotes");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Doris liks apples", "Doris likes apples.doc", "Office");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Drawing1", "Drawing1.vsd", "MyMindMaps");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Drawing2", "Drawing2.vsd", "MyMindMaps");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Map1", "Map1.mmap", "MyMindMaps");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));

            //    bSuccess = TestData.TestData.AddTestArtifactToWorkspace("Map2", "Map2.mmap", "MyMindMaps");
            //    if (!bSuccess)
            //        Console.WriteLine("Thread13 - Error: Adding Artifact Failed!");

            //    Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));
            //}
        }

        /// <summary>
        /// Thread14 - Create another Write Artifact Thread! (Let's see what happens)
        /// </summary>
        static private void ThreadProc14()
        {
            while (true)
            {
                foreach (string workspacename in Data.Workspace.GetAllWorkspaceNames(SortOrderForWorkspaces.Ascending))
                {
                    ArtifactItem[] artifacts = Data.Artifacts.GetAllArtifactsForWorkspace(workspacename, SortOrderForArtifacts.Ascending);

                    if (!DataTypeValidation.IsEmptyArtifactItemGroup(artifacts))
                    {
                        foreach (ArtifactItem artifact in artifacts)
                        {
                            artifact.WindowHeight = 678;
                            if (!Data.Artifacts.ModifyExistingArtifactProperties(artifact, workspacename))
                            {
                                Console.WriteLine("Thread14 - Error: ModifyExistingArtifactProperties Failed");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Thread14 - Error: artifactsgroup is empty");
                    }
                }

                Thread.Sleep((20 * THREAD_SLEEP_EACH_ITERATION));
            }
        }

        /// <summary>
        /// Entry Point for running all the thread tests
        /// </summary>
        static public void RunStressTest()
        {
            Console.WriteLine("TestDataAccessLayer");
            Thread thread1 = new Thread(new ThreadStart(StressTest.ThreadProc1));
            Thread thread2 = new Thread(new ThreadStart(StressTest.ThreadProc2));
            Thread thread3 = new Thread(new ThreadStart(StressTest.ThreadProc3));
            Thread thread4 = new Thread(new ThreadStart(StressTest.ThreadProc4));
            Thread thread5 = new Thread(new ThreadStart(StressTest.ThreadProc5));
            Thread thread6 = new Thread(new ThreadStart(StressTest.ThreadProc6));
            Thread thread7 = new Thread(new ThreadStart(StressTest.ThreadProc7));
            Thread thread8 = new Thread(new ThreadStart(StressTest.ThreadProc8));
            Thread thread9 = new Thread(new ThreadStart(StressTest.ThreadProc9));
            Thread thread10 = new Thread(new ThreadStart(StressTest.ThreadProc10));
            Thread thread11 = new Thread(new ThreadStart(StressTest.ThreadProc11));
            Thread thread12 = new Thread(new ThreadStart(StressTest.ThreadProc12));
            Thread thread13 = new Thread(new ThreadStart(StressTest.ThreadProc13));
            Thread thread14 = new Thread(new ThreadStart(StressTest.ThreadProc14));
            Console.WriteLine("Threads Created");

            ////
            // Start All the Threads
            ////                                   
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            thread5.Start();
            thread6.Start();
            thread7.Start();
            thread8.Start();
            thread9.Start();
            thread10.Start();
            thread11.Start();
            thread12.Start();
            thread13.Start();
            thread14.Start();

            Console.WriteLine("Threads Started....");

            Console.WriteLine("To Quit Press the 'Q' Key and then Enter");
            while (true)
            {
                string str = Console.ReadLine();
                if (str == "Q" || str == "q")
                {
                    break;
                }
                System.Threading.Thread.Sleep(100);
            }

            Console.WriteLine("Stopping Threads....");
            thread1.Abort();
            thread2.Abort();
            thread3.Abort();
            thread4.Abort();
            thread5.Abort();
            thread6.Abort();
            thread7.Abort();
            thread8.Abort();
            thread9.Abort();
            thread10.Abort();
            thread11.Abort();
            thread12.Abort();
            thread13.Abort();
            thread14.Abort();
        }
    }
}
