<?php

namespace app\components;

use app\models\Oauth;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use yii\base\Component;

class Oceanengine extends Component
{

    const CACHE_KEY = 'oceanengine_cache';

    public function getAccessToken($adminUid)
    {
        return Oauth::find()->where(['uid' => $adminUid])->one()->access_token;
    }

    public function getRefreshToken($adminUid)
    {
        return Oauth::find()->where(['uid' => $adminUid])->one()->refresh_token;
    }

    /**
     *
     * 获取授权账号下的子账号
     * @param $adminAccountId
     *
     * @return array
     */
    public function getAdminChildAccount($adminAccountId): array
    {
        $client = new Client();
        $headers = [
            'Access-Token' => $this->getAccessToken($adminAccountId)
        ];
        $request = new Request('GET',
            'https://api.oceanengine.com/open_api/oauth2/advertiser/get/',
            $headers);
        $res = $client->sendAsync($request)->wait();
        return json_decode($res->getBody(), true);
    }

    /**
     * 获取本地推用户
     *
     * @param $adminUid
     *
     * @return array
     */
    public function getAccountLocal($adminUid): array
    {
        $client = new Client();
        $headers = [
            'Access-Token' => $this->getAccessToken($adminUid)
        ];
        $request = new Request('GET', 'https://ad.oceanengine.com/open_api/2/customer_center/advertiser/list/?account_source=LOCAL&cc_account_id=1742303335399432&filtering=%7B%22account_name%22%3A%22%22%7D&page=1&page_size=100', $headers);
        $res = $client->sendAsync($request)->wait();
        return json_decode($res->getBody(), true);
    }

    /**
     * 获取最新的线索
     */
    public function getClue($adminUid, array $accountId, $startTime, $endTime, $page = 1)
    {
        $client = new Client();
        $headers = [
            'Content-Type' => 'application/json',
            'Access-Token' => $this->getAccessToken($adminUid)
        ];
        $accounts = implode(',', $accountId);
        $body = '{
          "local_account_ids": [
            ' . $accounts . '
          ],
          "start_time": "' . $startTime . '",
          "end_time": "' . $endTime . '",
          "page": ' . $page . ',
          "page_size": 100
        }';
        $request = new Request('POST', 'https://api.oceanengine.com/open_api/2/tools/clue/life/get/', $headers, $body);
        $res = $client->sendAsync($request)->wait();

        return json_decode($res->getBody(), true);
    }

    public function refreshAccessToken($adminUid)
    {
        $client = new Client();
        $headers = [
            'Content-Type' => 'application/json'
        ];
        $body = '{
  "app_id": ' . \Yii::$app->params['app_id'] . ',
  "secret": "' . \Yii::$app->params['secret'] . '",
  "refresh_token": "' . $this->getRefreshToken($adminUid) . '"
}';
        $request = new Request('POST',
            'https://api.oceanengine.com/open_api/oauth2/refresh_token/',
            $headers, $body);
        $res = $client->sendAsync($request)->wait();
        return json_decode($res->getBody(), true);
    }
}