using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace PDFGenerator.BusinessLayer.DataSection
{

    /// <summary>
    /// Summary description for DS80EvoluzionePiramideBisognics
    /// </summary>
    public class DSS80EvoluzionePiramideBisogni : IDataSection
    {
        public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
        {
            DataSectionResult dsr = new DataSectionResult();

            FormatNum formatNum = new FormatNum();
            //dati del grafico + dati della tabella
            DataTable dt = SectionManager.GetDataSection(tabelleSessione, querySql, dataThread);
            DataSetS80 ds80 = new DataSetS80();


            // dati tabella
            //V
            DataView dv = new DataView(dt);
            DataTable dtDatiTabella = dv.ToTable("DatiTabella", true, "Fase", "Controvalorecomplessivo", "Partita_viaggiante", "Risorsenonallocate", "Contocorrente");
            int nColonne = dtDatiTabella.Rows.Count; // numero di colonne da mostrare, corrisponde al nuemro delle FASI

            //int nColonne = dt.Rows.Count;

            //

            //Se è presente un solo istogramma la sezione non viene mostrata 
            //Richiesta fatta dal cliente.
            if (nColonne > 1)
            {
                DateTime? dataAvvioMonitoraggio= null;
                DateTime dataPrecedenteInvioReport = new DateTime();

                #region Data Avvio Monitoraggio

                DataView dvAvvioMonitoraggio = new DataView(dt);
                dvAvvioMonitoraggio.RowFilter = ("Fase = 1");
                if (dvAvvioMonitoraggio.Count > 0)
                {
                    DataRow row = dvAvvioMonitoraggio[0].Row;
                    dataAvvioMonitoraggio = Convert.ToDateTime(row["DataFase"].ToString());
                }
                #endregion

                #region Data Precedente Invio Report

                DataView dwPrecedenteInvioReport = new DataView(dt);
                dwPrecedenteInvioReport.RowFilter = ("Fase = 2");
                if (dwPrecedenteInvioReport.Count > 0)
                {
                    DataRow rowPrecedenteInvioReport = dwPrecedenteInvioReport[0].Row;
                    dataPrecedenteInvioReport = Convert.ToDateTime(rowPrecedenteInvioReport["DataFase"].ToString());
                }

                #endregion

                // caption dell' header della Tabella
                if (dataAvvioMonitoraggio.HasValue)
                {
                    ds80.DatiTabella.DescrizioneColumn.Caption = string.Empty; // la setto a stringa vuota
                    ds80.DatiTabella.Fase1Column.Caption = "Avvio Monitoraggio <BR>(" + dataAvvioMonitoraggio.Value.ToShortDateString() + ")";
                }

                if (dwPrecedenteInvioReport.Count > 0)
                    ds80.DatiTabella.Fase2Column.Caption = "Precedente invio report <BR>(" + dataPrecedenteInvioReport.ToShortDateString() + ")";
                else
                    ds80.DatiTabella.Fase2Column.Caption = string.Empty;

                ds80.DatiTabella.Fase3Column.Caption = "Situazione Corrente <BR>(" + dataThread.DataFineTrimestreCorrente.ToShortDateString() + ")";

                DataSetS80.DatiTabellaRow drDatiTabella;
                DataRow[] drFase1 = dtDatiTabella.Select(" fase  = 1");
                DataRow[] drFase2 = dtDatiTabella.Select(" fase  = 2");
                DataRow[] drFase3 = dtDatiTabella.Select(" fase  = 3");


                #region RISORSE FINANZIARIE
                //CONTROVALORE COMPLESSIVO -------------------------------------------------------------------------

                #region Determinazione Controvalore complessivo
                decimal controvalorecomplessivofase1 = 0;
                decimal controvalorecomplessivofase2 = 0;
                decimal controvalorecomplessivofase3 = 0;

                decimal controvalorefase1 = 0;
                decimal controvalorefase2 = 0;
                decimal controvalorefase3 = 0;
                decimal contocorrentefase1 = 0;
                decimal contocorrentefase2 = 0;
                decimal contocorrentefase3 = 0;
                decimal partiteviaggiantifase1 = 0;
                decimal partiteviaggiantifase2 = 0;
                decimal partiteviaggiantifase3 = 0;

                // controvalore
                if (drFase1.Length > 0 && drFase1[0]["controvalorecomplessivo"] != DBNull.Value)
                    controvalorefase1 = Convert.ToDecimal(drFase1[0]["controvalorecomplessivo"]);

                if (drFase2.Length > 0 && drFase2[0]["controvalorecomplessivo"] != DBNull.Value)
                    controvalorefase2 = Convert.ToDecimal(drFase2[0]["controvalorecomplessivo"]);

                if (drFase3.Length > 0 && drFase3[0]["controvalorecomplessivo"] != DBNull.Value)
                    controvalorefase3 = Convert.ToDecimal(drFase3[0]["controvalorecomplessivo"]);


                // conto corrente
                if (drFase1.Length > 0 && drFase1[0]["contocorrente"] != DBNull.Value)
                    contocorrentefase1 = Convert.ToDecimal(drFase1[0]["contocorrente"]);

                if (drFase2.Length > 0 && drFase2[0]["contocorrente"] != DBNull.Value)
                    contocorrentefase2 = Convert.ToDecimal(drFase2[0]["contocorrente"]);

                if (drFase3.Length > 0 && drFase3[0]["contocorrente"] != DBNull.Value)
                    contocorrentefase3 = Convert.ToDecimal(drFase3[0]["contocorrente"]);

                // partite viaggianti
                if (drFase1.Length > 0 && drFase1[0]["Partita_viaggiante"] != DBNull.Value)
                    partiteviaggiantifase1 = Convert.ToDecimal(drFase1[0]["Partita_viaggiante"]);

                if (drFase2.Length > 0 && drFase2[0]["Partita_viaggiante"] != DBNull.Value)
                    partiteviaggiantifase2 = Convert.ToDecimal(drFase2[0]["Partita_viaggiante"]);
              

                partiteviaggiantifase3 = dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento;

                decimal risorsenonallocateFase1 = drFase1.Length == 0 ? 0 : (decimal)drFase1[0]["risorsenonallocate"];
                decimal risorsenonallocateFase2 = drFase2.Length == 0 ? 0 : (decimal)drFase2[0]["risorsenonallocate"];
                decimal risorsenonallocateFase3 = drFase3.Length == 0 ? 0 : (decimal)drFase3[0]["risorsenonallocate"];

                //il controvalore già include il c/c bisogna scorporarlo, tolgo anche le partite viaggianti (solo per fase2)
                controvalorefase1 = controvalorefase1 - contocorrentefase1 ;
                controvalorefase2 = controvalorefase2 - contocorrentefase2 - partiteviaggiantifase2;
                controvalorefase3 = controvalorefase3 - contocorrentefase3;

                controvalorecomplessivofase1 = controvalorefase1 + contocorrentefase1 + partiteviaggiantifase1;// + risorsenonallocateFase1;
                controvalorecomplessivofase2 = controvalorefase2 + contocorrentefase2 + partiteviaggiantifase2;// +risorsenonallocateFase2;
                controvalorecomplessivofase3 = controvalorefase3 + contocorrentefase3 + partiteviaggiantifase3;// +risorsenonallocateFase3;

                #endregion

                drDatiTabella = ds80.DatiTabella.NewDatiTabellaRow();
                drDatiTabella.Descrizione = "Risorse finanziarie";
                drDatiTabella.NumeroFasi = nColonne;
                if (drFase1.Length > 0)
                    drDatiTabella.Fase1 = (drFase1.Length == 0 ? "0€" : formatNum.ConvertNum(controvalorefase1, 2) + " €");

                if (drFase2.Length > 0)
                    drDatiTabella.Fase2 = (drFase2.Length == 0 ? "0€" : formatNum.ConvertNum(controvalorefase2, 2) + " €");


                if (drFase3.Length > 0)
                    drDatiTabella.Fase3 = (drFase3.Length == 0 ? "0€" : formatNum.ConvertNum(controvalorefase3, 2) + " €");


                drDatiTabella.NumeroFasi = nColonne;
                if (drFase1.Length > 0 || drFase2.Length > 0 || drFase3.Length > 0)
                    ds80.DatiTabella.AddDatiTabellaRow(drDatiTabella);

                #endregion

                #region RISORSE NON ALLOCATE
                ////RISORSE NON ALLOCATE -------------------------------------------------------------------------
                //drDatiTabella = ds80.DatiTabella.NewDatiTabellaRow();
                //drDatiTabella.Descrizione = "<i><font pointSize='7'>" + "Risorse non allocate" + "</font></i>";
                //drDatiTabella.NumeroFasi = nColonne;
                //if (drFase1.Length > 0)
                //    drDatiTabella.Fase1 = (drFase1.Length == 0 ? "<i>0</i>" : "<i>" + formatNum.ConvertNum(drFase1[0]["risorsenonallocate"].ToString(), 2) + " €</i>");

                //// drDatiTabella.Fase2 = (drFase2.Length == 0 ? "<i>0</i>" : drFase2[0]["risorsenonallocate"].ToString() + " €");

                //if (drFase3.Length > 0)
                //    drDatiTabella.Fase3 = (drFase3.Length == 0 ? "<i>0</i>" : "<i>" + formatNum.ConvertNum(drFase3[0]["risorsenonallocate"].ToString(), 2) + " €</i>");


                ////se per tutti e 3 gli istogrammi non sono presenti “risorse non allocate” 
                ////la relativa riga non deve essere riportata
                //decimal valoreRisorsenonallocateFase1 = 0;
                //decimal valoreRisorsenonallocateFase2 = 0;
                //decimal valoreRisorsenonallocateFase3 = 0;
                //if (drFase1.Length > 0)
                //    valoreRisorsenonallocateFase1 = Convert.ToDecimal(drFase1[0]["risorsenonallocate"].ToString());
                ////if (drFase2.Length > 0)
                ////    valoreRisorsenonallocateFase2 = Convert.ToDecimal(drFase2[0]["risorsenonallocate"].ToString());
                //if (drFase3.Length > 0)
                //    valoreRisorsenonallocateFase3 = Convert.ToDecimal(drFase3[0]["risorsenonallocate"].ToString());
                //if (valoreRisorsenonallocateFase1 + valoreRisorsenonallocateFase2 + valoreRisorsenonallocateFase3 > 0)
                //    ds80.DatiTabella.AddDatiTabellaRow(drDatiTabella);
                #endregion

                #region CONTOCORRENTE
                //CONTOCORRENTE -------------------------------------------------------------------------
                drDatiTabella = ds80.DatiTabella.NewDatiTabellaRow();
                drDatiTabella.Descrizione = "Conto corrente";
                drDatiTabella.NumeroFasi = nColonne;
                if (drFase1.Length > 0)
                    drDatiTabella.Fase1 = (drFase1.Length == 0 ? "0€" : formatNum.ConvertNum(drFase1[0]["contocorrente"].ToString(), 2) + " €");

                if (drFase2.Length > 0)
                    drDatiTabella.Fase2 = (drFase2.Length == 0 ? "0€" : formatNum.ConvertNum(drFase2[0]["contocorrente"].ToString(), 2) + " €");

                if (drFase3.Length > 0)
                    drDatiTabella.Fase3 = (drFase3.Length == 0 ? "0€" : formatNum.ConvertNum(drFase3[0]["contocorrente"].ToString(), 2) + " €");


                //se per tutti e 3 gli istogrammi non è presente “conto corrente”  (c/c = 0) 
                //la relativa riga non deve essere riportata
                decimal valoreContoCorrenteFase1 = 0;
                decimal valoreContoCorrenteFase2 = 0;
                decimal valoreContoCorrenteFase3 = 0;
                if (drFase1.Length > 0)
                    valoreContoCorrenteFase1 = Convert.ToDecimal(drFase1[0]["contocorrente"].ToString());
                if (drFase2.Length > 0)
                    valoreContoCorrenteFase2 = Convert.ToDecimal(drFase2[0]["contocorrente"].ToString());
                if (drFase3.Length > 0)
                    valoreContoCorrenteFase3 = Convert.ToDecimal(drFase3[0]["contocorrente"].ToString());

                //V la riga del contocorrente deve uscire sempre
                //if (valoreContoCorrenteFase1 + valoreContoCorrenteFase2 + valoreContoCorrenteFase3 != 0)
                ds80.DatiTabella.AddDatiTabellaRow(drDatiTabella);
                #endregion

                #region PARTITE VIAGGIANTI O INVESTIMENTI IN CORSO
                // PARTITE VIAGGIANTI O INVESTIMENTI IN CORSO
                drDatiTabella = ds80.DatiTabella.NewDatiTabellaRow();
                drDatiTabella.Descrizione = Resource.PartiteViaggianti;
                drDatiTabella.NumeroFasi = nColonne;
                if (drFase1.Length > 0)
                    drDatiTabella.Fase1 = (drFase1.Length == 0 ? "0€" : formatNum.ConvertNum(drFase1[0]["Partita_viaggiante"].ToString(), 2) + " €");

                if (drFase2.Length > 0)
                    drDatiTabella.Fase2 = (drFase2.Length == 0 ? "0€" : formatNum.ConvertNum(drFase2[0]["Partita_viaggiante"].ToString(), 2) + " €");

                if (drFase3.Length > 0)
                    drDatiTabella.Fase3 = (drFase3.Length == 0 ? "0€" : formatNum.ConvertNum(dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento) + " €");

                decimal valorePartiteViaggiantiFase1 = 0;
                decimal valorePartiteViaggiantiFase2 = 0;
                decimal valorePartiteViaggiantiFase3 = 0;
                if (drFase1.Length > 0)
                    if (drFase1[0]["Partita_viaggiante"] != DBNull.Value)
                        valorePartiteViaggiantiFase1 = Convert.ToDecimal(drFase1[0]["Partita_viaggiante"]);
                    else
                        valorePartiteViaggiantiFase1 = 0;

                if (drFase2.Length > 0)
                    if (drFase2[0]["Partita_viaggiante"] != DBNull.Value)
                        valorePartiteViaggiantiFase2 = Convert.ToDecimal(drFase2[0]["Partita_viaggiante"]);
                    else
                        valorePartiteViaggiantiFase2 = 0;

                if (drFase3.Length > 0)
                    valorePartiteViaggiantiFase3 = dataThread.PartiteViaggiantiInvestimento + dataThread.PartiteViaggiantiDisinvestimento;

                if (valorePartiteViaggiantiFase1 + valorePartiteViaggiantiFase2 + valorePartiteViaggiantiFase3 > 0)
                    ds80.DatiTabella.AddDatiTabellaRow(drDatiTabella);

                #endregion

                #region TOTALE
                DataSetS80.DatiTotaleRow drDatiTotale;
                drDatiTotale = ds80.DatiTotale.NewDatiTotaleRow();
                drDatiTotale.Descrizione = "Totale Patrimonio";

                drDatiTotale.Fase1 = formatNum.ConvertNum(controvalorecomplessivofase1) + " €";
                drDatiTotale.Fase2 = formatNum.ConvertNum(controvalorecomplessivofase2) + " €";
                drDatiTotale.Fase3 = formatNum.ConvertNum(controvalorecomplessivofase3) + " €";

                //DataView dwTotale = new DataView(dtDatiTabella);
                //DataTable dtTotale;

                //dwTotale.RowFilter = " Fase = 1";
                //dtTotale = dwTotale.ToTable(true, "controvalorecomplessivo");
                //drDatiTotale.Fase1 = formatNum.ConvertNum(dtTotale.Rows[0][0]);

                //dwTotale.RowFilter = " Fase = 2";
                //dtTotale = dwTotale.ToTable(true, "controvalorecomplessivo");
                //if(dtTotale.Rows.Count>0)
                //    drDatiTotale.Fase2 = formatNum.ConvertNum(dtTotale.Rows[0][0]);


                //dwTotale.RowFilter = " Fase = 3";
                //dtTotale = dwTotale.ToTable(true, "controvalorecomplessivo");
                //drDatiTotale.Fase3 = formatNum.ConvertNum(dtTotale.Rows[0][0]);            

                ds80.DatiTotale.AddDatiTotaleRow(drDatiTotale);
                #endregion

                // imposto il valore di ritorno
                dsr.Esito = ds80.DatiTabella.Rows.Count;
            }
            else
            {
                dsr.Esito = 0;
            }

            dsr.DatiSezione = ds80;
            return dsr;
        }
    }
}