using System;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Web.UI;
using TheArtOfDev.HtmlRenderer.WinForms;

namespace AdvertsingProfitControl
{
    internal class BackPageGenerator
    {
        FrmLogConsole _console = FrmLogConsole.GetStaticInstance;
        public void RenderHtmlToImage()
        {
            var htmlCode = File.ReadAllLines(Application.StartupPath + "\\BackPage.html");
            var code = "";
            foreach (var line in htmlCode)
            {
                code = code + "\r\n" + line;
            }

            try
            {
                var imageFromHtml = HtmlRender.RenderToImage(code);
                imageFromHtml.Save(Application.StartupPath + "\\BackPage.png", ImageFormat.Png);
                imageFromHtml.Dispose();
            }
            catch (Exception e)
            {
                _console.WriteToLog(FrmLogConsole.Level.Critical,
                    "Failed to generate an updated image for the back page of Advertising Profit Control.");
                _console.WriteToLog(FrmLogConsole.Level.Debug, e.Message);
                _console.WriteToLog(FrmLogConsole.Level.Info,
                    "This is most likely a screw you from the GDI+ subsystem of Windows.");
            }
        }

        public void GenerateWeeklyInventoryControlPage(int dateId)
        {
            //var databaseTracker = new DatabaseTracker();
            //var databaseReader = new DatabaseReader();
            //var invoiceTable = databaseReader.ReturnInvoiceTable(dateId, databaseTracker.DatabaseConnectionString);
            //var weeklySalesTable = databaseReader.ReturnWeeklySalesFromDateId(dateId,
            //    databaseTracker.DatabaseConnectionString);
            var stringWriter = new StringWriter();
            var writer = new HtmlTextWriter(stringWriter);
            writer.Write("<!DOCTYPE html>\n");
            writer.RenderBeginTag(HtmlTextWriterTag.Html);
            writer.RenderBeginTag(HtmlTextWriterTag.Head);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
            writer.RenderBeginTag(HtmlTextWriterTag.Style);
            writer.Write("td{margins:0px; padding:0; border:1px solid black; text-align:left} table{table-layout:fixed;} td{ width: 1px; white-space:nowrap; }");
            writer.RenderEndTag();//style
            writer.RenderEndTag();//Head
            writer.RenderBeginTag(HtmlTextWriterTag.Body);
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-collapse: collapse");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            //Create the header as the first row.
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "8");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:center; width:800px");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<center>Weekly Inventory Control</center>");
            writer.RenderEndTag();//td
            //New row for Name & Location
            writer.RenderEndTag();//tr (header row)
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "5");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left"); //; width:560px
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Name & Location");
            writer.RenderEndTag();//td
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none"); //; width:240px
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Week Ending <u>");// + CleanDate(databaseReader.RetrieveDateStringById(dateId, databaseTracker.DatabaseConnectionString)) + "</u>");
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr
            //Begin rendering the proper header row for the table itself.
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //Purchases td
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "5");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:15px; font-size:10px; text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<center>Purchases</center>");
            writer.RenderEndTag();//td
            //Sales td
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:15px; font-size:10px; text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("<center>Sales</center>");
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr
            //Begin rendering the description row and the Sunday row in the sales table.
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //Date td
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center"); //; width:100px
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Date");
            writer.RenderEndTag();//td
            //Supplier and invoices td
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "font-size:10px; text-align:center"); //width:250px;
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Suppliers and Invoice Number");
            writer.RenderEndTag();//td
            //Net amount td
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "font-size:10px; text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Net Amount of Invoices At Cost");
            writer.RenderEndTag();//td
            //Net amount extended retail
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "font-size:10px; text-align:center"); //width:50px
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Net Amount of <br>Invoices<br> Extended Retail");
            writer.RenderEndTag();//td
            //Sunday td
            //writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:60px");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Sunday");
            writer.RenderEndTag();//td
            //Dollar amount for Sunday
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            var sundaySalesAmount = "Spam";//FormatNumberForDisplay(weeklySalesTable.Rows[0].ItemArray[0]).Split('.');
            writer.Write(sundaySalesAmount[0]);
            writer.RenderEndTag();//td
            //cents amount td
            //writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:10px");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(sundaySalesAmount[1]);
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr
            //Loop 35 times to build all the rows.
            double totalCostsOfSales = 0; //holds the total value of all the invoices purchases.
            //foreach (DataRow row in invoiceTable.Rows)
            //{
            //    //Index four (4) contains the Net Amount of Invoices Extended Retail values.
            //    double parsedString = 0;
            //    if (double.TryParse(row.ItemArray[4].ToString(), out parsedString))
            //    {
            //         totalCostsOfSales += parsedString;                   
            //    }
            //}

            for (var i = 0; i < 34; i++)
            {
                var purchasesColumnsRendered = false;
                var salesColumnsRendered = false;
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                //if (i < invoiceTable.Rows.Count)
                //{
                //    purchasesColumnsRendered = RenderPurchasesColumns(i, writer, invoiceTable);
                //}
                if ((i + 1) < 8)
                {
                    //padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write((i + 1) + ".");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    //Generates the SALES columns.
                    //salesColumnsRendered = RenderWeeklySalesColumns(i, writer, weeklySalesTable);
                }
                if (i == 7)
                {
                    //padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write((i + 1) + ".");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Taxable");
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }
                if (i == 16)
                {
                    //Gross Profit Row
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write((i + 1) + ".");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Gross Profit");
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }
                if (i == 19)
                {
                    //Padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write((i + 1) + ".");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Dollar Gross Profit");
                    writer.RenderEndTag();//td
                }
                if (i == 20)
                {
                    //Padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write((i + 1) + ".");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    double totalSales = 0;

                    //if (double.TryParse(weeklySalesTable.Rows[0].ItemArray[7].ToString(), out totalSales))
                    //{
                    //    var dollarGrossProfit = totalSales - totalCostsOfSales;
                    //    var dollarGrossProfitSplit = FormatNumberForDisplay(dollarGrossProfit).Split('.');
                    //    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    //    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    writer.Write(dollarGrossProfitSplit[0]);
                    //    writer.RenderEndTag(); //td
                    //    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    writer.Write(dollarGrossProfitSplit[1]);
                    //    writer.RenderEndTag(); //td
                    //}
                    //else
                    //{
                    //    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    writer.RenderEndTag();//td
                    //    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    writer.RenderEndTag();//td
                    //    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //    writer.RenderEndTag();//td  
                    //}
                }
                if (i == 21)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Total Purchases");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("% Gross Profit 41.9%");
                    writer.RenderEndTag();//td 
                    salesColumnsRendered = true;
                }
                if (i == 22)
                {
                    var dollarsAndCents = FormatNumberForDisplay(totalCostsOfSales).Split('.');
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Less Transfer & Credits");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "font-size:10px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Est. Wkly. Dept. Expense");
                    writer.RenderEndTag();//td 
                    //Align the text for the dollar amount to the ride side.
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(dollarsAndCents[0]);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(dollarsAndCents[1]);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }
                if (i == 23)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("1.");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; font-size:12px");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("% Gross obtained by dividing<br>Total Sales into Gross Profit");
                    writer.RenderEndTag();//td 
                    salesColumnsRendered = true;
                }
                if (i == 24)
                {
                    //padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("2.");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; border-right:1px solid black");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                }

                if (i == 25)
                {
                    //padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("3.");
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Manager_____________");
                    writer.RenderEndTag();//td
                }

                if (i > 25 && i < 29)
                {
                    //padding
                    if (purchasesColumnsRendered == false)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write(i - 22);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.RenderEndTag();//td
                        purchasesColumnsRendered = true;
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                }
                if (i == 29)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Total Purchases");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    purchasesColumnsRendered = true;
                    //
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }
                if (i == 30)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Less Total Transfers & Credits");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    purchasesColumnsRendered = true;
                    //
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }

                if (i == 31)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Net Purchases of Week");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    purchasesColumnsRendered = true;
                    //
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }

                if (i == 32)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Sales Per Man Hour_____");
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Salary Percentage______");
                    writer.RenderEndTag();//td
                    purchasesColumnsRendered = true;
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }

                if (i == 33)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Salary Dollars_____");
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Supplies______");
                    writer.RenderEndTag();//td
                    purchasesColumnsRendered = true;
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    salesColumnsRendered = true;
                }

                //Padding
                if (purchasesColumnsRendered == false && salesColumnsRendered == false)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write((i + 1) + ".");
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderEndTag();//td
                }
                //End Padding
                writer.RenderEndTag();//tr
            }
            writer.RenderEndTag();//table
            writer.RenderEndTag();//body
            writer.RenderEndTag();//html

            using (var streamWriter = new StreamWriter(Application.StartupPath + "\\BackPage.html"))
            {
                streamWriter.WriteLine(stringWriter.ToString());
            }
        }

        private bool RenderPurchasesColumns(int rowIndex, HtmlTextWriter writer, DataTable invoiceTable)
        {
            var purchasesColumsRender = false;
            if (rowIndex < invoiceTable.Rows.Count)
            {
                //Grab the date
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(CleanDate(invoiceTable.Rows[rowIndex].ItemArray[2].ToString()));
                writer.RenderEndTag();//td
                //Grab the invoice number and the supplier's name
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write((rowIndex + 1) + ". " + invoiceTable.Rows[rowIndex].ItemArray[0] + " " + invoiceTable.Rows[rowIndex].ItemArray[1]);
                writer.RenderEndTag();//td
                //Get the net amount of invoices at cost
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                //For now, if the net amount of invoices at cost are empty, throw in the comments in place of a blank space.
                writer.Write(invoiceTable.Rows[rowIndex].ItemArray[3].ToString() == ""
                    ? invoiceTable.Rows[rowIndex].ItemArray[5]
                    : FormatNumberForDisplay(invoiceTable.Rows[rowIndex].ItemArray[3]));
                writer.RenderEndTag();//td
                //Get the net amount of invoices extended retail (InvoiceNetAmount in the Invoice table)
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right"); //width:40px
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                var netAmountOfInvoicesExtendedRetail = FormatNumberForDisplay(invoiceTable.Rows[rowIndex].ItemArray[4]);
                var splitDollarAmount = netAmountOfInvoicesExtendedRetail.Split('.');
                writer.Write(splitDollarAmount[0]);
                writer.RenderEndTag();//td
                //Render the cents into their own column.
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left; width:10px"); //width:10px
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(splitDollarAmount[1]);
                writer.RenderEndTag();//td
                purchasesColumsRender = true;
            }
            return purchasesColumsRender;
        }

        private bool RenderWeeklySalesColumns(int rowIndex, HtmlTextWriter writer, DataTable weeklySalesTable)
        {
            var salesColumnsRendered = false;
            if ((rowIndex + 1) < 8)
            {
                //Generates the SALES columns.
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(weeklySalesTable.Columns[rowIndex + 1].ColumnName);
                writer.RenderEndTag(); //td
                var weeklySales = FormatNumberForDisplay(weeklySalesTable.Rows[0].ItemArray[rowIndex + 1]);
                var splitWeeklySalesAMount = weeklySales.Split('.');
                var dollarAmount = splitWeeklySalesAMount[0];
                //Align the dollar amount column text to the right side.
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(dollarAmount);
                writer.RenderEndTag(); //td
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(splitWeeklySalesAMount[1]);
                writer.RenderEndTag(); //td
                salesColumnsRendered = true;
            }
            return salesColumnsRendered;
        }

        private static string CleanDate(string date)
        {
            var tempDate = date.Split(' ');
            date = tempDate[0];
            return date;
        }

        private string FormatNumberForDisplay(object dollarAmount, bool includeCurrencySign = false)
        {
            var formattedNumber = "";
            double parsedValue = 0;
            if (double.TryParse(dollarAmount.ToString(), out parsedValue))
            {
                formattedNumber = parsedValue.ToString("C2");

                if (includeCurrencySign == false)
                {
                    formattedNumber = formattedNumber.Replace("$", "");
                }
            }
            else
            {
                formattedNumber = "0.00";
            }

            return formattedNumber;
        }
    }
}
