<?php

namespace App\Services\Vendor\GeTui;

use App\Services\Service;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Ixudra\Curl\Facades\Curl;

class ApiService extends Service
{
    /**
     * @var string 域名
     */
    private $host = "https://restapi.getui.com";
    private $auth_token;
    /**
     * @var string appid
     */
    private $appid;
    /**
     * @var string
     */
    private $appkey;
    /**
     * @var string
     */
    private $mastersecret;
    /**
     * @var array
     */
    private $result;

    public function __construct()
    {
        $this->appid = config('getui.appid');
        $this->appkey = config('getui.appkey');
        $this->mastersecret = config('getui.mastersecret');
    }

    /**
     * 设置appid
     * @param  string  $appid
     */
    public function setAppid(string $appid)
    {
        $this->appid = $appid;
    }

    /**
     * 设置appkey
     * @param  string  $appkey
     */
    public function setAppkey(string $appkey)
    {
        $this->appkey = $appkey;
    }

    /**
     * @param  string  $cid
     * @param  string  $alias
     * @return bool
     */
    public function bindAlias(string $cid, string $alias)
    {

        $this->authSign();
        $api = "{$this->host}/v1/{$this->appid}/bind_alias";
        $data = array(
            'alias_list' => [
                [
                    'cid' => $cid,
                    'alias' => $alias,
                ],
            ],
        );
        $result = Curl::to($api)->withHeader("authtoken:{$this->auth_token}")->asJson(true)->withData($data)->post();
        $this->setResult($result['result']);
        if ('ok' == $this->result) {
            return true;
        }
    }

    /**
     * 鉴权
     */
    public function authSign()
    {
        if (!$this->auth_token = Cache::get("gt:authsign:{$this->appid}", false)) {
            $api = "{$this->host}/v1/{$this->appid}/auth_sign";
            $data = array(
                'timestamp' => micro_time(),
                'appkey' => $this->appkey,
            );
            $data['sign'] = $this->sign($data);
            $result = Curl::to($api)->asJson(true)->withData($data)->post();
            $this->setResult($result['result']);
            if ('ok' == $this->result) {
                Cache::put(
                    "gt:authsign:{$this->appid}",
                    $result['auth_token'],
                    Carbon::createFromTimestampMs($result['expire_time'])
                );
                $this->auth_token = $result['auth_token'];
            }
        }
        Redis::set("gt:authsign", $this->auth_token);
    }

    /**
     * 计算sign值
     * @param  array  $data
     * @return string
     */
    private function sign(array $data): string
    {
        return hash('sha256', $data['appkey'] . $data['timestamp'] . $this->mastersecret);
    }

    public function setResult($result)
    {
        $this->result = $result;
    }

    public function pushSingle()
    {
        $this->authSign();
        $api = "{$this->host}/v1/{$this->appid}/push_single";
        $data = array(
            'message' => [
                'appkey' => $this->appkey,
                'is_offline' => true,
                'offline_expire_time' => 10000000,
                'msgtype' => 'notification',
            ],
            'notification' => [
                'style' => [
                    'type' => 1,
                    'text' => '请填写通知内容',
                    'title' => '请填写通知标题',
                ],
                'transmission_type' => true,
                'transmission_content' => '透传内容',
            ],
            'cid' => '256d2ee813a80a824326641d352f7ab6',
            'requestid' => uniqid(),
        );
        dump($data, $this->auth_token);
        $result = Curl::to($api)->withHeader("authtoken:{$this->auth_token}")->asJson(true)->withData($data)->post();
        $this->setResult($result['result']);
        dump($result);
    }
}
