using DevExpress.XtraReports.UI;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MTWorkHR.Infrastructure.Reports
{
    public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension
    {
      //  private readonly GlobalInfo globalInfo;
    //    private readonly AppSettingsConfiguration settings;
        public CustomReportStorageWebExtension()
        {
        }
        public override byte[] GetData(string url)
        {
            try
            {
                // Parse the string with the report name and parameter values.
                string[] parts = url.Split('?');
                string reportName = parts[0];
                string parametersQueryString = parts.Length > 1 ? parts[1] : String.Empty;

                // Create a report instance.

                XtraReport report= getReport(reportName);
              
                if (report != null)
                {
                    // Apply the parameter values to the report.
                    var parameters = HttpUtility.ParseQueryString(parametersQueryString);

                    foreach (string parameterName in parameters.AllKeys)
                    {
                        if (parameters.Get(parameterName) != "null"&& parameters.Get(parameterName) != null)
                        {
                                report.Parameters[parameterName].Value = Convert.ChangeType(
                            parameters.Get(parameterName), report.Parameters[parameterName].Type);
                        }
                    }

                    // Disable the Visible property for all report parameters
                    // to hide the Parameters Panel in the viewer.
                    foreach (var parameter in report.Parameters)
                    {
                        parameter.Visible = false;
                    }

                    // If you do not hide the panel, disable the report's RequestParameters property.
                    // report.RequestParameters = false;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        report.SaveLayoutToXml(ms);
                        return ms.ToArray();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
                    "Could not get report data.", ex);
            }
            throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
                string.Format("Could not find report '{0}'.", url));
        }
        private XtraReport getReport(string reportName)
        {
            switch (reportName)
            {
                case "ContractReport":
                    return new ContractReport();
                case "ContractAllowance":
                    return new ContractAllowances();
                default: return null;
            }
        }
    
    }
}