using DataAccessLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;


namespace PDFGenerator.BusinessLayer.Utils
{
    class TemplateProvider
    {
        string _templateReportPath;
        string _templateCopertinaPath;
        string _templateGlossarioPath;
        string _templateWelcomeLetterPath;
        public float _reportTypeFontColor_R;
        public float _reportTypeFontColor_G;
        public float _reportTypeFontColor_B;
        public float _chapterBackground_R;
        public float _chapterBackground_G;
        public float _chapterBackground_B;
        public float _chapterFontColor_R;
        public float _chapterFontColor_G;
        public float _chapterFontColor_B;
        public float _tableHeaderFontColor_R;
        public float _tableHeaderFontColor_G;
        public float _tableHeaderFontColor_B;
        private Stream _templateReportStream;
        private Stream _templateCopertinaStream;
        private Stream _templateGlossarioStream;
        private Stream _templateWelcomeLetterStream;

        int _tipoReport;
        string _rete;
        bool _periodico;

        /********* Fix Direct Bank 09/03/2023 - Pino *********************/
        public static int idReportDiagnosi;
        public static string CF;
        public static BusinessLayer.DataThread _dataThread;
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        /*****************************************************************/

        public TemplateProvider(int TipoReport, string Rete, bool Periodico)
        {
            Initialize(TipoReport, Rete, Periodico);
        }

        //public TemplateProvider(int TipoReport, string Rete, bool Periodico, string Flagnqp = "", string Flagprlrde = "", string Flagpg = "")
        //{
        //    Initialize(TipoReport, Rete, Periodico, Flagnqp, Flagprlrde, Flagpg);
        //}

        public bool Initialize(int TipoReport, string Rete, bool Periodico)
        {
            bool result = false;
            try
            {
                _tipoReport = TipoReport;
                _rete = Rete;
                _periodico = Periodico;
                using (DataAccessDE de = new DataAccessDE(DBProvider.SqlServerStampeC6))
                {

                    /*** Verifico se il datathred in corso contiene un Cf di FD 20/07/2023 - Pino ****/
                    //int idModello = _dataThread.ReportsType.Find(f => f.Descrizione == "Diagnosi").IdModello;

                    //if (_dataThread.CodiceFiscale.Equals("MRLSLV83E60D912U")) TipoReport = 3;

                    ////if (idModello == 6) TipoReport = 3;
                    /*********************************************************************************/


                    IDataReader rd = de.ExecuteDataReaderStoredProcedure(DBProvider.SqlServerStampeC6, "dbo.GetPDFTemplateConfiguration_Rosaspina",
                         new List<Parametro>()
                         {
                        new Parametro() { ParameterName = "Rete", Value = Rete },
                        new Parametro() { ParameterName = "Trimestrale", Value = (Periodico ? 1:0) },
                        new Parametro() { ParameterName = "Tipo", Value = TipoReport }
                         });
                    if (rd.Read())
                    {
                        //_templateReportPath = rd["TemplateReportPath"].ToString();
                        //_templateCopertinaPath = rd["TemplateCopertinaPath"].ToString();
                        //_templateGlossarioPath = rd["TemplateGlossarioPath"].ToString();
                        //_templateWelcomeLetterPath = rd["TemplateWelcomeLetterPath"].ToString();


                        ///***********  Forzatura Direct Banck per Test - Pino **************/
                        if (idReportDiagnosi != 6)
                        {
                            _templateReportPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\Templ_BFreport.pdf";
                            _templateCopertinaPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\Templ_copertinaSP.pdf";
                            _templateGlossarioPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\GlossarioBF_REPORT_MONITORAGGIO.pdf";
                            _templateWelcomeLetterPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\TemplateBF_New2.pdf";
                        }
                        else
                        {
                            /* PER  SIMULARE DIRECT BANK  Pino */
                            _templateReportPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\Templ_BFreport_DB.pdf";
                            _templateCopertinaPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\Templ_copertinaBF_DB_1.pdf";
                            _templateGlossarioPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\GlossarioBF_REPORT_MONITORAGGIO.pdf";
                            _templateWelcomeLetterPath = @"C:\Users\giuseppe.rotondo\Fideuram\Solution\TP_SEI_Project\root\ContrattoSEI\PDFGenerator\PDFTemplate\TemplateBF_New2_DB_2.pdf";
                        }
                        ///***************************************************/

                        logger.Info("Utils -> TipoReport: " + TipoReport);

                        logger.Info("Utils -> Path Template: " + _templateReportPath);


                        _reportTypeFontColor_R = (float)(((Double)Int32.Parse(rd["ReportTypeFontColor_R"].ToString())) / 255.00);
                        _reportTypeFontColor_G = (float)(((Double)Int32.Parse(rd["ReportTypeFontColor_G"].ToString())) / 255.00);
                        _reportTypeFontColor_B = (float)(((Double)Int32.Parse(rd["ReportTypeFontColor_B"].ToString())) / 255.00);

                        _chapterBackground_R = (float)(((Double)Int32.Parse(rd["ChapterBackground_R"].ToString())) / 255.00);
                        _chapterBackground_G = (float)(((Double)Int32.Parse(rd["ChapterBackground_G"].ToString())) / 255.00);
                        _chapterBackground_B = (float)(((Double)Int32.Parse(rd["ChapterBackground_B"].ToString())) / 255.00);

                        _chapterFontColor_R = (float)(((Double)Int32.Parse(rd["ChapterFontColor_R"].ToString())) / 255.00);
                        _chapterFontColor_G = (float)(((Double)Int32.Parse(rd["ChapterFontColor_G"].ToString())) / 255.00);
                        _chapterFontColor_B = (float)(((Double)Int32.Parse(rd["ChapterFontColor_B"].ToString())) / 255.00);

                        _tableHeaderFontColor_R = (float)(((Double)Int32.Parse(rd["TableHeaderFontColor_R"].ToString())) / 255.00);
                        _tableHeaderFontColor_G = (float)(((Double)Int32.Parse(rd["TableHeaderFontColor_G"].ToString())) / 255.00);
                        _tableHeaderFontColor_B = (float)(((Double)Int32.Parse(rd["TableHeaderFontColor_B"].ToString())) / 255.00);

                        (new FileStream(_templateWelcomeLetterPath, FileMode.Open, FileAccess.Read)).CopyTo(_templateWelcomeLetterStream = new MemoryStream());
                        (new FileStream(_templateReportPath, FileMode.Open, FileAccess.Read)).CopyTo(_templateReportStream = new MemoryStream());
                        (new FileStream(_templateCopertinaPath, FileMode.Open, FileAccess.Read)).CopyTo(_templateCopertinaStream = new MemoryStream());
                        (new FileStream(_templateGlossarioPath, FileMode.Open, FileAccess.Read)).CopyTo(_templateGlossarioStream = new MemoryStream());
                    }
                }
                result = true;
            }
            catch(Exception ex)
            {
                result = false;
            }
            return result;
        }


        public System.IO.Stream TemplateReport { get { return _templateReportStream; } }
        public System.IO.Stream TemplateWelcomeLetter { get { return _templateWelcomeLetterStream; } }
        public System.IO.Stream TemplateCopertina { get { return _templateCopertinaStream; } }
        public System.IO.Stream TemplateGlossario { get { return _templateGlossarioStream; } }

        public int MarginRight
        {
            get
            {
                return 259;// (!_periodico && _tipoReport == 2) ? 261 : 259;
            }
        }

        public int GlossarioMarginRight
        {
            get
            {
                return 40;// (!_periodico && _tipoReport == 2) ? 37 : 40;
            }
        }
        public int MarginDown
        {
            get
            {
                return 105;// (!_periodico && _tipoReport == 2) ? 103 : 105;
            }
        }

        public ceTe.DynamicPDF.RgbColor ReportTypeFontColor
        {
            get
            {
                return new ceTe.DynamicPDF.RgbColor(_reportTypeFontColor_R, _reportTypeFontColor_G, _reportTypeFontColor_B);
            }
        }
        public ceTe.DynamicPDF.RgbColor ChapterBackgroundColor
        {
            get
            {
                ceTe.DynamicPDF.RgbColor c;

                //logger.Info(string.Concat("Utils -> CF: ", _dataThread.CodiceFiscale," idReport: ", _dataThread.IdReportPDFDiagnosi," Rete: ",_dataThread.Rete));

                //var id = _dataThread.ReportsType.Find(f => f.Descrizione == "Diagnosi").IdModello;

                if (idReportDiagnosi != 6)
                    c =  new ceTe.DynamicPDF.RgbColor(_chapterBackground_R, _chapterBackground_G, _chapterBackground_B);
                else
                    c = new ceTe.DynamicPDF.RgbColor(34, 128, 0);
                return c;
            }
        }
        public ceTe.DynamicPDF.RgbColor ChapterFontColor
        {
            get
            {
                ceTe.DynamicPDF.RgbColor c;

                if (idReportDiagnosi != 6)
                    c = new ceTe.DynamicPDF.RgbColor(_chapterBackground_R, _chapterBackground_G, _chapterBackground_B);
                else
                    c = new ceTe.DynamicPDF.RgbColor(34, 128, 0);

                return c;

                //return new ceTe.DynamicPDF.RgbColor(_chapterFontColor_R, _chapterFontColor_G, _chapterFontColor_B);
            }
        }
        public ceTe.DynamicPDF.RgbColor TableHeaderFontColor
        {
            get
            {
                return new ceTe.DynamicPDF.RgbColor(_tableHeaderFontColor_R, _tableHeaderFontColor_G, _tableHeaderFontColor_B);
            }
        }
    }
}