<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>USB Validator</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            flex-direction: column;
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 20px;
            background-color: #f4f4f4;
            overflow: auto;
        }
        h1 {
            font-size: 2em;
            margin-bottom: 20px;
        }
        p {
            font-size: 1.2em;
            margin: 10px 0;
        }
        ol {
            font-size: 1.2em;
            margin: 20px 0;
            text-align: left;
            display: inline-block;
        }
        strong {
            font-weight: bold;
        }
        button {
            font-size: 1.5em;
            padding: 15px 30px;
            cursor: pointer;
            margin-top: 20px;
        }
        input[type="file"] {
            font-size: 1.2em;
            margin-top: 20px;
        }
    </style>
</head>
<body>

    <br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br>

    <h1>USB Drive Validator</h1>

    <p><strong>Step-by-Step Instructions:</strong></p>
    <ol>
        <li>Insert the USB drive that you want to validate into your computer.</li>
        <li>Ensure the file <strong>USBValidator.exe</strong> is in the same folder as this HTML file (if not, please move the file).</li>
        <li>Click on the "Step1: choose the correct USBValidator.exe file" button below to choose the USBValidator.exe file.</li>
        <li>Once selected, click on "Choose USB drive to save the file" to copy the file to your USB drive.</li>
        <li>The USB drive is now validated. Do not remove the USB drive from your computer until the process is complete.</li>
    </ol>

    <p><strong>Important Notes:</strong></p>
    <p>1. Do not share this file with anyone. It is only for your personal use and verification.</p>
    <p>2. Do not modify this file in any way, as it could invalidate its purpose and authenticity.</p>
    <p>3. If you encounter any issues, please contact your manager or send an email to CyberFulfillment@toronto.ca</p>

    <!-- File input and Save Validator File button -->
    <button id="chooseFileButton" onclick="chooseFile()">Step1: Choose the correct USBValidator.exe file</button>
    <button id="chooseDriveButton" onclick="copyValidatorFile()" disabled>Choose USB drive to save the file</button>

    <script>
        let selectedFile = null;

        // Function to allow user to choose the file
        function chooseFile() {
            const fileInput = document.createElement('input');
            fileInput.type = 'file';
            fileInput.accept = '.exe';
            fileInput.onchange = (event) => {
                if (event.target.files.length > 0) {
                    selectedFile = event.target.files[0];

                    // Enable the next button after file is selected
                    document.getElementById('chooseDriveButton').disabled = false;
                    alert('USBValidator.exe selected successfully!');
                }
            };
            fileInput.click();
        }

        // Function to copy the file to USB drive
        async function copyValidatorFile() {
            if (!selectedFile) {
                alert("Please choose the correct file first.");
                return;
            }

            try {
                // Open the file picker dialog to select USB drive location
                if ('showDirectoryPicker' in window) {
                    // The File System Access API is supported by the browser
                    const directoryHandle = await window.showDirectoryPicker();

                    // Copy the file to the selected USB drive location
                    const newFileHandle = await directoryHandle.getFileHandle(selectedFile.name, { create: true });
                    const writable = await newFileHandle.createWritable();
                    await writable.write(selectedFile);
                    await writable.close();

                    alert('USBValidator.exe successfully copied to your USB drive!');
                } else {
                    alert('Your browser does not support the File System Access API. Please use a supported browser like Chrome or Edge.');
                }
            } catch (error) {
                console.error('Error while copying the file:', error);
                alert('An error occurred while copying the file: ' + error.message);
            }
        }
    </script>
</body>
</html>