<?php

namespace Themes\Pro\modunawa\app\Services;

use Unirest\Request;

class MsqMarketApiService
{
    const BASE_URL = 'https://api.dev.msq.market';

    private function response($response, $checkCode)
    {
        if (is_array($checkCode) == 1) {
            foreach ($checkCode as $code) {
                if ($response->code == $code) {
                    $data = json_encode($response->body ?? []);
                    return json_decode($data, true);
                }
            }
            return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
        } else {
            if ($response->code == $checkCode) {
                $data = json_encode($response->body ?? []);
                return json_decode($data, true);
            } else {
                return [ 'apiStatus' => $response->code, 'body' => $response->body->msg ?? _e('Action failed') ];
            }
        }

    }

    public function result($request_key)
    {
        $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/result',
            [ 'Accept' => 'application/json', ],

            [ 'request_key' => $request_key, ]
        );

        return $this->response($response, [200, 201]);
    }

    public function prepare($type, $to = '', $msqxpAmount = 0, $p2upAmount = 0)
    {
        $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/prepare',
            [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ],

            json_encode([
                'dapp' => [
                    'name' => 'Point2u',
                    'url' => 'https://p2u.kr'
                ],
                'type' => $type,
                'transaction' => [
                    'tokens' => [
                        'MSQXP' => (int)$msqxpAmount,
                        'P2UP' => (int)$p2upAmount,
                    ],
                    'to' => $to,
                ],
            ], JSON_UNESCAPED_UNICODE)
        );

        return $this->response($response, 201);
    }

    public function prepareConnection()
    {
        $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/prepare_connection',
            [ 'Accept' => 'application/json' ],
        );

        return $this->response($response, 201);
    }

    public function establishConnection($uuid, $token)
    {
        $response = Request::post(MsqMarketApiService::BASE_URL . '/wallet/establish_connection',
            [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
                'Authorization' => 'Bearer ' . $token
            ],

            json_encode([ 'keyId' => $uuid, 'platform' => '' ], JSON_UNESCAPED_UNICODE)
        );

        return $this->response($response, 201);
    }

    public function getBalance($token)
    {
        $response = Request::get(MsqMarketApiService::BASE_URL . '/wallet/get_balance',
            [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $token
            ],
        );

        return $this->response($response, 200);
    }
}
