<?php

namespace App\Game\Services;

use App\Util;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Crypt;

class AviatrixService
{
    protected $client;
    protected $baseUrl;
    protected $secretKey;

    protected $cid;

    public function __construct()
    {
        $this->client = new Client();
        $this->cid = '24680com';
        $this->baseUrl = 'https://game-preprod.aviatrix.work';
        $this->secretKey = '4fb4dbe2-0009-438c-8734-0e23a683588b';
    }

    protected function generateSignature($data)
    {
        return base64_encode(hash_hmac('md5', json_encode($data), $this->secretKey, true));
    }

    public function checkSignature($data,$signature)
    {
        //Util::WriteLog('24680_aviatri',$data);
        return $this->generateSignature($data) == $signature;
    }

    protected function postRequest($endpoint, $data)
    {
        $signature = $this->generateSignature($data);

        $response = $this->client->post($this->baseUrl . $endpoint, [
            'headers' => [
                'Content-Type' => 'application/json',
                'X-Auth-Signature' => $signature,
            ],
            'json' => $data,
        ]);

        return json_decode($response->getBody(), true);
    }


    public function getPlayerInfo($data)
    {
        return $this->postRequest('/playerInfo', $data);
    }

    public function placeBet($data)
    {
        return $this->postRequest('/bet', $data);
    }

    public function processWin($data)
    {
        return $this->postRequest('/win', $data);
    }

    public function promoWin($data)
    {
        return $this->postRequest('/transactions/promoWin', $data);
    }

    public function healthCheck()
    {
        return ['healthy' => true];
    }

    public function closeMatch($data)
    {
        return $this->postRequest('/closeMatch', $data);
    }

    /**
     * 获取游戏回合信息 (Game API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     * @param string  $productId , 游戏标识
     * @param string  $roundId , 回合标识 (betId和roundId至少提供一个)
     * @param string  $betId , 下注标识 (betId和roundId至少提供一个)
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john",
     *   "productId": "nft-aviatrix",
     *   "roundId": "2028207"
     * }
     */
    public function getRoundInfo($cid, $playerId, $productId, $roundId = null, $betId = null)
    {
        $data = compact('cid', 'playerId', 'productId', 'roundId', 'betId');
        return $this->postRequest('/game/round', $data);
    }

    /**
     * 启动游戏 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     * @param string  $productId , 游戏标识
     * @param string  $currency , 货币类型
     * @param string  $lang , 可选, 语言
     * @param string  $lobbyUrl , 可选, 大厅URL
     * @param boolean  $isDemo , 可选, 是否为演示模式
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john",
     *   "productId": "nft-aviatrix",
     *   "currency": "USD",
     *   "lang": "en",
     *   "lobbyUrl": "https://example.com/lobby",
     *   "isDemo": true
     * }
     */
    public function launchGame($playerId, $lang = null, $lobbyUrl = null, $isDemo = null)
    {
        $cid = $this->cid;
        $productId = 'nft-aviatrix';
        $sessionToken = Crypt::encryptString($playerId);
        $sessionToken = rtrim(strtr($sessionToken, '+/', '-_'), '=');
        return $this->baseUrl.'?cid='.$cid.'&productId='.$productId.'&sessionToken='.$sessionToken.'&lang='.$lang;
    }

    /**
     * 检查交易 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $txId , 事务标识
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715"
     * }
     */
    public function checkTransaction($cid, $txId)
    {
        $data = compact('cid', 'txId');
        return $this->postRequest('/transferwallet/checkTx', $data);
    }

    /**
     * 获取玩家余额 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john"
     * }
     */
    public function getBalance($cid, $playerId)
    {
        $data = compact('cid', 'playerId');
        return $this->postRequest('/transferwallet/getBalance', $data);
    }

    /**
     * 获取交易历史 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     * @param integer  $limit , 可选, 返回记录的限制数
     * @param string  $from , 可选, 起始时间
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john",
     *   "limit": 10,
     *   "from": "2023-01-01"
     * }
     */
    public function getTransactionHistory($cid, $playerId, $limit = null, $from = null)
    {
        $data = compact('cid', 'playerId', 'limit', 'from');
        return $this->postRequest('/transferwallet/getHistory', $data);
    }

    /**
     * 存款 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     * @param string  $txId , 事务标识
     * @param string  $currency , 货币类型
     * @param integer  $amount , 存款金额
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john",
     *   "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
     *   "currency": "EUR",
     *   "amount": 1234
     * }
     */
    public function deposit($cid, $playerId, $txId, $currency, $amount)
    {
        $data = compact('cid', 'playerId', 'txId', 'currency', 'amount');
        return $this->postRequest('/transferwallet/deposit', $data);
    }

    /**
     * 取款 (Transfer Wallet API)
     * 请求参数:
     * @param string  $cid , 品牌标识
     * @param string  $playerId , 玩家标识
     * @param string  $txId , 事务标识
     * @param string  $currency , 货币类型
     * @param integer  $amount , 取款金额
     *
     * 调用示例:
     * {
     *   "cid": "somebrand",
     *   "playerId": "john",
     *   "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
     *   "currency": "EUR",
     *   "amount": 1234
     * }
     */
    public function withdraw($cid, $playerId, $txId, $currency, $amount)
    {
        $data = compact('cid', 'playerId', 'txId', 'currency', 'amount');
        return $this->postRequest('/transferwallet/withdraw', $data);
    }
}
