<?php 
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
	
class SgxController {
	private $agent_code;
	private $agent_token;
	private $base_url;
	
	public function __construct() {
		// Constructor kosong atau untuk inisialisasi jika diperlukan
		$restapi = DB::table('api_providers')->where('provider', 'SGX')->first();
		$this->agent_code = $restapi->apikey;

		$this->agent_token = $restapi->secretkey;

		$this->base_url = $restapi->url;
	}
	
	public function money_info() {
		return $this->send_request([
		'command' => 'agent_info',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token
		]);
	}
	
	public function provider_list() {
		return $this->send_request([
		'command' => 'provider_list',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token
		]);
	}
	
	public function info_betting() {
		return $this->send_request([
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token
		], "/info_bet_history");
	}
	
	public function game_list($provider) {
		return $this->send_request([
		'command' => 'game_list',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'provider_code' => $provider,
		'lang' => 'id'
		]);
	}
	
	public function money_info_user($user_member) {
		return $this->send_request([
		'command' => 'user_info',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'user_code' => $user_member
		]);
	}
	
	public function user_create($user_member) {
		return $this->send_request([
		'command' => 'user_create',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'user_code' => $user_member
		]);
	}
	
	public function user_deposit($user_member, $deposit) {
		return $this->send_request([
		'command' => 'user_deposit',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'user_code' => $user_member,
        'amount' => $deposit
		]);
	}
	
	public function user_withdraw($user_member, $withdraw) {
		return $this->send_request([
		'command' => 'user_withdraw',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'user_code' => $user_member,
        'amount' => $withdraw
		]);
	}
	
	public function game_launch($user_member, $provider_code, $game_code) {
		return $this->send_request([
		'command' => 'game_launch',
        'agent_code' => $this->agent_code,
        'signature' => $this->agent_token,
        'user_code' => $user_member,
		'provider_code' => $provider_code,
        'game_code' => $game_code,
		'lang' => 'id'
		]);
	}
	
	private function send_request($postdata) {
		$url = $this->base_url;
		
		// Mengonversi array ke JSON
		$jsonData = json_encode($postdata);
		
		$headerArray = ['Content-Type: application/json'];
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // Menggunakan jsonData
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Pastikan kita menerima response
		$res = curl_exec($ch);
		curl_close($ch);
		return json_decode($res, true);
	}
}