<?php

namespace Base\Tool;

use GuzzleHttp\Client;

class TaskClient
{
    const SUCCESS = '0000';

    protected $client;

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => C('TASK_URL'),
            'timeout'  => 10.0,
        ]);
    }

    protected function post($uri, $data)
    {
        $response = $this->client->post($uri, [
            'verify' => false,
            'form_params' => $data
        ]);
        $result = (string)$response->getBody();
        return json_decode($result, true);
    }

    public function sendSmsCode(string $mobile, string $clientIp)
    {
        $options = ['type' => 'code', 'client_ip' => $clientIp];
        return $this->sendSms($mobile, $options);
    }

    public function sendSmsContent(string $mobile, string $content)
    {
        $options = ['type' => 'content', 'content' => $content];
        return $this->sendSms($mobile, $options);
    }

    public function sendSmsBatch(array $mobiles, string $content)
    {
        $options = ['type' => 'batch', 'content' => $content];
        return $this->sendSms($mobiles, $options);
    }

    private function sendSms($mobile, array $options)
    {
        return $this->post('/message/sms-send', ['mobile' => $mobile, 'options' => $options]);
    }

    public function checkSms($mobile, $code)
    {
        return $this->post('/message/sms-check', ['mobile' => $mobile, 'code' => $code]);
    }

    public function registerEvent($userId, $source)
    {
        return $this->post('/game-event/register', ['user_id' => $userId, 'source' => $source]);
    }
}