<?php


namespace App\Console\Commands\Push;


use App\Consts\PushConst;
use App\Libs\Push\HuaWei\HwPushCommon;
use App\Libs\Push\OPPOPush\OPPOPushCommon;
use App\Libs\Push\VPush\VPush;
use App\Libs\Push\XMPush\MiPushCommon;
use App\Modules\Push\Models\QappPushApp;
use App\Modules\Push\Models\QappPushUser;
use Exception;
use Illuminate\Console\Command;

class PushTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'push:test {uid} {url}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test push';

    /**
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle()
    {
        // 用户Uid
        $uid    = $this->argument('uid');
        $newUrl = $this->argument('url');

        // 获取用户push信息
        $pushUser  = QappPushUser::getPushUserByUid($uid);
        $regId     = getProp($pushUser, 'reg_id');
        $regIdList = [$regId];

        // 获取push app信息
        $pushAppId    = getProp($pushUser, 'app_id');
        $pushApp      = QappPushApp::getPushAppByAppId($pushAppId);
        $package      = getProp($pushApp, 'package');
        $appId        = getProp($pushApp, 'app_id');
        $name         = getProp($pushApp, 'name');
        $provider     = getProp($pushApp, 'provider');
        $appSecret    = getProp($pushApp, 'app_secret');
        $appKey       = getProp($pushApp, 'app_key');
        $masterSecret = getProp($pushApp, 'master_secret');

        // 打印
        var_dump('name:' . $name);
        var_dump('package:' . $package);
        var_dump('provider:' . $provider);
        var_dump('app_id:' . $appId);
        var_dump('app_secret:' . $appSecret);
        var_dump('app_key:' . $appKey);
        var_dump('master_secret:' . $masterSecret);

        $title   = 'this title';
        $content = 'this content ' . date('Y-m-d H:i:s');
        $url     = '/views/Reader?send_order_id=1643289&bid=vdqY7p15xnZQzK4VzzgmMD6wG2yr8BNX&chapter_id=4774851';
        if (strlen($newUrl) > 5) {
            $url = $newUrl;
        }
        var_dump('uid:' . $uid);
        var_dump('title:' . $title);
        var_dump('content:' . $content);
        var_dump('url:' . $url);
        var_dump('reg id:' . $regId);

        $result = [];
        try {
            switch ($provider) {
                // 华为
                case PushConst::PROVIDER_HW:
                    // 初始化huawei推送
                    $client = new HwPushCommon($appId, $appSecret);

                    // 循环推送
                    $client->setToken($regIdList);
                    $client->setFastAppTarget(1);
                    $result = $client->sendPushMessage($title, $content, $url);
                    break;
                // 小米
                case PushConst::PROVIDER_MI:
                    // 初始化小米推送
                    $client = new MiPushCommon($package, $appSecret);

                    // 循环推送
                    $client->setRegArr($regIdList);
                    $result = $client->sendMessage($title, $content, $url);
                    break;
                // OPPO
                case PushConst::PROVIDER_OPPO:
                    // 初始化oppo推送
                    $client    = new OPPOPushCommon($appKey, $masterSecret);
                    $messageId = $client->getMessageId($title, $content, $url);

                    // 循环推送
                    $client->setRegArr($regIdList);
                    $result = $client->broadCastRegIds($messageId);
                    break;
                // VIVO
                case PushConst::PROVIDER_VIVO:
                    // 初始化oppo推送
                    $client = new VPush($appId, $appKey, $appSecret);

                    // 设置相关推送数据,保存消息内容(批量推送至少需要两个用户,故写死一个reg_id)
                    $regIdList[] = '16004127020450881896432';
                    $client->setPushData($title, $content, $url, $regIdList);
                    $client->saveListPayload();

                    // 循环推送
                    $result = $client->sendMessage();
                    break;
            }
        } catch (Exception $e) {
            var_dump($e->getFile());
            var_dump($e->getLine());
            var_dump($e->getMessage());
            var_dump($e->getTraceAsString());
        }

        dd($result);
    }

}