/// <param name="Type">Can be "Team", "Top Individuals" or "Top Lower Year Group"</param>
        /// <returns>Array of file paths</returns>
        public string[] GenerateTempCertificates(AEvent Event, String Type, bool Print = false, bool Open = false, string ExportPath = "")
        {
            if (Event == null)
            {
                return new string[] { }
            }
            ;

            List <string> files;

            if (string.IsNullOrWhiteSpace(ExportPath))
            {
                files =
                    PrintCertificates.SaveAllEventCertificatesByType(
                        ACertificate.GenerateCertificates(Event),
                        ((App)Application.Current).CurrentChampionship.getChampionshipExportsDir( ),
                        new AEvent[] { Event });
            }
            else
            {
                files =
                    PrintCertificates.SaveAllEventCertificatesByType(
                        ACertificate.GenerateCertificates(Event),
                        ExportPath,
                        new AEvent[] { Event });
            }

            // Why am i trying to save here anyway
            // 2016-05-29 !**!
            //saveChangesToDatabase();

            if (Print)
            {
                files.Where(s => s.Contains(Type)).ToList( ).ForEach(file => Printing.PrintPDF(file));
            }
            else if (Open)
            {
                files.Where(s => s.Contains(Type)).ToList( ).ForEach(file => Process.Start(file));
            }

            if (string.IsNullOrWhiteSpace(Type))
            {
                return(files.ToArray( ));
            }
            else
            {
                return(files.Where(s => s.Contains(Type)).ToArray( ));
            }
        }
        private string[] printResultsSheet(object Instructions)
        {
            Instuction instructions = (Instuction)Instructions;

            List <AEvent> events = instructions.events;
            bool          print  = instructions.print;
            bool          open   = instructions.open;

            Wait wait = new Wait();

            wait.Bar1.setLabel("Competitors");
            wait.Bar2.setLabel("Events");
            wait.Bar1.setEnabled(true);
            wait.Bar2.setEnabled(true);
            wait.Bar2.setMax(events.Count);

            Thread t = new Thread(new ParameterizedThreadStart(wait.Start));

            t.Start(wait);

            List <string> fileNames = new List <string>();

            lock (ExcelLock)
            {
                Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook     wb;
                Microsoft.Office.Interop.Excel.Worksheet    sheet;

                try
                {
                    wb    = excel.Workbooks.Open(@Properties.Settings.Default.OLD_RootPathTemplate + "EventResults.xlsx", false, false);
                    sheet = wb.ActiveSheet;
                }
                catch
                {
                    wait.Complete();
                    t.Abort();
                    return(new string[] { });
                }

                //AResult results;// = new Results();

                foreach (AEvent e in events)
                {
                    List <AResult> lresults = e.Results.Where(r => r.isComplete()).ToList();

                    // Condition removed 2016-01-15 so that empty results sheets will be made.
                    //if (lresults.Count > 0)
                    if (true)
                    {
                        wait.Bar1.setValue(0);
                        wait.Bar1.setMax(lresults.Count);


                        // WSAA 2014-15
                        var leftData    = new object[46, 5];
                        var rightData   = new object[26, 5];
                        var ATeamData   = new object[5, 5];
                        var BTeamData   = new object[5, 5];
                        var OverallData = new object[5, 5];
                        // Clear cells from the last event
                        sheet.Range["Clear1"].Value = ""; // left
                        sheet.Range["Clear2"].Value = ""; // right
                        sheet.Range["Clear3"].Value = ""; // ATeam
                        sheet.Range["Clear4"].Value = ""; // BTeam
                        sheet.Range["Clear5"].Value = ""; // Overall


                        //// SW 2014-15
                        //var leftData = new object[46, 5];
                        //var rightData = new object[32, 5];
                        //var ATeamData = new object[7, 5];
                        ////var OverallData = new object[5, 5];
                        //// Clear cells from the last event
                        //sheet.Range["Clear1"].Value = ""; // left
                        //sheet.Range["Clear2"].Value = ""; // right
                        //sheet.Range["Clear3"].Value = ""; // ATeam

                        int highestRank = 0;

                        foreach (AResult r in lresults)
                        {
                            if (r.Rank.Value < 47)
                            {
                                // use leftData
                                int row = r.Rank.Value;

                                leftData[row - 1, 0] = r.printRank;
                                leftData[row - 1, 1] = r.printVestNo();
                                leftData[row - 1, 2] = string.Format("{0}{1} - {2} - {3}", (r.CertificateEarned ? "*" : ""), r.printName(), r.printResultValueString(), r.printParameter("Attends"));
                                leftData[row - 1, 3] = r.printParameter("YearGroup"); //((StudentCompetitor) r.Competitor).YearGroup;
                                leftData[row - 1, 4] = r.printTeamShort();
                            }
                            else
                            {
                                int row = r.Rank.Value - 46;

                                // use rightData
                                rightData[row - 1, 0] = r.printRank;
                                rightData[row - 1, 1] = r.printVestNo();
                                rightData[row - 1, 2] = string.Format("{0}{1} - {2} - {3}", (r.CertificateEarned ? "*" : ""), r.printName(), r.printResultValueString(), r.printParameter("Attends"));
                                rightData[row - 1, 3] = r.printParameter("YearGroup"); //((StudentCompetitor)r.Competitor).YearGroup;
                                rightData[row - 1, 4] = r.printTeamShort();
                            }

                            if (r.Rank > highestRank)
                            {
                                highestRank = r.Rank.Value;
                            }

                            wait.Bar1.increment();
                        }

                        // move away from the last entered rank
                        highestRank++;

                        List <AResult> DNFresults = e.Results.Where(r => r.getTypeDescription() == ResultTypeDescription.CompetativeDNF).ToList();

                        foreach (AResult r in DNFresults)
                        {
                            if (highestRank < 47)
                            {
                                // use leftData
                                int row = highestRank;

                                leftData[row - 1, 0] = r.printRank;
                                leftData[row - 1, 1] = r.printVestNo();
                                leftData[row - 1, 2] = string.Format("{0}{1} - {2} - {3}", (r.CertificateEarned ? "*" : ""), r.printName(), r.printResultValueString(), r.printParameter("Attends"));
                                leftData[row - 1, 3] = r.printParameter("YearGroup"); //((StudentCompetitor) r.Competitor).YearGroup;
                                leftData[row - 1, 4] = r.printTeamShort();
                            }
                            else
                            {
                                int row = highestRank - 46;

                                // use rightData
                                rightData[row - 1, 0] = r.printRank;
                                rightData[row - 1, 1] = r.printVestNo();
                                rightData[row - 1, 2] = string.Format("{0}{1} - {2} - {3}", (r.CertificateEarned ? "*" : ""), r.printName(), r.printResultValueString(), r.printParameter("Attends"));
                                rightData[row - 1, 3] = r.printParameter("YearGroup"); //((StudentCompetitor)r.Competitor).YearGroup;
                                rightData[row - 1, 4] = r.printTeamShort();
                            }

                            highestRank++;
                        }

                        int i = 0;

                        foreach (ScoringTeam ar in e.getScoringTeams().Where(f => f.ScoringTeamName == "A").OrderBy(f => f.orderableRank()))// results.getAreaResults(e, "A"))
                        {
                            if (ar.Points == 0)
                            {
                                continue;
                            }

                            ATeamData[i, 0] = "    " + ar.Team.ShortName;
                            ATeamData[i, 2] = ar.printPositions();
                            ATeamData[i, 3] = ar.printSumOfPositions();
                            ATeamData[i, 4] = ar.printPoints();

                            i++;
                        }

                        i = 0;

                        // Not used for SW 2014-15
                        foreach (ScoringTeam ar in e.getScoringTeams().Where(f => f.ScoringTeamName == "B").OrderBy(f => f.orderableRank()))
                        {
                            if (ar.Points == 0)
                            {
                                continue;
                            }

                            BTeamData[i, 0] = "    " + ar.Team.ShortName;
                            BTeamData[i, 2] = ar.printPositions();
                            BTeamData[i, 3] = ar.printSumOfPositions();
                            BTeamData[i, 4] = ar.printPoints();

                            i++;
                        }

                        i = 0;

                        foreach (ChampionshipTeamResult or in e.Championship.getOverallSores().Where(f => f.ScoringTeamName == "A").OrderBy(f => f.orderableRank()))// results.getOverAllResults("A"))
                        {
                            OverallData[i, 0] = "    " + or.Team.Name;

                            ChampionshipTeamResult bResult = e.Championship.getOverallSores().Where(f => f.ScoringTeamName == "B" && f.Team == or.Team).FirstOrDefault();

                            if (bResult != null)
                            {
                                OverallData[i, 3] = string.Format("{0} / {1}", or.Points, bResult.Points);
                            }
                            else
                            {
                                OverallData[i, 3] = string.Format("{0} / {1}", or.Points, 0);
                            }

                            i++;
                        }

                        sheet.Range["TeamName"].Value = e.Name;

                        var writeRange = sheet.Range["Clear1"];
                        writeRange.Value2 = leftData;

                        writeRange        = sheet.Range["Clear2"];
                        writeRange.Value2 = rightData;

                        writeRange        = sheet.Range["Clear3"];
                        writeRange.Value2 = ATeamData;

                        // not used for SW 2014-15
                        writeRange        = sheet.Range["Clear4"];
                        writeRange.Value2 = BTeamData;

                        writeRange        = sheet.Range["Clear5"];
                        writeRange.Value2 = OverallData;

                        try
                        {
                            if (@Properties.Settings.Default.SaveExcel)
                            {
                                wb.SaveAs(@Properties.Settings.Default.ResultsPath + e.Name + " Full.xlsx");
                                fileNames.Add(@Properties.Settings.Default.ResultsPath + e.Name + " Full.xlsx");
                            }
                            wb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, @Properties.Settings.Default.ResultsPDFPath + e.Name + " Full.pdf");
                            fileNames.Add(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Full.pdf");
                            //new AdobePrn(@Properties.Settings.Default.ResultsPDFPath + e.ShortName + "\\Full.pdf", @Properties.Settings.Default.prnOpA3Results, _main);

                            if (open)
                            {
                                Process.Start(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Full.pdf");
                            }

                            if (print)
                            {
                                Printing.PrintPDF(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Full.pdf");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Could not save the file " + Properties.Settings.Default.ResultsPDFPath + e.Name + " Full.pdf");
                        }
                        wait.Bar2.increment();
                    }
                }

                wb.Close(false);
                excel.Quit();
            } // end Excel Lock
            wait.Complete();
            t.Abort();
            return(fileNames.ToArray());
        }
        private string[] printTeamResultsSheet(object Instructions)
        {
            Instuction instructions = (Instuction)Instructions;

            List <AEvent> events = instructions.events;
            bool          print  = instructions.print;
            bool          open   = instructions.open;

            Wait wait = new Wait();

            wait.Bar1.setLabel("Competitors");
            wait.Bar2.setLabel("Areas");
            wait.Bar3.setLabel("Events");

            wait.Bar1.setEnabled(true);
            wait.Bar2.setEnabled(true);

            wait.Bar3.setMax(events.Count);
            wait.Bar3.setEnabled(true);

            Thread t = new Thread(new ParameterizedThreadStart(wait.Start));

            t.Start(wait);

            List <string> fileNames = new List <string>();

            lock (ExcelLock)
            {
                Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook     wb;
                Microsoft.Office.Interop.Excel.Worksheet    sheet;

                try
                {
                    wb    = excel.Workbooks.Open(@Properties.Settings.Default.OLD_RootPathTemplate + "TeamResults.xlsx", false, false);
                    sheet = wb.ActiveSheet;
                }
                catch
                {
                    wait.Complete();
                    t.Abort();
                    return(new string[] { });
                }


                //AResult results;//= new AResult();
                foreach (AEvent e in events)
                {
                    // Clear cells from the last event

                    sheet.Range["Clear1"].Value = "";
                    sheet.Range["Clear2"].Value = "";

                    bool        competitors = false;
                    List <Team> areas       = MainWindow.CurrentChampionship.listAllTeams();

                    wait.Bar2.setValue(0);
                    wait.Bar2.setMax(areas.Count);

                    //// WSAA 2014-15
                    var topData    = new object[14, 5];
                    var bottomData = new object[1, 5];

                    // SW 2015
                    //var topData = new object[14, 7];
                    //var bottomData = new object[1, 7];

                    foreach (Team a in areas)
                    {
                        List <AResult> lresults = e.Results.Where(f => f.printTeam() == a.Name && f.isComplete()).ToList();
                        // Condition removed 2016-01-15 so that empty results sheets will be made.
                        //if (lresults.Count > 0)
                        if (true)
                        {
                            competitors = true;
                            wait.Bar1.setValue(0);
                            wait.Bar1.setMax(lresults.Count);


                            int i = 0;  //changed from 1 when changing to range based updates
                            int column;
                            switch (a.Name)
                            {
                            case "Avon":
                                column = 1;
                                break;

                            case "Cornwall":
                                column = 2;
                                break;

                            case "Devon":
                                column = 3;
                                break;

                            case "Dorset":
                                column = 4;
                                break;

                            case "Gloucestershire":
                                column = 5;
                                break;

                            case "Somerset":
                                column = 6;
                                break;

                            case "Wiltshire":
                                column = 7;
                                break;


                            case "Swindon":
                                column = 4;
                                break;

                            case "North Wiltshire":
                                column = 2;
                                break;

                            case "West Wiltshire":
                                column = 5;
                                break;

                            case "Kennet":
                                column = 1;
                                break;

                            case "Salisbury":
                                column = 3;
                                break;

                            default:
                                continue;
                            }


                            //int column = a.getLogicalNumber();

                            foreach (AResult r in lresults.OrderBy(f => f.Rank.Value))
                            {
                                topData[i, column - 1] = string.Format("{0} / {1} / Y{4} {2} {3}", r.Rank.Value.ToString(), r.printVestNo(), "\n", r.Competitor.getName(), r.printParameter("YearGroup")); //((StudentCompetitor)r.Competitor).YearGroup);

                                wait.Bar1.increment();
                                i++;
                            }

                            foreach (ScoringTeam ar in e.getScoringTeams().Where(f => f.ScoringTeamName == "A" && f.Team == a))// results.getAreaResults(e, "A", a))
                            {
                                // row chanced for SW 2014-15
                                //topData[10, column - 1] = ar.printSumOfPositions();

                                topData[13, column - 1] = ar.printSumOfPositions();
                                // not required for SW 2014-15
                                bottomData[0, column - 1] = ar.printPoints();
                            }

                            //i = 1;

                            //foreach (AreaResult ar in results.getAreaResults("B"))
                            //{
                            //    string range = "BTeamHeader";
                            //    sheet.Range[range].Offset[i, 0].Value = ar.area.getCode();
                            //    sheet.Range[range].Offset[i, 1].Value = ar.positions;
                            //    sheet.Range[range].Offset[i, 2].Value = ar.total;
                            //    sheet.Range[range].Offset[i, 3].Value = ar.points;

                            //    i++;
                            //}

                            var writeRange = sheet.Range["Clear1"];
                            writeRange.Value2 = topData;

                            // not required for SW 2014-15
                            writeRange        = sheet.Range["Clear2"];
                            writeRange.Value2 = bottomData;

                            sheet.Range["AreaName"].Value = e.Name;
                        }
                        wait.Bar2.increment();
                    }
                    if (competitors)
                    {
                        try
                        {
                            if (@Properties.Settings.Default.SaveExcel)
                            {
                                wb.SaveAs(@Properties.Settings.Default.ResultsPath + e.Name + " Team.xlsx");
                                fileNames.Add(@Properties.Settings.Default.ResultsPath + e.Name + " Team.xlsx");
                            }

                            wb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, @Properties.Settings.Default.ResultsPDFPath + e.Name + " Team.pdf");
                            fileNames.Add(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Team.pdf");
                            //new AdobePrn(@Properties.Settings.Default.ResultsPDFPath + e.ShortName + "\\Team.pdf", @Properties.Settings.Default.prnOpA3ResultsByTeam, _main);

                            if (open)
                            {
                                Process.Start(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Team.pdf");
                            }

                            if (print)
                            {
                                Printing.PrintPDF(@Properties.Settings.Default.ResultsPDFPath + e.Name + " Team.pdf");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Could not save the file " + @Properties.Settings.Default.ResultsPDFPath + e.Name + " Team.pdf");
                        }
                    }
                    wait.Bar3.increment();
                }// for each event

                wb.Close(false);
                excel.Quit();
            }// end ExcelLock

            wait.Complete();
            t.Abort();
            return(fileNames.ToArray());
        }