<?php
class SGX {
    private $agent_code = 'slot888';
    private $secret_key = 'fb73e2b7adcaf5b58f14d99c3a571c8d';
    private $base_url = 'https://sgxcenter.xyz/api';
 	private $log_file    = __DIR__ . '/sgx.log'; 
  
    private function writeLog($label, $data) {
          $time = date('Y-m-d H:i:s');
          $msg  = "[$time] [$label] " . (is_array($data) || is_object($data) ? json_encode($data, JSON_PRETTY_PRINT) : $data);
          file_put_contents($this->log_file, $msg . PHP_EOL, FILE_APPEND);
      }

    private function sendRequest($payload) {
        $payload['agent_code'] = $this->agent_code;
        $payload['signature'] = $this->secret_key;

        $jsonData = json_encode($payload);
     	$this->writeLog('REQUEST', $payload);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->base_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            $response = json_encode([
                'status' => 0,
                'msg'    => 'CURL_ERROR',
                'detail' => curl_error($ch)
            ]);
        }
        curl_close($ch);
      	$this->writeLog('RESPONSE', $response);

        return $response;
    }

    public function create_member($username) {
        $payload = [
            'command'   => 'user_create',
            'user_code' => $username,
        ];
        return $this->sendRequest($payload);
    }

    public function info_member($username) {
        $payload = [
            'command'   => 'user_info',
            'user_code' => $username,
        ];
        return $this->sendRequest($payload);
    }

    public function info_agent() {
        $payload = [
            'command' => 'agent_info'
        ];
        return $this->sendRequest($payload);
    }

    public function deposit_member($username, $amount) {
        $payload = [
            'command'   => 'user_deposit',
            'user_code' => $username,
            'amount'    => (int)$amount,
        ];
        return $this->sendRequest($payload);
    }

    public function withdraw_member($username, $amount) {
        $payload = [
            'command'   => 'user_withdraw',
            'user_code' => $username,
            'amount'    => (int)$amount,
        ];
        return $this->sendRequest($payload);
    }

    public function launch_game($username, $gameCode = null) {
        $payload = [
            'command'   => 'game_launch',
            'user_code' => $username,
        ];

        if (!empty($gameCode)) {
            $payload['game_code'] = $gameCode;
        }

        return $this->sendRequest($payload);
    }

    public function getprovider() {
        $payload = [
            'command' => 'provider_list'
        ];
        return $this->sendRequest($payload);
    }

    public function getgames($providerCode) {
        $payload = [
            'command'       => 'game_list',
            'provider_code' => $providerCode,
        ];
        return $this->sendRequest($payload);
    }

    public function game_history() {
        $payload = [
            'command' => 'game_history'
        ];
        return $this->sendRequest($payload);
    }
    
    public function reset_balance($user) {
        $payload = [
            'command' => 'user_withdraw_reset',
            'user_code' => $user
        ];
        return $this->sendRequest($payload);
    }
}

$SGX = new SGX();