<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
            <Windows>
                <telerik:RadWindow ID="RadWindow1" runat="server" Modal="true" Width="800px" Height="600px" />
            </Windows>
        </telerik:RadWindowManager>

        <script type="text/javascript">
            function OnClientFileOpen(sender, args) {
                var item = args.get_item();
                var filePath = args.get_path();
                var fileExtension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
                var fileName = args.get_item().get_name();
                var viewerUrl = '';

                // Check if the item is a file, but not a directory
                if (!item.isDirectory()) {
                    // Supported file types for PDFViewer
                    var pdfViewerSupportedTypes = ['pdf', 'doc', 'docx', 'xls', 'xlsm'];
                    // Supported file types for RadWindow
                    var radWindowSupportedTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'mp4', 'webm', 'ogg', 'txt', 'sql'];

                    // Supported file types for RadSpreadsheet
                    var spreadsheetSupportedTypes = ['xlsx'];

                    if (spreadsheetSupportedTypes.includes(fileExtension)) {
                        args.set_cancel(true);
                        viewerUrl = "SpreadsheetViewer.aspx?file=" + encodeURIComponent(filePath);
                        openRadWindow(viewerUrl, fileName);
                    }
                    else if (pdfViewerSupportedTypes.includes(fileExtension)) {
                        args.set_cancel(true);
                        viewerUrl = "PdfViewer.aspx?file=" + encodeURIComponent(filePath);
                        openRadWindow(viewerUrl, fileName);
                    } else if (radWindowSupportedTypes.includes(fileExtension)) {
                        args.set_cancel(true);
                        viewerUrl = filePath; // Adjust based on your application's logic
                        openRadWindow(viewerUrl, fileName);
                    } else {
                        args.set_cancel(true); // Cancel the default open event
                        window.location = filePath; // Redirect to the file path to initiate download
                    }
                }
            }

            function openRadWindow(viewerUrl, fileName) {
                var oWnd = window.radopen(viewerUrl, "RadWindow1");
                oWnd.maximize();
                oWnd.set_title(fileName);
                oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Maximize + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move);

                setTimeout(function () {
                    var radWindow = oWnd.get_contentFrame().contentWindow.document;

                    // Disable the context menu in the iframe
                    radWindow.addEventListener('contextmenu', function (event) {
                        event.preventDefault();
                        alert('Right-click context menu is disabled on this page.');
                    }, false);

                    // Disable shortcuts
                    radWindow.addEventListener('keydown', function (event) {
                        if ((event.ctrlKey || event.metaKey) && (event.key === 's' || event.key === 'p' || event.key === 'c')) {
                            event.preventDefault();
                            alert('Saving, printing, and copying are disabled on this page.');
                        }
                    }, false);
                }, 100);
            }

            // Disable the context menu
            document.addEventListener('contextmenu', function (event) {
                event.preventDefault();
                // Optionally, you can inform the user that the right-click functionality is disabled.
                alert('Right-click context menu is disabled on this page.');
            }, false);

            //disable shortcuts
            document.addEventListener('keydown', function (event) {
                if ((event.ctrlKey || event.metaKey) && (event.key === 's' || event.key === 'p' || event.key === 'c')) {
                    event.preventDefault();
                    // Optionally, you can alert the user that this action is disabled.
                    alert('Saving, printing, and copying are disabled on this page.');
                }
            }, false);

        </script>

        <telerik:RadFileExplorer runat="server" ID="RadFileExplorer1" Width="100%" Height="400px" EnableOpenFile="true"
            OnClientFileOpen="OnClientFileOpen">
            <Configuration ViewPaths="~/Files" UploadPaths="~/Files" DeletePaths="~/Files" />
        </telerik:RadFileExplorer>
    </form>
</body>
</html>
