<?php

// Connect to the database
require_once('pdo.php');
require_once('get_provider_translit.php');
require ('create_images.php');

$id = 51; 
$provider_translit = getProviderTranslit($id);

$page = 1;

$insertedGames = [];

while (true) {
    $url = "https://www.videoslots.com/phive/modules/BoxHandler/html/ajaxActions.php?operator=Playtech&func=printGameList&lang=en&boxid=725&action=GetBoxHtml&juri=GB&logged=false&page=$page";
    require_once('simple_html_dom.php');
    $html_content = file_get_contents($url);
    $html = str_get_html($html_content);

    $gameDivs = $html->find('.game-row .game');

    if (empty($gameDivs)) {
        break;
    }

        foreach ($gameDivs as $div) {

        $isLive = $div->find('.img-wrapper img', 1);

        if ($isLive) { // Check if the element exists
            $altAttribute = $isLive->getAttribute('alt');
            
            if ($altAttribute == "Live Casino") {
                // Skip the element
                continue;
            }
        }

        $link = "";

        $name = $div->find('.game-image', 0);
        $name = $name->getAttribute('alt');
        if($name == 'Fire Blaze Blue Wizard'){continue;}

        $provider_id = $id;

        $translit = str_replace(" ", "-", $name);
        $translit = str_replace("&#039;", "-", $translit);
        $translit = str_replace("&#038;", "-", $translit);
        $translit = str_replace("&#8211;", "-", $translit);
        $translit = str_replace("&#8217;", "-", $translit);
        $translit = preg_replace("/[^a-zA-Z0-9\-]/", "", $translit);
        $translit = str_replace("---", "-", $translit);
        $translit = str_replace("--", "-", $translit);

        $img = $div->find('.game-image', 0);
        $img = $img->getAttribute('src');

        $parts = explode('/', $img);
        $gameId = end($parts);
        $gameId = str_replace("_c.jpg", "", $gameId);
        $gameId = str_replace("playtech_", "", $gameId);

        if (strpos($gameId, 'gpas') !== false) {
            $demo_desk = "https://gamelauncher-uu-pop-stg.playtechone.com/launcher?game=$gameId&casino=playtechhorizon&clientplatform=web&clienttype=casino&real=0&currency=GBP&language=en";
        } else {
            continue;
        }

        $demo_mob = $demo_desk;

        $volatility = 0;

        $rtp = 0.0;

        $rtp_low = 0.0;

        $rtp_high = 0.0;

        $max_win = 0;

        createImage($provider_translit, $translit, $img);

        require ('insert_values.php');

        require ('inserted_games.php');

        }

        $page++;
}

// echo json_encode(['insertedGames' => $insertedGames]);

?>