using Microsoft.Extensions.Configuration;
using Telerik.Reporting;
using Telerik.Reporting.Processing;

namespace BMA.EHR.Report.Service
{
    public class GenericReportGenerator
    {
        #region " Fields "

        private readonly IConfiguration _configuration;

        #endregion

        #region " Constructor and Destructor "

        public GenericReportGenerator(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        #endregion

        #region " Methods "

        public virtual byte[] GenerateReport(string fileName, string exportType)
        {
            try
            {
                //var rptFile = Path.Combine("Reports", fileName);

                ReportPackager reportPackager = new ReportPackager();
                Telerik.Reporting.Report? report = null;
                using (var sourceStream = System.IO.File.OpenRead(fileName))
                {
                    report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
                }

                //report.DataSource = command;

                System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();

                InstanceReportSource instanceReportSource = new InstanceReportSource()
                {
                    ReportDocument = report
                };


                ReportProcessor reportProcessor = new ReportProcessor(_configuration);
                RenderingResult result = reportProcessor.RenderReport(exportType, instanceReportSource, deviceInfo);

                var content = result.DocumentBytes;

                return content;
            }
            catch
            {
                throw;
            }
        }

        #endregion
    }
}