<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class RtpController extends Controller
{
    public function index() {
        $brands = env('BRANDS');
        $url = env('APP_URL');
        // Mengambil data dari API
        $getProvidersAvailable = file_get_contents($url.'/api/GetProvider/'.$brands);
        $provider = json_decode($getProvidersAvailable);

        $getWebsiteAvailable = file_get_contents($url.'/api/GetWebsite/'.$brands);
        $website = json_decode($getWebsiteAvailable);

        $getGamesAvailable = file_get_contents($url.'/api/GetGames/'.$brands.'/PRAGMATIC');
        $gamesNoProvider = json_decode($getGamesAvailable);

        $gamesWithProvider = [];

        foreach ($gamesNoProvider as $game) {
            foreach ($provider as $item) {
                if ($game->provider_id == $item->id) {
                    $game->provider = $item;
                    break; // Break out of the loop once the matching provider is found
                }
            }
            $games[] = $game;
        }
        // Mendeklarasikan variabel untuk dikirim ke tampilan
        return view('index', compact('website', 'provider', 'games'));
        // return view('index', compact('website', 'provider'));
    }

    public function gamesProvider($provider) {
        try {
            $providerGames = urldecode($provider);
            $brands = env('BRANDS');

            $url = env('APP_URL');
        
            // Mengambil data dari API
            $getProvidersAvailable = file_get_contents($url.'/api/GetProvider/'.$brands);
            $provider = json_decode($getProvidersAvailable);
        
            $getWebsiteAvailable = file_get_contents($url.'/api/GetWebsite/'.$brands);
            $website = json_decode($getWebsiteAvailable);
        
            $getGamesAvailable = file_get_contents($url.'/api/GetGames/'.$brands.'/'.$providerGames);
            $gamesNoProvider = json_decode($getGamesAvailable);

            $gamesWithProvider = [];

            foreach ($gamesNoProvider as $game) {
                foreach ($provider as $item) {
                    if ($game->provider_id == $item->id) {
                        $game->provider = $item;
                        break; // Break out of the loop once the matching provider is found
                    }
                }
                $games[] = $game;
            }
        
            return view('index', compact('website', 'provider', 'games'));
        } catch (\Exception $e) {
            // Jika terjadi kesalahan, alihkan ke rute index
            return Redirect::route('index')->withErrors(['error' => 'Terjadi kesalahan dalam mengambil data.']);
        }
    }

    public function getGames(){
        $brands = env('BRANDS');
        $url = env('APP_URL');
        // Mengambil data dari API
        $getProvidersAvailable = file_get_contents($url.'/api/GetProvider/'.$brands);
        $provider = json_decode($getProvidersAvailable);

        $getWebsiteAvailable = file_get_contents($url.'/api/GetWebsite/'.$brands);
        $website = json_decode($getWebsiteAvailable);

        $getGamesAvailable = file_get_contents($url.'/api/GetGames/'.$brands.'/PRAGMATIC');
        $gamesNoProvider = json_decode($getGamesAvailable);

        $gamesWithProvider = [];

        foreach ($gamesNoProvider as $game) {
            foreach ($provider as $item) {
                if ($game->provider_id == $item->id) {
                    $game->provider = $item;
                    break; // Break out of the loop once the matching provider is found
                }
            }
            $games[] = $game;
        }

        // Mendeklarasikan variabel untuk dikirim ke tampilan
        return response()->json($games);
        // return $games;
    }
}
