<?php

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

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

$url = 'https://wazdan.com/gamesapi';
$data = file_get_contents($url);
$games = json_decode($data, true)['data'];

$insertedGames = [];

foreach ($games as $game) {

    if ($game['type'] === 'game') {

        $link = $game['src'];

        $name = str_replace("™", "", $game['name']);

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

        $gamePage = file_get_contents($link);
        preg_match('/<a[^>]*href="([^"]*game_id=(\d+)[^"]*)"/', $gamePage, $match); 
        $gameId = isset($match[2]) ? $match[2] : null; 

        if ($gameId=="null"){$demo_desk="null";}
        else{$demo_desk = "https://gamelaunch.wazdan.com/demo-demo/gamelauncher?license=wazdancom&mode=demo&game=".$gameId."&platform=desktop";}

        if ($gameId=="null"){$demo_mob="null";}
        else{$demo_mob = "https://gamelaunch.wazdan.com/demo-demo/gamelauncher?license=wazdancom&mode=demo&game=".$gameId."&platform=mobile";}

        $volatility_map = [
            'Low' => 1,
            'Low-Medium' => 2,
            'Medium' => 3,
            'Standard' => 3,
            'Medium-High' => 4,
            'High' => 5
        ];
        $gamePage = file_get_contents($link);
        preg_match('/<p class="info">Volatility: <b>([^<]*)<\/b><\/p>/', $gamePage, $match);
        $volatility = isset($match[1]) ? $match[1] : null;
        if (array_key_exists($volatility, $volatility_map)) {
            $volatility = $volatility_map[$volatility];
        } else {
            $volatility = 0;
        }

        $gamePage = file_get_contents($link);
        preg_match('/<p class="info">RTP: <b>(\d+\.\d+)<\/b>%<\/p>/', $gamePage, $match);
        $rtp = isset($match[1]) ? $match[1] : 0.0;

        $rtp_low = 0.0;

        $rtp_high = 0.0;

        $gamePage = file_get_contents($link);
        preg_match('/<p class="info">Max Win: <b>(\d+(?:\.\d+)?)x bet<\/b><\/p>/', $gamePage, $match);
        $max_win = isset($match[1]) ? $match[1] : 0.0;

        $img = $game['img'];

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

        require ('insert_values.php');

        require ('inserted_games.php');
    }
}

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