<?php
include "../function/connect.php";
class Zer00iB
{
    private $agentCode = "anak_2";
    private $signature = "4bee33f2233e7e5db50618571cd6f29a";
    private $api = "https://api.nexusggr.com";
    public function __construct($koneksi)
    {
        $config = $this->getConfigFromDatabase($koneksi);
        $this->agentCode = $config['agent_code'];
        $this->signature = $config['signature'];
    }
    private function getConfigFromDatabase($koneksi)
    {
        $sql = "SELECT agent_code, signature FROM softgaming_api WHERE id = 1";
        $result = mysqli_query($koneksi, $sql);

        $config = array();
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                $config['agent_code'] = $row['agent_code'];
                $config['signature'] = $row['signature'];
            }
        } else {
            die("Tidak ada data konfigurasi");
        }

        return $config;
    }
    public function Create($username)
    {
        $action = [
            'method' => 'user_create',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    public function GameList($provider)
    {
        $action = [
            'method' => 'game_list',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'provider_code' => $provider,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    public function ProviderList()
    {
        $action = [
            'method' => 'provider_list',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    public function OpenGame($username, $game_code, $game_provider)
    {
        $action = [
            'method' => 'game_launch',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
            'game_code' => $game_code,
            'provider_code' => $game_provider,
            'lang' => 'id',
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }

    public function Deposit($username, $amount)
    {
        $action = [
            'method' => 'user_deposit',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
            'amount' => $amount
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    
    public function Withdraw($username, $amount)
    {
        $action = [
            'method' => 'user_withdraw',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
            'amount' => $amount,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }

    public function GetBalance($username)
    {
        $action = [
            'method' => 'money_info',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    public function InfoAgent()
    {
        $action = [
            'method' => 'money_info',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    public function HistoryBet($username, $type, $start, $end, $page, $perpage)
    {
        $action = [
            'method' => 'get_game_log',
            'agent_code' => $this->agentCode,
            'agent_token' => $this->signature,
            'user_code' => $username,
            'game_type' => $type,
            'start' => $start,
            'end' => $end,
            'page' => $page,
            'perPage' => $perpage,
        ];

        $api = $this->api;
        return $this->connect($api, $action);
    }
    private function connect($api, $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, $api);
        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 $res;
    }
}
$ZH = new Zer00iB($koneksi);
?>
