<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>King Stocks Token Value</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #f4f4f4;
        }
        .container {
            text-align: center;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        input {
            margin-top: 10px;
            padding: 10px;
            width: 200px;
        }
        button {
            margin-top: 10px;
            padding: 10px 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>King Stocks Token (KST)</h1>
        <h2 id="tokenValue">Value of 1 KST: <span id="value">0.00</span> €</h2>
        <h3>Percentage of Shares Bought: <span id="percentage">0%</span></h3>
        <input type="text" id="username" placeholder="Enter your username">
        <button onclick="fetchTokens()">Get Token Value</button>
        <h3 id="result"></h3>
    </div>

    <script>
        // Change these values as needed
        let tokenValue = "50"; // Value of a single KST in €
        const totalTokens = 5000; // Total tokens representing 100%
        let totalSharesOwned = 0; // Total shares owned by all users

        // Update the displayed values
        document.getElementById('value').innerText = tokenValue; // Display without rounding

        function fetchTokens() {
            fetch('tokens.txt')
                .then(response => response.text())
                .then(data => {
                    const lines = data.split('\n');
                    let tokensOwned = 0;
                    totalSharesOwned = 0; // Reset total shares owned

                    for (let line of lines) {
                        const [user, amount] = line.split(':');
                        const tokenAmount = parseFloat(amount);
                        if (!isNaN(tokenAmount)) {
                            totalSharesOwned += tokenAmount; // Sum all tokens
                        }
                        if (user.trim() === document.getElementById('username').value.trim()) {
                            tokensOwned = tokenAmount; // Get tokens for the specific user
                        }
                    }

                    // Update the percentage of shares bought based on total shares owned
                    const ownershipPercentage = (totalSharesOwned / totalTokens) * 100;

                    // Update the displayed results for the user
                    const totalValue = tokensOwned * parseFloat(tokenValue);
                    const userOwnershipPercentage = (tokensOwned / totalTokens) * 100; // Calculate user's ownership percentage

                    if (tokensOwned > 0) {
                        document.getElementById('result').innerText = 
                            `They own ${tokensOwned} tokens, worth ${totalValue.toString()} €. They own ${userOwnershipPercentage.toFixed(2)}% of King Stocks' value.`;
                    } else {
                        document.getElementById('result').innerText = 'Username not found or no tokens owned.';
                    }

                    // Update the percentage of shares bought
                    document.getElementById('percentage').innerText = ownershipPercentage.toFixed(2) + '%';
                })
                .catch(error => {
                    console.error('Error fetching token data:', error);
                    document.getElementById('result').innerText = 'Error fetching token data.';
                });
        }

        // Function to set a new token value with unlimited decimals
        function setTokenValue(newValue) {
            tokenValue = newValue.toString(); // Store as string to allow unlimited decimals
            document.getElementById('value').innerText = tokenValue; // Update display
        }

        // Fetch tokens on page load to display the total shares percentage
        fetchTokens(); // Call this function to initialize the display
    </script>
    </body>
</html>