using System;
using System.Collections.Generic;
using System.Text;
using ceTe.DynamicPDF.Merger;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.BusinessLayer;
using ceTe.DynamicPDF.PageElements;
using System.Data.SqlClient;
using System.Data;
using ContrattoSei.Utilities;

namespace PDFGenerator.Presentation.Section
{
    class S80 : ISezione
    {

        string Titolo = " ";
        private string _testointroduttivo;

        /// <summary>
        /// Testo che introduce la sezione.
        /// </summary>
        public string TestoIntroduttivo
        {
            get { return _testointroduttivo; }
            set { _testointroduttivo = value; }
        }

        /// <summary>
        /// Setta il titolo.
        /// </summary>
        /// <param name="label"></param>
        public void setTitolo(string label)
        {
            Titolo = label;
        }



        public void writeSezione(DataThread dataThread)
        {

            DocumentPDF document = dataThread.DocumentPDF;
            DatiTabella datitab;
            DataSetS80 set = (DataSetS80)dataThread.Data.DatiSezione;

            //setta la datatable che contiene i dati per disegnare la tabella Dati
            datitab = new DatiTabella();
            datitab.table = set.Tables["DatiTabella"];
            int nFasi = Convert.ToInt32(datitab.table.Rows[0]["NumeroFasi"]); //il valore è uguale in tutte le righe
            datitab.table.Columns.Remove("NumeroFasi");// rimuovo la colonna, non va stampata

            string captionFase1 = datitab.table.Columns["Fase1"].Caption;
            string captionFase2 = datitab.table.Columns["Fase2"].Caption;
            string captionFase3 = datitab.table.Columns["Fase3"].Caption;

            // Aggiungo una pagina se la tabella non entra nella corrente.
            if (document.checkMargin(datitab.GetRowDim() * datitab.getNumRow()))
                document.addPage();

            document.setSezTitolo(Titolo);
            document.setChapterHeader(_testointroduttivo);


            #region Disegno Tabella Dati

            Tabella tabellaDati = new Tabella();
            tabellaDati.X = document.getMargineLeft();
            tabellaDati.Y = document.getLastPos();
            tabellaDati.Header = true;
            tabellaDati.SaltoPagina = false;
            tabellaDati.Datasource = datitab.table;

            DataRow rowTemplate = tabellaDati.Datasource.Rows[0];

            int larghezzaColonnaDescrizione = 512; //è variabile e da calcolare in base allo spazio disponibile
            int larghezzaColonnaFase1 = 130;
            int larghezzaColonnaFase2 = 150;
            int larghezzaColonnaFase3 = 130;
            int larghezzaUltimaColonna = 0; 

                       
            // Definizione delle colonne
            foreach (DataColumn column in tabellaDati.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    larghezzaUltimaColonna = larghezzaColonnaFase1;
                    tabellaDati.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    larghezzaUltimaColonna = larghezzaColonnaFase2;
                    tabellaDati.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    larghezzaUltimaColonna = larghezzaColonnaFase3;
                    tabellaDati.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));
                }
            }

            tabellaDati.Colonne.Insert(0,new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));
               
            tabellaDati.Draw(datitab, document);

            #endregion

            #region Disegno Tabella Totale

            //setta la datatable che contiene i Dati per disegnare la tabella del Totale
            datitab = new DatiTabella();
            datitab.table = set.Tables["DatiTotale"];

            Tabella tabellaTotale = new Tabella();
            tabellaTotale.X = document.getMargineLeft();
            tabellaTotale.Y = document.getLastPos() + tabellaDati.AltezzaTabella + 30;
            tabellaTotale.Header = false;
            tabellaTotale.SaltoPagina = false;
            tabellaTotale.Datasource = datitab.table;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaTotale.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, true, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, true, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, true, 8, true));
                }
            }

            tabellaTotale.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, true, 8, true));
           
            tabellaTotale.Draw(datitab, document);



            #endregion

            #region Disegno riquadro su Situazione Corrente e freccia

            // riquadro intorno la colonna Situazione Corrente
            float xRiquadro = 528 - larghezzaUltimaColonna + (larghezzaUltimaColonna / 2);

            ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
            Rectangle rettangoloSuSituazioneCorrente;
            rettangoloSuSituazioneCorrente = new Rectangle(xRiquadro, tabellaDati.Y - 10, larghezzaUltimaColonna - 35, tabellaDati.AltezzaTabella + tabellaTotale.AltezzaTabella);          
            rettangoloSuSituazioneCorrente.CornerRadius = 5;
            page.Elements.Add(rettangoloSuSituazioneCorrente);

            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file = thisExe.GetManifestResourceStream(UtilityManager.getAppSetting("FrecciaS80"));

            switch (nFasi)
            {
                case 2: // Una freccia tra "attivazione monitoraggio" e "situazione corrente" o tra "Precedente invio report" e "situazione corrente"
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 395, tabellaDati.Y - 10, 0.57F));
                    break;

                case 3: // Una freccia tra "attivazione monitoraggio" e  "precedente invio report" e tra  "precedente invio report" e "situazione corrente"
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 245, tabellaDati.Y - 10, 0.57F));
                    page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 395, tabellaDati.Y - 10, 0.57F));
                    break;

                default:
                    break;
            }



            #endregion

            // imposto l'ultima posizione scrivibile. //V era 50 in caso di sforamento mettere a 0
            document.setLastPos(tabellaDati.AltezzaTabella + tabellaTotale.AltezzaTabella); 
          
        }
    }
}