﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Data;


// Générer dans Kitbox/bin/debug/Invoice
namespace KitBox
{
    public class BuildInvoice
    {
        public static string setTitle(int number)
        {
            string title = "Invoice of order n°" + number;
            return title;
        }

        public static string setPersonInfo(Person person)
        {
            String personInfo = String.Format("\n{0} - {1} - {2} - {3}\n\n", person.Name, person.Phone, person.Address, person.Email);
            return personInfo;
        }

        public static void setInvoiceInfo(Invoice invoice, StreamWriter invoiceNote)
        {
            int j;

            invoiceNote.Write("---------------------------------------\n");
            DataTable dt = new DataTable();
            dt.Columns.Add("Date");
            dt.Columns.Add("Customer Id");
            dt.Columns.Add("Statut");
            dt.Rows.Add(invoice.Date, invoice.UserId, invoice.Status);

            foreach (DataColumn column in dt.Columns)
            {
                invoiceNote.Write(column.ColumnName + " | ");
            }
            invoiceNote.Write("\n");

            object[] array = dt.Rows[0].ItemArray;
            for (j = 0; j < array.Length; j++)
            {
                invoiceNote.Write(array[j] + " | ");
            }
            invoiceNote.Write("\n");
            invoiceNote.Write("---------------------------------------\n\n");
        }

        public static string setTitleBloc(int number)
        {
            string title = "Bloc n°" + number + "\n";
            title += "---------------------------------------\n";
            return title;
        }

        public static string newBloc(int blocNumber)
        {
            string stringBloc = "";
            stringBloc += setTitleBloc(blocNumber);
            return stringBloc;
        }

        public static DataTable itemBloc(List<InvoiceItem> invoiceItemBloc, Company kitbox)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Code product");
            dt.Columns.Add("Type");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Unit price (€)");
            dt.Columns.Add("Available");

            foreach (InvoiceItem item in invoiceItemBloc)
            {
                bool notAvailable = kitbox.ProductManager.CheckStock(item.ProductId);
                Console.WriteLine(notAvailable);
                if (notAvailable)
                {
                    dt.Rows.Add(item.ProductId, item.Type, item.Quantity, item.Price, "Out of stock");
                }
                else
                {
                    dt.Rows.Add(item.ProductId, item.Type, item.Quantity, item.Price, "In stock");
                }
            }
            return dt;
        }

        public static void Run(Company kitbox, Invoice invoice)
        {
            List<InvoiceItem> invoiceItem = kitbox.InvoiceManager.SelectInvoiceItem(invoice);
            Role role = Role.CUSTOMER;
            Person person = kitbox.PersonManager.SelectPersonById(role, invoice.UserId);

            string directory = "Invoice/";

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string path = Path.Combine(directory, "invoice" + invoice.Id + ".txt");
            if (!File.Exists(path))
            {
                File.Create(path);
            }

            using (StreamWriter invoiceNote = File.CreateText(path))
            {
                int i;
                int k;
                List<InvoiceItem> invoiceItemBloc = new List<InvoiceItem>();

                for (k = 1; k < 8; k++)
                {
                    for (i = 0; i < invoiceItem.Count; i++)
                    {
                        if (invoiceItem[i].NbreBloc == k)
                        {
                            invoiceItemBloc.Add(invoiceItem[i]);
                        }
                    }

                    DataTable dt = itemBloc(invoiceItemBloc, kitbox);

                    if (k == 1)
                    {
                        invoiceNote.Write(setTitle(invoice.Id));
                        invoiceNote.Write(setPersonInfo(person));
                        setInvoiceInfo(invoice, invoiceNote);
                        invoiceNote.Write("\n\n");

                        foreach (DataColumn column in dt.Columns)
                        {
                            invoiceNote.Write(column.ColumnName + " | ");
                        }
                        invoiceNote.Write("\n\n");
                    }

                    invoiceNote.Write(newBloc(k));

                    foreach (DataRow row in dt.Rows)
                    {
                        int j;
                        object[] array = row.ItemArray;
                        for (j = 0; j < array.Length; j++)
                        {
                            invoiceNote.Write(array[j] + " | ");
                        }
                        invoiceNote.Write("\n");
                    }
                    invoiceItemBloc.Clear();
                    invoiceNote.Write("\n\n\n");
                }
                invoiceNote.Write("---------------------------------------\n");
                invoiceNote.Write("TOTAL INVOICE = " + invoice.Price + "€");
                invoiceNote.Write("\n---------------------------------------\n\n");
                invoiceNote.Write("*****END OF INVOICE**** " + DateTime.Now.ToString());
                invoiceNote.Flush();
                invoiceNote.Close();
            }
        }
    }
}
