using System;
using System.Collections.Generic;
using System.Text;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.Merger;
using ceTe.DynamicPDF.Text;
using System.Configuration;
using System.Reflection;
using System.IO;
using ceTe.DynamicPDF.Imaging;
using Dundas.Charting.WebControl;
using System.Threading;
using System.Globalization;
using ceTe.DynamicPDF.PageElements;
using PDFGenerator.BusinessLayer.Utils;
using PDFGenerator.BusinessLayer;
//using PDFGeneratorLetteraBenvenuto;

public enum TipoContratto
{
    Old = 0,
    Affluent = 1,
    Private = 2,
    Dadvisory = 3
}
public enum TipoLettera
{
    LetteraAccettazioneConAttivazioneSei,
    LetteraAccettazioneAttivazioneSeiPosticipata,
    RistampaLettera,
    ReportPeriodico,
    LetteraCommerciale
}
namespace LetteraAccompagnamento
{
    public enum TipoReport
    {
        Diagnosi,
        Monitoraggio
    }
}

public abstract class LetteraBenvenuto
{
    private byte[] data;

    public byte[] Data
    {
        get { return data; }
    }

    protected Image firma;

    protected Document lettera;
    protected int fontSize,
                    idLettera;
    protected string path,
                        nome,
                        cognome,
                        appellativo,
                        nomeLettera,
                        indirizzo,
                        rete,
                        testoSaluti,
                        testoOggetto,
                        testo;
    protected FormattedTextArea cliente,
                                    indirizzoBanca,
                                    oggetto,
                                    luogo,
                                    saluti,
                                    corpo;

    protected DateTime dataGenerazione;

    protected TemplateProvider tp;

    /*** Fix idReportDiagnosi per FD 22/06/2023 - Pino ***/
    public int idReportDiagnosiLettera;
    /*****************************************************/

    protected LetteraBenvenuto(string rete, int idLettera, string testo, string nome, string cognome, string indirizzo, bool ristampa, string appellativo, string obj, string firma, TipoContratto contract, int idReportDiagnosi,DataThread _datathread = null)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        this.rete = rete;
        this.data = null;
        this.appellativo = appellativo;
        this.testoOggetto = obj;
        this.testoSaluti = firma;
        this.indirizzo = indirizzo;
        this.idLettera = idLettera;
        this.testo = testo;
        this.nome = nome;
        this.cognome = cognome;
        //this.codiceContratto = codiceContratto;
        dataGenerazione = DateTime.Now;

        PageDimensions dimensions = new PageDimensions(PageSize.A4, PageOrientation.Portrait);
        dimensions.BottomMargin = Convert.ToInt32(ConfigurationManager.AppSettings["MargineInfPX"]);
        dimensions.TopMargin = Convert.ToInt32(ConfigurationManager.AppSettings["MargineSupPX"]);
        dimensions.LeftMargin = Convert.ToInt32(ConfigurationManager.AppSettings["MargineSxPX"]);
        dimensions.RightMargin = Convert.ToInt32(ConfigurationManager.AppSettings["MargineDxPX"]);
        //Page page = new Page(dimensions);
        lettera = new Document();

        // Lettura Template

        //System.Reflection.Assembly thisExe;
        //thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        //System.IO.Stream file = null;
        //if (rete.ToUpper() == "F")
        //    file = thisExe.GetManifestResourceStream("PDFGeneratorLetteraBenvenuto.TemplateBF_New2.pdf");

        //else
        //    file = thisExe.GetManifestResourceStream("PDFGeneratorLetteraBenvenuto.templateSPI_New2.pdf");
        //tp = new TemplateProvider((int)contract,rete, false, idReportDiagnosi);
        tp = new TemplateProvider(_datathread);
        System.IO.Stream file = tp.TemplateWelcomeLetter;// TemplateReport;


        PdfDocument pp = new PdfDocument(file);
        Page page = new ImportedPage(pp.GetPage(1));
        page.Dimensions = dimensions;
        // pg = new ImportedPage(pp.GetPage(1));
        lettera.Pages.Add(page);

        fontSize = 10;//Solo per lettera di benvenuto Convert.ToInt32(ConfigurationManager.AppSettings["fontSize"]);

        string[] valoriTextArea;
        valoriTextArea = ConfigurationManager.AppSettings["AreaClientePX"].Split(',');
        cliente = new FormattedTextArea(string.Empty,
                                        Convert.ToInt32(valoriTextArea[0]),
                                        Convert.ToInt32(valoriTextArea[1]),
                                        Convert.ToInt32(valoriTextArea[2]),
                                        Convert.ToInt32(valoriTextArea[3]),
                                        FontFamily.Times,
                                        fontSize,
                                        true);
        valoriTextArea = ConfigurationManager.AppSettings["AreaLuogoPX"].Split(',');
        luogo = new FormattedTextArea(string.Empty,
                                        Convert.ToInt32(valoriTextArea[0]),
                                        Convert.ToInt32(valoriTextArea[1]),
                                        Convert.ToInt32(valoriTextArea[2]),
                                        Convert.ToInt32(valoriTextArea[3]),
                                        FontFamily.Times,
                                        fontSize,
                                        true);
        valoriTextArea = ConfigurationManager.AppSettings["AreaOggettoPX"].Split(',');
        oggetto = new FormattedTextArea(string.Empty,
                                        Convert.ToInt32(valoriTextArea[0]),
                                        Convert.ToInt32(valoriTextArea[1]),
                                        Convert.ToInt32(valoriTextArea[2]),
                                        Convert.ToInt32(valoriTextArea[3]),
                                        FontFamily.Times,
                                        fontSize,
                                        true);
        valoriTextArea = ConfigurationManager.AppSettings["AreaCorpoPX"].Split(',');
        corpo = new FormattedTextArea(string.Empty,
                                        Convert.ToInt32(valoriTextArea[0]),
                                        Convert.ToInt32(valoriTextArea[1]),
                                        Convert.ToInt32(valoriTextArea[2]),
                                        Convert.ToInt32(valoriTextArea[3]),
                                        FontFamily.Times,
                                        fontSize,
                                        true);
        valoriTextArea = ConfigurationManager.AppSettings["AreaSalutiPX"].Split(',');
        this.saluti = new FormattedTextArea(string.Empty,
                                        Convert.ToInt32(valoriTextArea[0]),
                                        Convert.ToInt32(valoriTextArea[1]) + ((this.rete == "S") ? 0 : 0),
                                        Convert.ToInt32(valoriTextArea[2]),
                                        Convert.ToInt32(valoriTextArea[3]),
                                        FontFamily.Times,
                                        fontSize,
                                        true);

        // Fc 03/07/2015
        // modifica "a mano"  del valore impostato xFirma a 290
        //int xFirma = (int)(saluti.X + (this.saluti.Width - 94) / 2);
        int xFirma = 290;

        /*** Fix - Modifica posizionamento saluti - 03/03/2023 - Pino ***/
        saluti.Y -= 30;
        if (rete.ToUpper() == "W")
        {
            this.testoSaluti = string.Concat("<br>", this.testoSaluti,"</br>");
            saluti.X += 75;
        }
        else
        {
            if (rete.ToUpper() == "S")
            {
                this.testoSaluti = string.Concat("<br>", this.testoSaluti, "</br>");
                saluti.X += 50;
            }
            else
                saluti.X += 50;
        }
        /*****************************************************************/

        int yFirma = (int)(saluti.Y - saluti.Height + tp.SalutiYBonus) - ((this.rete == "S") ? 4 : 0);
        if (ristampa)
            this.firma = GetImageByResourceName("PDFGenerator.LettereBenvenuto.Firma_Ristampa.jpg", xFirma, yFirma, (float)0.2);
        else
        {
            /*** Fix Modifica posizionamento Firma - 03/03/2023 - Pino ***/
            //switch (rete.ToUpper())
            //{
            //    case "F":
            //        this.firma = GetImageByResourceName("PDFGeneratorLetteraBenvenuto.firma_Fideuram.jpg", xFirma, yFirma + 10, (float)0.15);
            //        break;
            //    case "S":
            //        this.firma = GetImageByResourceName("PDFGeneratorLetteraBenvenuto.firma_Fideuram.jpg", xFirma, yFirma + 14, (float)0.15);
            //        break;
            //    case "W":
            //        this.firma = GetImageByResourceName("PDFGeneratorLetteraBenvenuto.firma_SanPaoloInvest.jpg", xFirma, yFirma - 7, (float)0.2);
            //        break;
            //}

            switch (rete.ToUpper())
            {
                case "F":
                    this.firma = GetImageByResourceName("PDFGenerator.LettereBenvenuto.firma_Fideuram.jpg", xFirma + 65, yFirma + 10, (float)0.15);
                    break;
                case "S":
                    //this.firma = GetImageByResourceName("PDFGeneratorLetteraBenvenuto.firma_Fideuram.jpg", xFirma + 65, yFirma + 15, (float)0.15);
                    this.firma = GetImageByResourceName("PDFGenerator.LettereBenvenuto.firma_Fideuram.jpg", xFirma + 65, yFirma + 35, (float)0.15);
                    break;
                case "W":
                    //this.firma = GetImageByResourceName("PDFGeneratorLetteraBenvenuto.firma_SanPaoloInvest.jpg", xFirma + 85, yFirma - 20, (float)0.2);
                    this.firma = GetImageByResourceName("PDFGenerator.LettereBenvenuto.firma_SanPaoloInvest.jpg", xFirma + 85, yFirma + 25, (float)0.15);
                    break;
            }
            /***************************************************************************************************************/
        }



        oggetto.Style.Paragraph.Align = ceTe.DynamicPDF.TextAlign.Justify;
        corpo.Style.Paragraph.Align = ceTe.DynamicPDF.TextAlign.Justify;
        luogo.Style.Paragraph.Align = ceTe.DynamicPDF.TextAlign.Right;

        page.Elements.Add(corpo);
        page.Elements.Add(cliente);
        page.Elements.Add(oggetto);
        page.Elements.Add(luogo);
        //page.Elements.Add(indirizzoBanca);
        if (!(this is LetteraCommerciale) || (this is LetteraCommerciale && ((LetteraCommerciale)this).PageNumber == 1))
        {
            page.Elements.Add(this.firma);
            page.Elements.Add(saluti);
        }

        //** NOTA
        //FC 26/06/2015
        // MOdificato per nuovo template
        //float xNota = 0;
        //float yNota = 712;//709
        float xNota = 27;
        float yNota = tp.SignYPosition;// 716; //720
        float wNota = 150;

        /*** Fix per eliminare la nota nella lettera di benvenuto per i report di FD 23/05/2023 - Pino ***/
        
        if (idReportDiagnosi != 6)
        {
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Line(xNota, yNota, xNota + wNota, yNota, 0.5F, RgbColor.Black));
            page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label(
                "�Il private banker � un professionista della consulenza finanziaria, previdenziale e assicurativa iscritto all�Albo Unico dei Consulenti Finanziari."//pkall�albo dei promotori finanziari."

                , xNota
                , yNota + 3
                , 480
                , 30
                , new OpenTypeFont("times.ttf")
                , 9
                ));
        }
        /**************************************************************************************************/
        //** NOTA 
        path = ConfigurationManager.AppSettings["pathScritturaLettera"];
    }

    public void writePDF()
    {
        while (Data == null)
        {
            data = lettera.Draw();
           //pk Thread.Sleep(50);
        }
        //DECOMMENTARE PER EFFETTUARE LE PROVE, SALVA LA LETTERA SUL PERCORSO
        //SPECIFICATO SU pathScritturaLettera
        //lettera.Draw(path + nomeLettera + ".pdf"); 

    }

    protected static Image GetImageByResourceName(string resourceName, int X, int Y, float ratio)
    {
        Assembly a = Assembly.GetExecutingAssembly();


        try
        {
            Stream stream = a.GetManifestResourceStream(resourceName);
            Image gif = new Image(stream, X, Y, ratio);

            return gif;
        }
        catch
        {
            return null;
        }
    }


    protected abstract void settaCampi();
}