<?php

namespace App\Game\Services;

use App\Util;
use GuzzleHttp\Client;

class PPlayTestService
{
    protected $baseUrl;
    public $secretKey;

    public $providerId;

    public $secureLogin;

    public function __construct()
    {
        $this->baseUrl = "https://api.prerelease-env.biz/IntegrationService/v3/http/CasinoGameAPI";
        $this->secretKey = "Gp5XhqasNn";
        $this->providerId = "PragmaticPlay";
        $this->secureLogin = "btcsms_bitcosmos";
    }


    function generateHash($params)
    {
        ksort($params);
//        $queryString = http_build_query($params);

        $queryString = '';
        foreach ($params as $key => $value) {
            $queryString .= $key . '=' . $value . '&';
        }

        // 去掉最后一个多余的 '&'
        $queryString = rtrim($queryString, '&');


        $secretKey = $this->secretKey;
        $queryStringWithSecret = $queryString . $secretKey;
        return md5($queryStringWithSecret);
    }

    function checkHash($params)
    {
        $hash = @$params['hash'];
        unset($params['hash']);
        if(isset($params['providerId'])){
            return ($hash == $this->generateHash($params)) && ($params['providerId'] == $this->providerId);
        }
        return  $hash == $this->generateHash($params);
    }


    // 发送POST请求
    protected function postRequest($endpoint, $data,$json = true)
    {
        $data['secureLogin'] = $this->secureLogin;
        $data['hash'] = $this->generateHash($data);
        $post = http_build_query($data);
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
        $rs = Util::curlPost2($this->baseUrl . $endpoint,$post,true,$headers);
        Util::WriteLog('24680_pp_rs', $post);
        Util::WriteLog('24680_pp_rs', $rs);
        if(!$json){
            return $rs;
        }
        return json_decode($rs, true);
    }

    public function getCasinoGames()
    {
        $data = [];
        return $this->postRequest('/getCasinoGames/',$data);
    }

    public function getLobbyGames()
    {
        $data = [];
        return $this->postRequest('/getLobbyGames/',$data);
    }

    public function closeSession($externalPlayerId,$gameId,$clearHistory=0)
    {
        $data = [
            'externalPlayerId' => $externalPlayerId,
            'gameId' => $gameId,
            'clearHistory' => $clearHistory
        ];
        return $this->postRequest('/closeSession/',$data);
    }

    public function cancelRound($externalPlayerId,$gameId,$roundId)
    {
        $data = [
            'externalPlayerId' => $externalPlayerId,
            'gameId' => $gameId,
            'roundId' => $roundId
        ];
        return $this->postRequest('/cancelRound/',$data);
    }

    public function heartbeatCheck()
    {
        return file_get_contents($this->baseUrl.'/health/heartbeatCheck');
    }


    public function getLaunchURL($symbol,$token,$playerId,$currency='BRL',$lang='pt')
    {
        $data = [
            'symbol' => $symbol,
            'language' => $lang,
            'token' => $token,
            'externalPlayerId' => $playerId,
            'currency' => $currency,
            'lobbyUrl' => route('pp.lobby')
        ];
        return $this->postRequest('/game/url',$data);
    }

}

