using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
using Telerik.Windows.Documents.Spreadsheet.Model;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf;
using Telerik.Windows.Documents.Common.FormatProviders;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls;
using System.Xml.Linq;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsm;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders;

public partial class PdfViewer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get the file path from the query string
        string filePath = Request.QueryString["file"];
        if (!string.IsNullOrEmpty(filePath))
        {
            // Security checks should be implemented here
            string fullPath = Server.MapPath(filePath);
            string extension = Path.GetExtension(fullPath).ToLower();

            // Prepare a byte array to hold the rendered PDF bytes
            byte[] renderedBytes = null;
            
            if (extension == ".pdf")
            {
                RadPdfViewer1.PdfjsProcessingSettings.File = filePath; // Set the path for the PDF viewer to display the PDF
            }
            // RadFlow Documents (.docx, .rtf, .html, .txt)
            else if (Regex.IsMatch(extension, ".docx|.rtf|.html|.txt"))
            {
                IFormatProvider<RadFlowDocument> provider = null;
                RadFlowDocument document = null;

                // Determine the provider based on the file extension
                switch (extension)
                {
                    case ".docx": provider = new DocxFormatProvider(); break;
                    // Add other cases for .rtf, .html, .txt as needed
                    default: provider = null; break;
                }

                // Import the document
                using (Stream inputStream = File.OpenRead(fullPath))
                {
                    document = provider.Import(inputStream);
                }

                // Convert to PDF
                Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new
              Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
                using (MemoryStream ms = new MemoryStream())
                {
                    pdfProvider.Export(document, ms);
                    renderedBytes = ms.ToArray();
                }
            }
            // Workbook Documents (.xlsx, .csv, .xls, .xlsm)
            else if (Regex.IsMatch(extension, ".xlsx|.csv|.xls|.xlsm"))
            {
                Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
                using (Stream input = File.OpenRead(fullPath))
                {
                    IWorkbookFormatProvider provider = null;

                    switch (extension)
                    {
                        case ".xlsx":
                            provider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();
                            break;
                        case ".xls":
                            provider = new XlsFormatProvider();
                            break;
                        case ".xlsm":
                            provider = new XlsmFormatProvider();
                            break;
                        // Add case for .csv if needed
                        default:
                            provider = null;
                            break;
                    }

                    if (provider != null)
                    {
                        workbook = provider.Import(input);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unsupported file format.");
                    }
                }

                // Convert to PDF
                Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();
                using (MemoryStream ms = new MemoryStream())
                {
                    pdfProvider.Export(workbook, ms);
                    renderedBytes = ms.ToArray();
                }
                // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
            }

            // If we have rendered bytes, load them into the RadPdfViewer
            if (renderedBytes != null)
            {
                RadPdfViewer1.PdfjsProcessingSettings.FileSettings.Data = Convert.ToBase64String(renderedBytes);
            }
            else if (extension != ".pdf")
            {
                // Handle cases where the file extension is not supported
                Response.Write("Unsupported file type.");
            }
        }
        else
        {
            // Handle the case where no file was specified
            Response.Write("No file specified.");
        }
    }

}