<?php

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

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

$page = 0;

$insertedGames = [];

while (true) {
    $url = "https://gamomat.com/wp-admin/admin-ajax.php?action=load_more_games&term_id=&range=&search=&paged=$page";

    // Initialize cURL session
    $ch = curl_init($url);
    
    // Set cURL options for the POST request
    $postData = http_build_query([
        'action' => 'load_more_games',
        'term_id' => '',
        'range' => '',
        'search' => '',
        'paged' => $page
    ]);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute cURL session and capture the response
    $html_content = curl_exec($ch);

    // Check for cURL errors
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
        break;
    }

    // Close cURL session
    curl_close($ch);

    // Parse HTML content using Simple HTML DOM
    require_once('simple_html_dom.php');
    $html = str_get_html($html_content);

    $gameDivs = $html->find('.row .col .teaser-content');

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

    foreach ($gameDivs as $div) {
        $link = $div->find('a', 0);
        $link = $link->href;

        $name = $div->find('.teaser-title p', 0);
        $name = $name->plaintext;

        $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);

        if (!empty($link)) {
            // Initialize cURL session for the game detail page
            $ch = curl_init($link);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $html = curl_exec($ch);

            if (curl_errno($ch)) {
                echo 'cURL error: ' . curl_error($ch);
            }

            curl_close($ch);

            $dom = new DOMDocument();
            @$dom->loadHTML($html);
            $xpath = new DOMXPath($dom);
            $iframe = $xpath->query('//div[@class="block-container button-link-container"]')->item(0);
            if ($iframe !== null) {
                $demo_desk = $iframe->getAttribute('data-embed');
            } else {
                $demo_desk = "";
            }
            $demo_mob = $demo_desk;
            $image = $xpath->query('//div[@class="js-media-container block-container height-100"]')->item(0);
            $dir = $image->getAttribute('data-dir');
            $imgs = $image->getAttribute('data-imgs');
            $imgs = json_decode($imgs, true);
            $path = $imgs[4]; 
            $img = "https://gamomat.com".$dir."/".$path;
        } else {
            $demo_desk = "";
            $demo_mob = "";
            $img = "";
        }

        $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++;
    // if($page>1) {
    //         break;
    // }
}

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

?>
