<?php

$slug = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == '/') {
    $slug = $setting['homeGame'];
} else {
    $slug = str_replace('/', '', $slug);
}

$popgames = [];

$linkGame = $setting['daftarlink'];
$navigations = $setting['navigations'];
if ($navigations) {
    $navs = json_decode($navigations, true);
    $ptitle = '';
    $url = '';
    $topgame = '';
    $popgame = '';
    $linking = '';
    $randNumn = '';
    $hidegame = '';
    $secGameTitle = '';
    $altlink = '';
    foreach ($navs as $nav) {
        if ($nav['navSlug'] == $slug) {
            $ptitle = $nav['navTitle'];
            $url = $nav['gamelink'];
            $altlink = $nav['altgamelink'];
            $topgame = $nav['topgame'];
            $popgame = $nav['popgame'];
            $linking = $nav['linking'];
            $randNumb = $nav['randNumb'];
            $hidegame = explode(',', str_replace(' ', '', $nav['hidegame']));
            $secGameTitle = $nav['pagetitle'];
        }
    }
}
if ($topgame)
    $gamecarousels = explode(',', $topgame);
if ($popgame)
    $popgames = explode(',', $popgame);
if ($setting['popgameVal'])
    $popgameVal = $setting['popgameVal'];

if ($url) {

    if ($linking == 'yes')
        $linkGame = $url;

    $grablink = $url;
    if ($altlink) $grablink = $altlink;

    require 'vendor/autoload.php';
    $httpClient = new \GuzzleHttp\Client();
    $response = $httpClient->get($grablink);
    $htmlString = (string) $response->getBody();
    //add this line to suppress any warnings
    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTML($htmlString);
    $xpath = new DOMXPath($doc);

    $titles =  $xpath->query('//li[contains(@class, "game-item__wrapper")]//a//div[contains(@class, "game-item")]//img[@class="game-item__img"]/@alt');
    $images = $xpath->query('//li[contains(@class, "game-item__wrapper")]//a//div[contains(@class, "game-item")]//img[@class="game-item__img"]/@src');

    if ($titles) : ?>
        <div class="gameSearch text-center">
            <h3><?php echo $secGameTitle; ?></h3>
            <div class="input-group mt-3 mb-5">
                <span class="input-group-text"><i class="fas fa-search"></i></span>
                <input class="form-control" id="dataGame" placeholder="Cari Game...">
            </div>
        </div>
        <div class="all-games anim-change">
            <div class="row px-3">
                <?php
                foreach ($titles as $key => $title) :
                    $gameTitle = $title->textContent;
                    $gameImg = $images[$key]->textContent; 
                    $hidden = '';
                    if (in_array($key, $hidegame)) $hidden = 'style="display:none"'; ?>
                    <div class="col-md-3 col-6" <?php echo $hidden; ?>>
                        <div class="single-game-wrapper">
                            <div class="single-game">
                                <a href="<?php echo $linkGame; ?>" rel="nofollow">
                                    <div class="row g-0">
                                        <div class="col-12">
                                            <div class="game-img">
                                                <img src="<?php echo $gameImg; ?>" alt="<?php echo $gameTitle; ?>">
                                            </div>
                                        </div>
                                    </div>
                                </a>
                                <div class="col-12">
                                    <div class="game-content">
                                        <h3>
                                            <a href="<?php echo $linkGame; ?>" rel="nofollow" title="<?php echo $gameTitle; ?>">
                                                <?php
                                                if (strlen($gameTitle) > 17)
                                                    $gameTitle = substr($gameTitle, 0, 15) . '...';
                                                echo $gameTitle;
                                                ?>
                                            </a>
                                        </h3>
                                        <div class="play-button w-100 mb-3">
                                            <button class="btn w-100" data-bs-toggle="modal" data-bs-target="#polaGacorModal" data-pola="<?php echo $key; ?>">Pola Gacor</button>
                                        </div>
                                        <div class="percent">
                                            <a href="<?php echo $linkGame; ?>" rel="nofollow">
                                                <p class="percent-txt-<?php echo $key; ?>"></p>
                                                <div id="percent-bar-<?php echo $key; ?>" class="percent-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="" style="width: 0"></div>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <script>
            var linking = '<?php echo $linking; ?>'
            var popgameVal = <?php echo $popgameVal; ?>;
            var randNumb = <?php echo json_encode($randNumb); ?>;
            var x = []
            for (n = 0; n < randNumb.length; n++) {
                x.push(parseInt(randNumb[n]))
            }
            var popgames = <?php echo json_encode($popgames); ?>;
            var popGames = []
            for (c = 0; c < popgames.length; c++) {
                popGames.push(parseInt(popgames[c]))
            }
        </script>
        <?php if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == '/') : ?>
            <?php if ($setting['artikelhome']) : ?>
                <div class="home-artikel px-3 mb-3">
                    <?php echo $setting['artikelhome']; ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>
<?php
    endif;
} else {
    include('404.php');
}
