<?php


namespace App\Libs\Push\HuaWei;


use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
use App\Libs\Push\HuaWei\Admin\Application;
use App\Libs\Push\HuaWei\Admin\Msg\Android\AndroidConfig;
use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppConfig;
use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppPushBody;
use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppRingTone;
use App\Libs\Push\HuaWei\Admin\Msg\PushMessage;

/**
 * 快应用
 * Class FastAppPushCommon
 * @package App\Libs\Push\HuaWei
 */
class HwPushCommon
{
    private $_app_id;
    private $_app_secret;
    private $_token = [];
    private $_topic = '';
    private $_fast_app_target = 2;
    private $_big_tag = 'app';

    public function __construct($appId, $appSecret)
    {
        $this->_app_id     = $appId;
        $this->_app_secret = $appSecret;

    }

    /**
     * 设置token
     * @param $tokenArr
     */
    public function setToken($tokenArr)
    {
        $this->_token = $tokenArr;
    }

    /**
     * 设置topic
     * @param $topic
     */
    public function setTopic($topic)
    {
        $this->_topic = $topic;
    }

    /**
     * 设置开发态和生产态，1：开发态， 2：生产态
     * @param $target
     */
    public function setFastAppTarget($target)
    {
        $this->_fast_app_target = $target;
    }

    /**
     * 设置bigTag
     * @param $bigTag
     */
    public function setBigTag($bigTag)
    {
        $this->_big_tag = $bigTag;
    }

    /**
     * 发送通知消息
     * @param       $title
     * @param       $desc
     * @param       $pageUrl
     * @param array $params
     * @return bool|mixed|string|null
     */
    public function sendPushMessage($title, $desc, $pageUrl, $params = [])
    {
        // 组装发送数据
        $data    = $this->createFastAppConfigNotificationData($title, $desc, $pageUrl, 0, $params);
        $message = $this->createFastAppMsg($data->getFields());

        # 创建app
        $application = $this->createApplication();

        # 发送消息
        return $application->push_send_msg($message->getFields());
    }

    /**
     * 查询push_token信息
     * @param $pushToken
     * @return mixed|null
     */
    public function queryPushToken($pushToken)
    {
        # 创建app
        $application = $this->createApplication();

        return $application->query_push_token($pushToken);
    }

    /**
     * 主题订阅
     * @param $topic
     * @param $tokenArr
     * @return bool|mixed|string|null
     */
    public function subscribeTopic($topic, $tokenArr)
    {
        # 创建app
        $application = $this->createApplication();

        return $application->subscribe_topic($topic, $tokenArr);
    }

    /**
     * 主题退订
     * @param $topic
     * @param $tokenArr
     * @return bool|mixed|string|null
     */
    public function unSubscribeTopic($topic, $tokenArr)
    {
        # 创建app
        $application = $this->createApplication();

        return $application->unsubscribe_topic($topic, $tokenArr);
    }

    /**
     * 主题退订
     * @param $token
     * @return bool|mixed|string|null
     */
    public function subscribeList($token)
    {
        # 创建app
        $application = $this->createApplication();

        return $application->topic_list($token);
    }

    /**
     * @param        $data
     * @param array  $tokenArr
     * @param string $topic
     * @return PushMessage
     */
    private function createFastAppMsg($data): PushMessage
    {
        $message = new PushMessage();
        $message->data($data);
        $message->android($this->createAndroidConfig()->getFields());
        if ($this->_topic) {
            // 按照Topic向订阅了本topic的用户推消息
            $message->topic($this->_topic);
        } else {
            // 按照Token向目标用户推消息
            $message->token($this->_token);
        }
        $message->buildFields();
        return $message;
    }

    /**
     * @param        $title
     * @param        $desc
     * @param string $pageUrl
     * @param int    $pushType
     * @param array  $params
     * @return InstanceAppConfig
     */
    private function createFastAppConfigNotificationData($title, $desc, $pageUrl = '/', $pushType = 0, $params = []): InstanceAppConfig
    {
        // 设置推送类型，0为通知栏消息
        $instanceAppConfig = new InstanceAppConfig();
        $instanceAppConfig->pushtype($pushType);

        //快应用推送消息体
        $instanceAppPushBody = new InstanceAppPushBody();
        $instanceAppPushBody->title($title);
        $instanceAppPushBody->description($desc);
        $instanceAppPushBody->page($pageUrl);
        $instanceAppPushBody->params((object)[]);

        // 设置是否震动和显示呼吸灯
        $instanceAppRingTone = new InstanceAppRingTone();
        $instanceAppRingTone->breathLight(true);
        $instanceAppRingTone->vibration(true);
        $instanceAppRingTone->buildFields();

        $instanceAppPushBody->ringtone($instanceAppRingTone->getFields());
        $instanceAppPushBody->buildFields();

        $instanceAppConfig->pushbody($instanceAppPushBody->getFields());
        $instanceAppConfig->buildFields();

        return $instanceAppConfig;
    }

    /**
     * @return AndroidConfig
     */
    private function createAndroidConfig(): AndroidConfig
    {
        $android_config = new AndroidConfig();
//        $android_config->collapse_key(-1);
//        $android_config->urgency(AndroidConfigDeliveryPriority::PRIORITY_HIGH);
//        $android_config->bi_tag('app');
        $android_config->fast_app_target($this->_fast_app_target);
        $android_config->buildFields();
        return $android_config;
    }

    /**
     * @return Application
     */
    private function createApplication(): Application
    {
        return new Application($this->_app_id, $this->_app_secret);
    }
}