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

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ChaosController extends Controller
{
    private $urlRequest;
    private $agentCode;
    private $signature;

    public function __construct()
    {
        $restapi = DB::table('api_providers')->where('provider', 'CHAOS')->first();
        $this->urlRequest = rtrim($restapi->url, '/') . '/';
        $this->agentCode  = $restapi->apikey;
        $this->signature  = $restapi->secretkey;
    }

    // Cek Saldo Agent
    public function money_info()
    {
        return $this->connectPost($this->urlRequest . "GetBalanceAgent.ashx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
        ]);
    }

    // Cek Saldo User
    public function money_info_user($username)
    {
        return $this->connectPost($this->urlRequest . "GetBalance.ashx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
            'username'   => $username,
        ]);
    }

    // Buat Member Baru
    public function user_create($username)
    {
        return $this->connectPost($this->urlRequest . "CreateMember.aspx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
            'username'   => $username,
        ]);
    }

    // Deposit ke User (type: "in")
    public function user_deposit($username, $amount)
    {
        return $this->connectPost($this->urlRequest . "MakeTransfer.ashx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
            'username'   => $username,
            'amount'     => $amount,
            'type'       => 'in',
        ]);
    }

    // Withdraw dari User (type: "out")
    public function user_withdraw($username, $amount)
    {
        return $this->connectPost($this->urlRequest . "MakeTransfer.ashx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
            'username'   => $username,
            'amount'     => $amount,
            'type'       => 'out',
        ]);
    }

    // Daftar Provider
    public function provider_list()
    {
        return $this->connectPost($this->urlRequest . "GetProvider.aspx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
        ]);
    }

    // Daftar Game per Provider
    public function game_list($provider_code)
    {
        return $this->connectPost($this->urlRequest . "GetGamelist.aspx", [
            'agent_code'    => $this->agentCode,
            'secret_key'    => $this->signature,
            'provider_code' => $provider_code,
        ]);
    }

    // Launch Game
    public function game_launch($username, $provider_code, $game_type, $game_code)
    {
        return $this->connectPost($this->urlRequest . "LaunchGame.aspx", [
            'agent_code' => $this->agentCode,
            'secret_key' => $this->signature,
            'username'   => $username,
            'provider'   => $provider_code,
            'game_type'  => $game_type,
            'game_code'  => $game_code,
        ]);
    }

    private function connectPost($url, $postData)
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0';
        $protocol  = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ($_SERVER['SERVER_PORT'] ?? 80) == 443
                     ? 'https://' : 'http://';
        $host      = rtrim($_SERVER['HTTP_HOST'] ?? '', '/');
        $domain    = $protocol . $host;

        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_IPRESOLVE      => CURL_IPRESOLVE_V4,
            CURLOPT_AUTOREFERER    => true,
            CURLOPT_USERAGENT      => $userAgent,
            CURLOPT_HEADER         => 0,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL            => $url,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_TCP_KEEPALIVE  => 1,
            CURLOPT_TCP_KEEPIDLE   => 2,
            CURLOPT_TCP_KEEPINTVL  => 3,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($postData),
            CURLOPT_HTTPHEADER     => [
                'Content-Type: application/json',
                'X-Client-Domain: ' . $domain,
            ],
        ]);

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

        return json_decode($output, true);
    }
}