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

$url = 'https://www.hacksawgaming.com/games';
require_once('simple_html_dom.php');
$html_content = file_get_contents($url);
$html = str_get_html($html_content);

$insertedGames = [];

foreach ($html->find('.GridListItem') as $div) {

        // $link = $div->find('.GridListItemOverlayContent__actions a', 0);
        // $link = $link->href;
        $link = "";

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

        $provider_id = $id;

        $translit = str_replace(" ", "-", $name);
        $translit = str_replace("&#039;", "-", $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);

        $url = $div->find('.GridListItem__thumbnail', 0);
        $url = $url->getAttribute('data-bg-image');
        $parts = explode('/', $url);
        $gameId = end($parts);
        $gameId = str_replace(".jpg", "", $gameId);

        $demo_desk = "https://static-live.hacksawgaming.com/".$gameId."/1.16.0/index.html?language=en&channel=desktop&gameid=".$gameId."&mode=2&token=123131&currency=EUR&partner=demo&env=https://rgs-demo.hacksawgaming.com/api&realmoneyenv=https://rgs-demo.hacksawgaming.com/api";

        $demo_mob = $demo_desk;

        $volatility = 0;

        $rtp = 0.0;

        $rtp_low = 0.0;

        $rtp_high = 0.0;

        $max_win = 0;

        $img = $div->find('.GridListItem__thumbnail', 0);
        $img = $img->getAttribute('data-bg-image');

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

        // require ('insert_values.php');

        require ('inserted_games.php');

}

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