<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>License Generator - Khanfar Systems</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-top: 20px;
        }
        #loginSection, #generatorSection {
            text-align: center;
        }
        #generatorSection {
            display: none;
        }
        .input-group {
            margin: 15px 0;
            text-align: left;
        }
        .input-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }
        input {
            width: calc(100% - 120px);
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            margin-right: 10px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .action-button {
            width: 100px;
        }
        h2 {
            color: #333;
            margin-bottom: 20px;
        }
        .error {
            color: #ff0000;
            margin: 10px 0;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="loginSection">
            <h2>License Generator - Khanfar Systems Login</h2>
            <div class="input-group">
                <input type="password" id="password" placeholder="Enter password">
                <button onclick="checkPassword()">Login</button>
            </div>
            <div id="loginError" class="error">Incorrect password!</div>
        </div>

        <div id="afterLogin" style="display: none;">
            <h2>Welcome!</h2>
            <div class="button-container">
                <button onclick="window.location.href='lg2.html'" class="red">Khanfar Push Tool</button>
            </div>
        </div>

        <div id="generatorSection">
            <h2>Khanfar Systems - License Generator</h2>
            <div class="input-group">
                <label>Step 1: Enter Customer's Hardware ID</label>
                <input type="text" id="hardwareId" placeholder="Enter Hardware ID">
                <button class="action-button" onclick="pasteHardwareId()">Paste</button>
            </div>
            <div id="hwError" class="error">Please enter a valid 32-character hardware ID</div>
            
            <div class="input-group">
                <label>Step 2: Generated Activation Code</label>
                <input type="text" id="activationCode" readonly placeholder="Generated activation code will appear here">
                <button class="action-button" onclick="copyActivationCode()">Copy</button>
            </div>
            
            <button onclick="generateActivation()">Generate Activation Code</button>
        </div>
    </div>

    <script>
        const CORRECT_PASSWORD = "8080";
        const LICENSE_KEY = "KhanfarSystems2024";  // Matches Python version
        let attempts = 0;
        const MAX_ATTEMPTS = 10; // Changed from 6 to 10 attempts

        function checkPassword() {
            const password = document.getElementById('password').value;
            const loginError = document.getElementById('loginError');
            attempts++;
            
            if (password === CORRECT_PASSWORD) {
                document.getElementById('loginSection').style.display = 'none';
                document.getElementById('afterLogin').style.display = 'block';
                document.getElementById('generatorSection').style.display = 'block';
                loginError.style.display = 'none';
            } else {
                if (attempts >= MAX_ATTEMPTS) {
                    loginError.textContent = 'Maximum attempts reached. Please try again later.';
                    document.getElementById('password').disabled = true;
                } else {
                    loginError.textContent = `Incorrect password! ${MAX_ATTEMPTS - attempts} attempts remaining.`;
                }
                loginError.style.display = 'block';
            }
        }

        function generateActivationCode(hardwareId) {
            const combined = `${hardwareId}:${LICENSE_KEY}`;
            return CryptoJS.SHA256(combined).toString().substring(0, 32);
        }

        function generateActivation() {
            const hardwareId = document.getElementById('hardwareId').value.trim();
            const hwError = document.getElementById('hwError');
            const activationField = document.getElementById('activationCode');
            
            if (!hardwareId || hardwareId.length !== 32) {
                hwError.style.display = 'block';
                activationField.value = '';
                return;
            }
            
            hwError.style.display = 'none';
            const activationCode = generateActivationCode(hardwareId);
            activationField.value = activationCode;
        }

        function pasteHardwareId() {
            navigator.clipboard.readText()
                .then(text => {
                    document.getElementById('hardwareId').value = text;
                })
                .catch(err => {
                    console.error('Failed to read clipboard:', err);
                });
        }

        function copyActivationCode() {
            const activationCode = document.getElementById('activationCode').value;
            if (activationCode) {
                navigator.clipboard.writeText(activationCode)
                    .catch(err => {
                        console.error('Failed to copy:', err);
                    });
            }
        }

        // Support for Enter key
        document.getElementById('password').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                checkPassword();
            }
        });

        document.getElementById('hardwareId').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                generateActivation();
            }
        });
    </script>
</body>
</html>
