using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web.UI;
using Pechkin;
using AdvertisingProfitControlData;

namespace AdvertsingProfitControl
{
    internal class BackPageGenerator
    {
        /// <summary>
        /// Generates the back page of the report, Weekly Inventory Control, as a single PDF file.
        /// </summary>
        /// <param name="date"></param>
        public void GenerateBackPage(DateTime date)
        {
            var stringWriter = new StringWriter();
            var writer = new HtmlTextWriter(stringWriter);
            BuildReportBackPage(date, ref writer);
            if (!Enum.TryParse(Properties.Settings.Default.ReportPaperKind, out PaperKind paperKind))
            {
                paperKind = PaperKind.A4;
            }
            var backPage = new SimplePechkin(new GlobalConfig().SetPaperOrientation(false).SetPaperSize(paperKind)).Convert(stringWriter.ToString());
            //
            using (var fs = new FileStream(GlobalClasses.LogFilePath + "Back.pdf", FileMode.Create, FileAccess.Write))
            {
                fs.Write(backPage, 0, backPage.Length);
            }
        }
        /// <summary>
        /// Generates the HTML code for the back page, Weekly Inventory Control.
        /// </summary>
        /// <param name="date"></param>
        /// <param name="writer"></param>
        private void BuildReportBackPage(DateTime date, ref HtmlTextWriter writer)
        {
            var db = new AdvertisingProfitControlDbContext();
            var dateRecord = db.WeekEndingDates.SingleOrDefault(x => x.EndingDate == date);
            var invoices = db.Invoices.Where(x => x.FkDateId == dateRecord.Id).ToList();
            var weeklySale = db.WeeklySales.SingleOrDefault(x => x.FkDateId == dateRecord.Id);
            var taxable = db.Taxables.SingleOrDefault(x => x.FkDateId == dateRecord.Id);
            var costAnylsis = db.CostAnalysis.SingleOrDefault(x => x.FkDateId == dateRecord.Id);

            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:2px; border:1px solid black; text-align:left; vertical-align:bottom; font-size:" + Properties.Settings.Default.WicFontSize + "}");
            writer.RenderEndTag();//style
            writer.RenderEndTag();//Head
            writer.RenderBeginTag(HtmlTextWriterTag.Body);
            //writer.AddAttribute(HtmlTextWriterAttribute.Style, "page-break-inside: avoid; page-break-before:always");
            //writer.RenderBeginTag(HtmlTextWriterTag.Div);//div
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-collapse: collapse; " + Properties.Settings.Default.WicReportWidth + "; " + Properties.Settings.Default.WicReportHeight);
            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");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Weekly Inventory Control");
            writer.RenderEndTag();//td
            //New row for Name & Location
            writer.RenderEndTag();//tr (header row)
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "6");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Name & Location <u>Allen's of Hastings</u>");
            writer.RenderEndTag();//td
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            //TODO: Validate the last week's date
            //Grab last week's date
            writer.Write("Week Ending <u>" + date.AddDays(-7).ToString("d") + "</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, "6");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:15px; font-size:14px; text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Purchases");
            writer.RenderEndTag();//td
            //Sales td
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:15px; font-size:14px; text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Sales");
            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.Rowspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Date");
            writer.RenderEndTag();//td
            //row number cell header
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "vertical-align:top; border-right:none; text-align:left");
            writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);//row number header column
            writer.Write("**");
            writer.RenderEndTag();//end row number header column
            //Supplier and invoices td
            writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center; border-left:none");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Suppliers and<br>Invoice Number");
            writer.RenderEndTag();//td
            //Net amount td
            writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Net Amount<br>of Invoices<br>At Cost");
            writer.RenderEndTag();//td
            //Net amount extended retail
            writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Net Amount of<br>Invoices<br>Extended Retail");
            writer.RenderEndTag();//td
            //Sunday td
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
            if (weeklySale == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Sunday");
            writer.RenderEndTag();//td
            if (weeklySale != null)
            {
                //Dollar amount for Sunday
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write($"{weeklySale.Sunday:N2}");
                writer.RenderEndTag();//dollar amount Sunday end 
                writer.RenderEndTag();//tr                
            }
            //Row spanned row for the header row.
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
            if (weeklySale == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);//Monday cell
            writer.Write("Monday");
            writer.RenderEndTag();//Monday end
            if (weeklySale != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);//Monday dollar amount
                writer.Write($"{weeklySale.Monday:N2}");
                writer.RenderEndTag();//end Monday dollar amount                
            }
            writer.RenderEndTag();//end spanned row tr
            //
            for (var i = 0; i < 33; i++)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                if (i < invoices.Count)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-right:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //Date
                    writer.Write(invoices[i].InvoiceDate.ToString("d"));
                    writer.RenderEndTag(); //end date cell
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-right:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //row number
                    writer.Write((i + 1) + ".");
                    writer.RenderEndTag(); //end row number
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-right:none; border-left:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //supplier
                    writer.Write(invoices[i].Supplier.Name);
                    writer.RenderEndTag(); //end supplier
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-left:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //invoice number
                    writer.Write(invoices[i].InvoiceNumber);
                    writer.RenderEndTag(); //end invoice
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //at cost
                    writer.Write($@"{invoices[i].InvoiceNetAmountAtCost:N2}");
                    writer.RenderEndTag(); //end at cost
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //extended retail
                    writer.Write($@"{invoices[i].InvoiceNetAmount:N2}");
                    writer.RenderEndTag(); //end extended retail
                }
                else if (i < 20)
                {
                    //Empty rows.
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//date
                    writer.RenderEndTag();
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write((i + 1) + ".");//number to invoice number
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//at cost
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//extended retail
                    writer.RenderEndTag();
                }
                //Weekly sales cells
                if (i < 6)
                {
                    //Build weekly sales cells.
                    BuildWeeklySalesCell(i, ref writer, weeklySale);
                    writer.RenderEndTag();//tr
                    continue;
                }
                //Taxable header
                if (i == 6)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //Taxable header
                    writer.Write("Taxable");
                    writer.RenderEndTag();
                    writer.RenderEndTag();//tr
                    continue;
                }
                //Taxable cells
                if (i < 15)
                {
                    BuildTaxablesCells(i, ref writer, taxable);
                    writer.RenderEndTag();//tr
                    continue;
                }
                //Gross Profit header
                if (i == 15)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:center");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Gross Profit");
                    writer.RenderEndTag();//td Gross profit header
                    writer.RenderEndTag();//tr
                    continue;
                }
                //Gross profit
                if (i < 22)
                {
                    if (i == 20)
                    {
                        var totalAtCost = invoices.Sum(x => x.InvoiceNetAmountAtCost);
                        //Add Total Purchases row
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("<br>");
                        writer.RenderEndTag();
                        writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);//Total purchases cell
                        writer.Write("Total Purchases");
                        writer.RenderEndTag();//end Total purchases cell
                        //At cost
                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write($@"{totalAtCost:N2}");
                        writer.RenderEndTag();
                        //Extended retail
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("<br>");
                        writer.RenderEndTag();
                    }

                    if (i == 21)
                    {
                        //Add Total Purchases row
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("<br>");
                        writer.RenderEndTag();
                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                        writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);//Less transfer credits
                        writer.Write("Less Transfer & Credits");
                        writer.RenderEndTag();//end less transfer credits
                        //At cost
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("<br>");
                        writer.RenderEndTag();
                        //Extended retail
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("<br>");
                        writer.RenderEndTag();
                    }
                    BuildGrossProfitCells(i, ref writer, weeklySale, invoices);
                    writer.RenderEndTag();//tr
                    continue;
                }

                if (i < 28)
                {
                    //Empty rows.
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//date
                    writer.RenderEndTag();
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write((i - 21) + ".");//number to invoice number
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//at cost
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//extended retail
                    writer.RenderEndTag();
                    BuildInformationCells(i, ref writer);
                    writer.RenderEndTag();//tr
                    continue;
                }

                if (i == 28)
                {
                    //Total Purchases final row
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Total Purchases");
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//at cost
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//extended retail
                    writer.RenderEndTag();
                    writer.RenderEndTag();//tr
                    continue;
                }

                if (i == 29)
                {
                    //Less Total Transfers and Credit row
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Less Total Transfers & Credits");
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//at cost
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//extended retail
                    writer.RenderEndTag();
                    writer.RenderEndTag();//tr
                    continue;
                }

                if (i == 30)
                {
                    //Net Purchases row
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("Net Purchases of Week");
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//at cost
                    writer.RenderEndTag();
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");//extended retail
                    writer.RenderEndTag();
                    writer.RenderEndTag();//tr
                    continue;
                }

                if (i > 30)
                {
                    BuildCostAnalysisCells(i, ref writer, costAnylsis);
                }

                writer.RenderEndTag();//tr
            }
            writer.RenderEndTag();//table
            //writer.RenderEndTag();//div
            writer.RenderEndTag();//body
            writer.RenderEndTag();//html
        }

        private static void BuildWeeklySalesCell(int index, ref HtmlTextWriter writer, WeeklySale sales)
        {
            switch (index)
            {
                case 0:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Tuesday
                    writer.Write("Tuesday");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Tuesday
                    writer.Write($@"{sales.Tuesday:N2}");
                    writer.RenderEndTag();
                    break;
                case 1:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if(sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Wednesday
                    writer.Write("Wednesday");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.Wednesday:N2}");
                    writer.RenderEndTag();
                    break;
                case 2:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Thursday
                    writer.Write("Thursday");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.Thursday:N2}");
                    writer.RenderEndTag();
                    break;
                case 3:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Friday
                    writer.Write("Friday");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.Friday:N2}");
                    writer.RenderEndTag();
                    break;
                case 4:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Saturday
                    writer.Write("Saturday");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.Saturday:N2}");
                    writer.RenderEndTag();
                    break;
                case 5:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//total sales
                    writer.Write("Total Sales");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.TotalSales:N2}");
                    writer.RenderEndTag();
                    break;
            }
        }

        private static void BuildTaxablesCells(int index, ref HtmlTextWriter writer, Taxable taxable)
        {
            switch (index)
            {
                case 7:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Sunday
                    writer.Write("Sunday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Sunday:N2}");
                    writer.RenderEndTag();
                    break;
                case 8:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Monday
                    writer.Write("Monday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Monday:N2}");
                    writer.RenderEndTag();
                    break;
                case 9:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Tuesday
                    writer.Write("Tuesday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Tuesday
                    writer.Write($@"{taxable.Tuesday:N2}");
                    writer.RenderEndTag();
                    break;
                case 10:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Wednesday
                    writer.Write("Wednesday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Wednesday:N2}");
                    writer.RenderEndTag();
                    break;
                case 11:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Thursday
                    writer.Write("Thursday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Thursday:N2}");
                    writer.RenderEndTag();
                    break;
                case 12:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Friday
                    writer.Write("Friday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Friday:N2}");
                    writer.RenderEndTag();
                    break;
                case 13:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Saturday
                    writer.Write("Saturday");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Saturday:N2}");
                    writer.RenderEndTag();
                    break;
                case 14:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (taxable == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//total sales
                    writer.Write("Total Taxable");
                    writer.RenderEndTag();
                    if (taxable == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{taxable.Total:N2}");
                    writer.RenderEndTag();
                    break;
            }
        }

        private static void BuildGrossProfitCells(int index, ref HtmlTextWriter writer, WeeklySale sales, List<Invoice> invoices)
        {
            switch (index)
            {
                case 16:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Total sales
                    writer.Write("1. Total Sales");
                    writer.RenderEndTag(); 
                    if (sales == null) break; 
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write($@"{sales.TotalSales:N2}");
                    writer.RenderEndTag();
                    break;
                case 17:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Less cost of sales
                    writer.Write("2. Less Cost of Sales");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    if (invoices.Count > 0)
                    {
                        var costOfSales = invoices.Sum(x => x.InvoiceNetAmountAtCost);
                        if(costOfSales != 0)
                        writer.Write($@"{costOfSales:N2}");
                    }
                    writer.RenderEndTag();
                    break;
                case 18:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-bottom:none; ; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//dollar gross profit header cell
                    writer.Write("Dollar Gross Profit");
                    writer.RenderEndTag();
                    break;
                case 19:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border-top:none");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//dollar gross amount
                    writer.Write("<br>");//Empty cell
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    if (invoices.Count > 0)
                    {
                        var dollarGross = sales.TotalSales - invoices.Sum(x => x.InvoiceNetAmountAtCost);
                        if(dollarGross != 0)
                        writer.Write($@"{dollarGross:N2}");                            
                    }
                    writer.RenderEndTag();
                    break;
                case 20:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, sales == null ? string.Empty : "border-right:none;" + " text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Gross dollar percentage
                    writer.Write("% Gross Profit");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:right; border-left:none");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    if (invoices.Count > 0)
                    {
                        var grossDollarPercentage = (sales.TotalSales - invoices.Sum(x => x.InvoiceNetAmountAtCost)) / sales.TotalSales;
                        if(grossDollarPercentage != 0)
                        writer.Write($@"{grossDollarPercentage:P}");
                    }
                    writer.RenderEndTag();
                    break;
                case 21:
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "text-align:left");
                    if (sales == null) writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Weekly department Expense
                    writer.Write("Est. Wkly. Dept. Expense");
                    writer.RenderEndTag();
                    if (sales == null) break;
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<br>");
                    writer.RenderEndTag();
                    break;
            }
        }

        private static void BuildInformationCells(int index, ref HtmlTextWriter writer)
        {
            switch (index)
            {
                case 22:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//% gross info cell
                    writer.Write("% Gross obtained by Dividing<br>Total Sales into Gross Profit");
                    writer.RenderEndTag();
                    break;
                case 25:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Manager
                    writer.Write("Manager");
                    writer.RenderEndTag();
                    break;
                case 27:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//** info cell
                    writer.Write("** Remove Supply Purchases");
                    writer.RenderEndTag();
                    break;
            }
        }

        private static void BuildCostAnalysisCells(int index, ref HtmlTextWriter writer, CostAnalysi anaylsis)
        {
            switch (index)
            {
                case 31:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Sales per man hour
                    if (anaylsis != null)
                    {
                        writer.Write("Sales Per Man Hour <u>" + anaylsis.SalesPerManHour + "</u>");
                    }
                    else
                    {
                        writer.Write("Sales Per Man Hour ");
                    }
                    writer.RenderEndTag();
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Salary Percentage
                    if (anaylsis != null)
                    {
                        writer.Write("Salary Percentage <u>" + $@"{anaylsis.SalaryPercentage:P}" + "</u>");
                    }
                    else
                    {
                        writer.Write("Salary Percentage");
                    }
                    writer.RenderEndTag();
                    break;
                case 32:
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "4");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Salary Dollars
                    if (anaylsis != null)
                    {
                        writer.Write("Salary Dollars <u>" + $@"{anaylsis.SalaryDollar:N2}" + "</u>");
                    }
                    else
                    {
                        writer.Write("Salary Dollars");
                    }
                    writer.RenderEndTag();
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "border:none; text-align:left");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);//Supplies
                    if (anaylsis != null)
                    {
                        writer.Write("Salary Dollars <u>" + $@"{anaylsis.Supplies:N2}" + "</u>");
                    }
                    else
                    {
                        writer.Write("Salary Dollars");
                    }
                    writer.RenderEndTag();
                    break;
            }
        }
    }
}
