using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Configuration;
using SteamWare;
using GMW_data;

/// <summary>
/// Classe che restituisce uno stream ed i dati accessori per reinviare un report in pdf
/// </summary>
public class reportExporter
{
    /// <summary>
    /// mimeType del report
    /// </summary>
    public string mimeType;
    /// <summary>
    /// encoding dei dati
    /// </summary>
    public string encoding;
    /// <summary>
    /// nome estensione
    /// </summary>
    public string fileNameExtension;
    /// <summary>
    /// steam di bytes del report
    /// </summary>
    public byte[] renderedBytes;
    /// <summary>
    /// metodo protected
    /// </summary>
    public reportExporter()
    {
    }
    /// <summary>
    /// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
    /// </summary>
    /// <param name="tipoReport">tipo di report richiesto</param>
    /// <param name="UDC">cod UDC</param>
    /// <returns>tabella dati</returns>
    private DataTable caricaDati(reportRichiesto tipoReport, string UDC)
    {
        DataTable tab = new DataTable();
        switch (tipoReport)
        {
            //case reportRichiesto.CartellinoFusi:
            //    tab = (DataTable)GMW_data.DataProxy.obj.taCartellini.getByUdc(UDC);
            //    break;
            case reportRichiesto.CartellinoTara:
                tab = (DataTable)GMW_data.DataProxy.obj.taCartellini.stp_getByUdc(UDC);
                break;
            default:
                break;
        }
        return tab;
    }
    /// <summary>
    /// renderizza il report in pdf
    /// </summary>
    /// <param name="tipoReport">report ammessi: RichiestaIntervento</param>
    /// <param name="UDC">cod UDC</param>
    /// <param name="deviceInfoParam">parametri "device input"</param>
    /// <returns></returns>
    public void popolaPdfStream(reportRichiesto tipoReport, string UDC, devInfoParam deviceInfoParam)
    {
        LocalReport report = new LocalReport();
        string deviceInfo = "";
        ReportParameter[] _UDC = { new ReportParameter("UDC", UDC) };
        switch (tipoReport)
        {
            case reportRichiesto.CartellinoFusi:
                report.ReportPath = @".\Reports\CartellinoCompleto.rdlc";
                report.DataSources.Add(new ReportDataSource(memLayer.ML.confReadString("ReportDatasource"), caricaDati(tipoReport, UDC)));
                deviceInfo = deviceInfoParam.xmlParam;
                break;
            case reportRichiesto.CartellinoTara:
                report.ReportPath = @".\Reports\CartellinoTara.rdlc";
                report.DataSources.Add(new ReportDataSource(memLayer.ML.confReadString("ReportDatasource"), caricaDati(tipoReport, UDC)));
                deviceInfo = deviceInfoParam.xmlParam;
                break;
            default:
                break;
        }
        createPdfStream(report, deviceInfo);
    }

    /// <summary>
    /// crea il vero e proprio stream pdf
    /// </summary>
    /// <param name="report"></param>
    /// <param name="deviceInfo"></param>
    private void createPdfStream(LocalReport report, string deviceInfo)
    {
        // export in PDF
        string reportType = "PDF";
        Warning[] warnings;
        string[] streams;
        renderedBytes = report.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);
    }

}

