<?php

namespace App\Game\Services;


use App\Game\BetBy\PlayerDataItem;
use App\Game\GlobalUserInfo;
use App\Notification\TelegramBot;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use App\Game\BetBy\PlayerDetails;
use App\Game\BetBy\PlayerSegment;
use App\Game\BetBy\BonusTemplate;
use App\Game\BetBy\TemplateItem;
use App\Game\BetBy\BonusItem;
use App\Game\BetBy\Bonus;
use App\Game\BetBy\ErrorItem;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Exception;
use Illuminate\Support\Facades\Log;

class BetbyTestService
{
    protected $client;
    protected $baseUrl;
    public $operatorId;
    public $brandId;
    protected $privateKey;
    protected $publicKey;

    //-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENPcid+kKnpbbW3HuY+m1uRbishkC
    //nhD1k1apZyZUqLy3kmCtBTdKfNaOrk9hjOmJjD84hRBHjwLCgPraOATGAw==-----END PUBLIC KEY-----
    public function __construct()
    {
        $this->client = new Client();

        $this->baseUrl = "https://external-api.invisiblesport.com/api/v1/external_api";
        $this->operatorId = '2424946715804176387';
        $this->brandId = '2424948766365847552';
        $this->privateKey = '-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIE+5vrcbCZ+7UyavHgEmaNDMVc4V/OmQcK8hMnoaBI0toAoGCCqGSM49
AwEHoUQDQgAENPcid+kKnpbbW3HuY+m1uRbishkCnhD1k1apZyZUqLy3kmCtBTdKfNaOrk9hjOmJjD84hRBHjwLCgPraOATGAw==
-----END EC PRIVATE KEY-----';
        $this->publicKey = '-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAjmWwsejCzJHljSDpYtwP
uA5HYIP49dNaIuY4ambFiYSTLCPEMqq6kJ9Xo6nphW0mfV+VsDOqcynkzgNAG2y0
kuEe7TF0Tpc5OgTrj9PEm6kxCryxnHSRB56Wv22RQlpqYEO/wQiTXxecgceVL/Dm
vQ91peCcuK6GtSr46RzT/lzsx6DrcGTdUb0aJz7sszmhN6YG8RAb/vHGuekfajZi
UilPU51NOFqRLtSYTxBG3uakPhNtwnxjTFoQZmQ3jPFXCXwn+FnocLIPK5UPJe35
sbnqGhWyIPnTPf75HE6WqdP4GnMrvO1oIaIluuyM1WjCxTVVqHUnUJpMOAPK4QtK
CkU8kWV3URJrTyuQ9A8dODcCvRjZZp96Dvd6taJHN2/brZ78zX3gBP4C8MrtP1na
lzs/Dr7Uo5yQoe/CW5cHczRG/XtbOhloqCJXu56vbMC9DQIDVMEythprAe6haVPE
GBJ2Ci3VgIMY91HILsE42Aoj5uN2LiHLWyh5UN/ESJeY/BL8Vxkv6VDaPjRZD7ee
JeepqOs7zNo2srXBfJIQ3n0ZZULIO/hdZInTLXnAGaP+5eUjkSqsQWt3OttRXmA7
7Ps7k45ghFB1ZFa+t00omOpBzP7HF6f8sdnFNSugvc2vW8c3+Noj74PimX9AcJfd
+rFDv70YSd67t+E/9DSo4l8CAwEAAQ==
-----END PUBLIC KEY-----';
    }

    /**
     * 验证并解码JWT payload
     *
     * @param string $token JWT token
     * @return array 解码后的数据
     */
    public function decodePayload(string $token): array
    {
        return (array) JWT::decode($token, new Key($this->publicKey, 'RS256'));
    }

    /**
     * 生成JWT payload
     *
     * @param array $data 数据
     * @return string 生成的JWT token
     */
    public function encodePayload(array $data): string
    {
        $now = time();
        $payload = [
            'iat' => $now,
            'exp' => $now + 3600,
            'jti' => strval(rand()),
            'iss' => $this->brandId,
            'aud' => $this->brandId,
            'nbf' => $now,
            'payload' => $data
        ];
        return JWT::encode($payload, $this->privateKey, 'ES256');
    }
    public function getDefaultJWT($user)
    {
        $user=(array) $user;
        $now = time();
        $payload = [
            'iss' => $this->brandId,
            'sub' => $user['GlobalUID'],
            'name' => $user['NickName'],
            'iat' => $now,
            'exp' => $now + 3600,
            'jti' => strval(rand()),
            'lang'=>$user['DefaultLanguage']??GlobalUserInfo::getLocale(),
            'currency'=>env('CONFIG_24680_CURRENCY')
        ];
        return JWT::encode($payload, $this->privateKey, 'ES256');
    }

    /**
     * 处理请求异常
     *
     * @param RequestException $e 异常对象
     * @return array 错误信息
     */
    private function handleRequestException(RequestException $e): array
    {
        $response = $e->getResponse();
        if ($response) {
            $body = json_decode($response->getBody(), true);
            if (isset($body['error'])) {

                (new TelegramBot())->sendMsgWithEnv($body);
                return [
                    'error' => new ErrorItem($body['error'])
                ];
            }
        }
        return [
            'error' => new ErrorItem(['name' => 'UnknownError', 'description' => trans('messages.api.description.unknown_error')])
        ];
    }

    /**
     * 处理响应数据
     *
     * @param $response
     * @return mixed
     */
    private function handleResponse($response)
    {
        $data = json_decode($response->getBody(), true);
        $data= $this->decodePayload($data);
        if (isset($data['error'])) {
            (new TelegramBot())->sendMsgWithEnv($data);
            return ['error' => new ErrorItem($data['error'])];
        }
        return $data;
    }

    public function ping()
    {
        try {
            $response = $this->client->get($this->baseUrl . '/ping' );
            $data = json_decode($response->getBody(),true);
            return $data;
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }
    /**
     * 调用Betby的PLAYER_DETAILS接口
     * 获取玩家详细信息
     *
     * @param string $externalPlayerId 玩家ID
     * @param string $segment 分段
     * @param array $depositMethods 存款方式
     * @return PlayerDetails|array
     */
    public function getPlayerDetails(string $externalPlayerId, string $segment, array $depositMethods)
    {
        $payload = $this->encodePayload([
            'external_player_id' => $externalPlayerId,
            'segment' => $segment,
            'deposit_methods' => $depositMethods
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/player_details', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return new PlayerDetails($data);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的PLAYER_SEGMENT接口
     * 获取玩家分段信息
     *
     * @param string $externalPlayerId 玩家ID
     * @return PlayerSegment|array
     */
    public function getPlayerSegment(string $externalPlayerId)
    {
        $payload = $this->encodePayload([
            'brand_id' => $this->brandId,
            'external_player_id' => $externalPlayerId
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/player/segment', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return new PlayerSegment($data);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的TEMPLATES接口
     * 获取所有奖金模板
     *
     * @return array<TemplateItem>|array
     */
    public function getBonusTemplates()
    {
        $payload = $this->encodePayload(['operator_id' => $this->operatorId]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus/templates', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return array_map(function($template) {
                return new TemplateItem($template);
            }, $data['items']);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的TEMPLATE接口
     * 获取指定ID的奖金模板
     *
     * @param string $templateId 奖金模板ID
     * @return TemplateItem|array
     */
    public function getBonusTemplate(string $templateId)
    {
        $payload = $this->encodePayload([
            'operator_id' => $this->operatorId,
            'template_id' => $templateId
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus/template', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return new TemplateItem($data);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的PLAYER_BONUSES接口
     * 获取玩家的所有奖金
     *
     * @param string $externalPlayerId 玩家ID
     * @return array<BonusItem>|array
     */
    public function getPlayerBonuses(string $externalPlayerId)
    {
        $payload = $this->encodePayload([
            'brand_id' => $this->brandId,
            'external_player_id' => $externalPlayerId
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus/player_bonuses', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return array_map(function($bonus) {
                return new BonusItem($bonus);
            }, $data['items']);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的BONUS接口
     * NB. Because of technical features one request can contain around 1000 players.
     * Please, split your list of players in case there are significantly more than 1000 players.
     * 获取指定ID的奖金信息
     *
     * @param string $bonusId 奖金ID
     * @return BonusItem|array
     */
    public function getBonus(string $bonusId)
    {
        $payload = $this->encodePayload([
            'brand_id' => $this->brandId,
            'bonus_id' => $bonusId
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data = $this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            return new BonusItem($data);
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的MASS_GIVE_BONUS接口
     * 批量授予奖金
     *
     * @param string $brandId 品牌ID
     * @param string $templateId 奖金模板ID
     * @param PlayerDataItem[] $playersData 玩家数据
     * @return array 响应数据
     */
    public function massGiveBonus(string $brandId, string $templateId, array $playersData): array
    {
        $payload = $this->encodePayload([
            'brand_id' => $brandId,
            'template_id' => $templateId,
            'players_data' => array_map(function($playerData) {
                return [
                    'external_player_id' => $playerData->externalPlayerId,
                    'currency' => $playerData->currency,
                    'amount' => $playerData->amount,
                    'force_activated' => $playerData->forceActivated,
                ];
            }, $playersData)
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus/mass_give_bonus', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data=$this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            $bonusItems = [];
            foreach (  $data['result'] as $BonusPlayerItem){
                foreach ($BonusPlayerItem as $playerID => $bonus) {
                    if(isset($bonus['error'])){
                        $bonusItems[$playerID]= ['error'=>new ErrorItem($bonus['error'])];
                    }else{
                        $bonusItems[$playerID] = new BonusItem($bonus);
                    }
                }
            }
            return $bonusItems;
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }

    /**
     * 调用Betby的REVOKE_BONUS接口
     * 撤销指定ID的奖金
     *
     * @param string[] $bonuses_ids 奖金IDs
     * @return array<BonusItem>|array
     */
    public function revokeBonus(array $bonuses_ids)
    {
        $payload = $this->encodePayload([
            'brand_id' => $this->brandId,
            'bonus_ids' => $bonuses_ids
        ]);

        try {
            $response = $this->client->post($this->baseUrl . '/bonus/revoke', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => ['payload' => $payload]
            ]);

            $data=$this->handleResponse($response);
            if (isset($data['error'])) {
                return $data;
            }
            $bonusItems = [];
            foreach (  $data['result'] as $BonusPlayerItem){
                foreach ($BonusPlayerItem as $playerID => $bonus) {
                    if(isset($bonus['error'])){
                        $bonusItems[$playerID]= ['error'=>new ErrorItem($bonus['error'])];
                    }else{
                        $bonusItems[$playerID] = new BonusItem($bonus);
                    }
                }
            }
            return $bonusItems;
        } catch (RequestException $e) {
            return $this->handleRequestException($e);
        }
    }
}
