<?php

function fetchRobloxAvatarHeadshot($userId, $size = "352x352", $isCircular = false, $savePath = null) {
    // Construct the API URL
    $url = "https://thumbnails.roblox.com/v1/users/avatar-headshot?userIds={$userId}&size={$size}&format=Png&isCircular=" . ($isCircular ? "true" : "false");
    
    // Initialize cURL session
    $ch = curl_init($url);
    
    // Set cURL options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json' // Accept JSON response
    ]);
    
    // Execute the request
    $response = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
        curl_close($ch);
        return null;
    }
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode === 200) {
        // Decode JSON response
        $data = json_decode($response, true);
        
        if (isset($data['data'][0]['imageUrl'])) {
            $imageUrl = $data['data'][0]['imageUrl'];
            
            // Optionally save the image locally
            if ($savePath) {
                $imageContent = file_get_contents($imageUrl);
                if ($imageContent !== false) {
                    file_put_contents($savePath, $imageContent);
                    echo "Image saved as $savePath\n";
                } else {
                    echo "Failed to download image content.\n";
                }
            }
            
            return $imageUrl;
        } else {
            echo "Image URL not found in the response.\n";
            return null;
        }
    } else {
        echo "Failed to fetch image. HTTP Status Code: $httpCode\n";
        return null;
    }
}


if(isset($_GET['user'])) {
    $user = $_GET['user'];
    // Base URL

    $url = "https://users.roblox.com/v1/usernames/users";

    $image = "";

    // Data to send
    $data = [
        "usernames" => [$user],
        "excludeBannedUsers" => true
    ];
    
    // Initialize cURL session
    $ch = curl_init($url);
    
    // Set cURL options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
    curl_setopt($ch, CURLOPT_POST, true); // Specify a POST request
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json', // Set content type
        'Accept: application/json'
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Attach JSON-encoded data
    
    // Execute the request
    $response = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    } else {
        // Decode and print the response
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $response = json_decode($response)->data[0];
        $final_userid = $response->id;

        $image = fetchRobloxAvatarHeadshot($final_userid);



    }
    
    // Close the cURL session
    curl_close($ch);

    


}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="http://localhost/credible/css/c.css">
    <link rel="icon" type="text/png" href="http://localhost/credible/cdn/img/icon.png">
    <title><?php print($response->displayName) ?> Reviews - Credible</title>
</head>
<body>
    <div class="nav">
        <img src="http://localhost/credible/cdn/img/logo.png"></img>
    </div>
    <div id="scroller"></div>
    <div class="banner light">
        <div id="user-information">
            <div class="user">
                <div class="user-image">
                    <img src="<?php print($image); ?>" id="headshot"></img>
                </div>
                <div class="user-name">
                    <h1 id="displayname"><?php print($response->displayName) ?></h1>
                    <h2 id="username">@<?php print($response->name) ?></h2>
                </div>
            </div>
            <div id="user-categories">
                <span>Web development</span>
                <span>UI Design</span>
            </div>
            <div id="overview">
                <div class="information"><h1>Rating</h1><h2>⭐3.8</h2></div>
                <div class="information"><h1>Reviews</h1><h2>152</h2></div>
            </div>
            <div id="buttons">
                <a href="" id="writeareview">Write a review</a>
                <a href="" id="report">Report</a>
            </div>
        </div>
    </div>
    <div class="content">
        <div class="section">
            <h1 class="section-title">About <?php print($response->displayName);?> <font style="float:right;color:#ababab; font-size:12px; font-weight:500; position:relative; top:2.5px; margin-left:5px;">Last updated 32 days ago by Harrison</font></h1>
            <p>Harrison is a seasoned Roblox developer with a unique twist—they're primarily a web developer who has seamlessly brought their skills from the web to the world of virtual experiences. Known for their minimalist yet highly functional designs, they specialize in integrating web technologies to enhance gameplay, build management, and community engagement on the Roblox platform.</p>
            <p>From an early age, I was fascinated with the internet, building websites and applications that showcased their knack for clean code and efficient user interfaces. They stumbled upon Roblox during their university years while working on a web project about virtual economies. Intrigued by the platform’s potential, they started experimenting, creating tools and experiences that combined their love for web development with game design.</p>
            <p>With a foot firmly planted in both the web and Roblox ecosystems, Harrison is a trailblazer who proves that the skills of a web developer can transform and elevate the possibilities of immersive virtual worlds.</p>
        </div>
        <div class="section">
            <h1 class="section-title">Featured Reviews</h1>
            <div class="featured-reviews">
                <div class="review">
                    <div class="review-user">
                        <div class="user-image"><img src="https://tr.rbxcdn.com/30DAY-AvatarHeadshot-BC6651B64A78B6C6DB4DF02EE94B313B-Png/352/352/AvatarHeadshot/Png/noFilter"></img></div>
                        <div class="user-name">
                            <h1 id="displayname">Genya</h1>
                            <h2 id="username">@ScriptOn</h2>
                        </div>
                    </div>
                    <div class="stars">
                        ⭐⭐⭐⭐
                    </div>
                    <p>This guy is pretty good, makes good stuff in relatively good timing. He would answer my messages quickly and was down to solve any problem. My game would not be the...</p>
                    <a href="">Read more</a>
                </div>
                <div class="review">
                    <div class="review-user">
                        <div class="user-image"><img src="https://tr.rbxcdn.com/30DAY-AvatarHeadshot-BC6651B64A78B6C6DB4DF02EE94B313B-Png/352/352/AvatarHeadshot/Png/noFilter"></img></div>
                        <div class="user-name">
                            <h1 id="displayname">Genya</h1>
                            <h2 id="username">@ScriptOn</h2>
                        </div>
                    </div>
                    <div class="stars">
                        ⭐⭐⭐⭐
                    </div>
                    <p>This guy is pretty good, makes good stuff in relatively good timing. He would answer my messages quickly and was down to solve any problem. My game would not be the...</p>
                    <a href="">Read more</a>
                </div>
                <div class="review">
                    <div class="review-user">
                        <div class="user-image"><img src="https://tr.rbxcdn.com/30DAY-AvatarHeadshot-BC6651B64A78B6C6DB4DF02EE94B313B-Png/352/352/AvatarHeadshot/Png/noFilter"></img></div>
                        <div class="user-name">
                            <h1 id="displayname">Genya</h1>
                            <h2 id="username">@ScriptOn</h2>
                        </div>
                    </div>
                    <div class="stars">
                        ⭐⭐⭐⭐
                    </div>
                    <p>This guy is pretty good, makes good stuff in relatively good timing. He would answer my messages quickly and was down to solve any problem. My game would not be the...</p>
                    <a href="">Read more</a>
                </div>
            </div>
        </div>
        <hr>
        <div class="section">
            <h1 class="section-title">Projects</h1>
            <div class="projects">
                <div class="project">
                    <div class="project-image"><img src=""></img></div>
                    <div class="project-details">
                        <h1 id="project-name">Project 1 2024</h1>
                        <p id="project-description">This is a short description of the project I worked on. It was a good project where I did ______ and was part of doing _____. All of the _ Design was done by me, except for the scripting. Also I made the thumbnails, in blender and photoshop</p>
                    </div>
                </div>
                <div class="project">
                    <div class="project-image"><img src=""></img></div>
                    <div class="project-details">
                        <h1 id="project-name">Project 2 2024</h1>
                        <p id="project-description">This is a short description of the project I worked on. It was a good project where I did ______ and was part of doing _____. All of the _ Design was done by me, except for the scripting. Also I made the thumbnails, in blender and photoshop</p>
                    </div>
                </div>
                <div class="project">
                    <div class="project-image"><img src=""></img></div>
                    <div class="project-details">
                        <h1 id="project-name">Project 3 2024</h1>
                        <p id="project-description">This is a short description of the project I worked on. It was a good project where I did ______ and was part of doing _____. All of the _ Design was done by me, except for the scripting. Also I made the thumbnails, in blender and photoshop</p>
                    </div>
                </div>
                <div class="project">
                    <div class="project-image"><img src=""></img></div>
                    <div class="project-details">
                        <h1 id="project-name">Project 4 2024</h1>
                        <p id="project-description">This is a short description of the project I worked on. It was a good project where I did ______ and was part of doing _____. All of the _ Design was done by me, except for the scripting. Also I made the thumbnails, in blender and photoshop</p>
                    </div>
                </div>
                <div class="project">
                    <div class="project-image"><img src=""></img></div>
                    <div class="project-details">
                        <h1 id="project-name">Project 5 2024</h1>
                        <p id="project-description">This is a short description of the project I worked on. It was a good project where I did ______ and was part of doing _____. All of the _ Design was done by me, except for the scripting. Also I made the thumbnails, in blender and photoshop</p>
                    </div>
                </div>

            </div>
        </div>
        <hr>
        <div class="section reviews">
            <h1 class="section-title">Reviews</h1>
            <div class="reviews-top">
            </div>
            <div class="reviews" --data-page="1">
                <div class="review">
                    
                </div>
            </div>
        </div>
    </div>
    
</body>
</html>

<script>
    function updateDivWidth() {
      // Get the scroll position as a percentage
      const scrollPercentage = Math.min(window.scrollY / (document.documentElement.scrollHeight - window.innerHeight), 1);
      
      // Set the width of the div based on scroll percentage (0% at 0% scroll, 100% at 100% scroll)
      const newWidth = scrollPercentage * 100; // 0% at 0% scroll, 100% at 100% scroll

      // Update the div width
      document.getElementById('scroller').style.width = newWidth + '%';
    }

    // Attach the scroll event to update width on scroll
    window.addEventListener('scroll', updateDivWidth);

    // Initialize the width when the page loads
    updateDivWidth();
  </script>