<?php

class sgx
{
    private $agent;
    private $token;
    private $url;

    public function __construct($call)
    {
        $cfg = $this->getApiConfig($call);

        $this->agent = $cfg['agent_code'];
        $this->token = $cfg['secret_key'];
        $this->url   = $cfg['api_url'];
    }
    public function create($username)
    {
        $param = [
            'command' => 'user_create',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function deposit($username, $amount)
    {
        $param = [
            'command' => 'user_deposit',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username,
            'amount' => $amount
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function withdraw($username, $amount)
    {
        $param = [
            'command' => 'user_withdraw',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username,
            'amount' => $amount
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }
    public function userbalance($username)
    {
        $param = [
            'command' => 'user_info',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function agentbalance()
    {
        $param = [
            'command' => 'agent_info',
            'agent_code' => $this->agent,
            'signature' => $this->token
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function resetBalance($username)
    {
        $param = [
            'command' => 'user_withdraw_reset',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username,
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function resetAllBalance()
    {
        $param = [
            'command' => 'user_withdraw_reset',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'all_users' => true,
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }
    
    public function gamelist($provider)
    {
        $param = [
            'command' => 'game_list',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'provider_code' => $provider
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function opengame($username, $gamecode)
    {
        $param = [
            'command' => 'game_launch',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'user_code' => $username,
            'game_code' => $gamecode,
        ];

        $url = $this->url;
        return $this->connect($url, $param);
    }

    public function getGameHistory($userCode = '', $limit = 300)
    {
        $limit = (int)$limit;
        if ($limit <= 0) {
            $limit = 300;
        } elseif ($limit > 500) {
            $limit = 500;
        }

        $param = [
            'command' => 'game_history',
            'agent_code' => $this->agent,
            'signature' => $this->token,
            'limit' => $limit,
        ];

        $userCode = trim((string)$userCode);
        if ($userCode !== '') {
            $param['user_code'] = $userCode;
        }

        $url = $this->url;
        return $this->connect($url, $param);
    }

    private function getApiConfig($call)
    {
        $q = mysqli_query(
            $call,
            "SELECT api_url, agent_code, secret_key
             FROM tb_api_config
             WHERE api_name='SGX'
             LIMIT 1"
        );

        if (!$q || mysqli_num_rows($q) == 0) {
            throw new Exception('SGX API CONFIG NOT FOUND');
        }

        return mysqli_fetch_assoc($q);
    }
    private function connect($url, $postArray)
    {
        $jsonData = json_encode($postArray);
        $headerArray = ['Content-Type: application/json'];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $res = curl_exec($ch);
        curl_close($ch);

        return json_decode($res, true);
    }
}
?>
