<?php


namespace App\Libs\Push\HuaWei\Admin\Msg\InstanceApp;

use App\Libs\Push\HuaWei\Admin\Constants;
use App\Libs\Push\HuaWei\Admin\PushLogConfig;

class InstanceAppPushBody
{
// for pass-through
    private $messageId;
    private $data;

    // for notification
    private $title;
    private $description;
    private $page;
    private $params;
    private $ringtone;

    private $fields;

    public function __construct()
    {
    }

    public function messageId($value)
    {
        $this->messageId = $value;
    }

    public function data($value)
    {
        $this->data = $value;
    }

    public function title($value)
    {
        $this->title = $value;
    }

    public function description($value)
    {
        $this->description = $value;
    }

    public function page($value)
    {
        $this->page = $value;
    }

    public function params($value)
    {
        $this->params = $value;
    }

    public function ringtone($value)
    {
        $this->ringtone = $value;
    }

    public function getFields()
    {
        $result = "{";
        foreach ($this->fields as $key => $value) {
            $result = $result . $key . ":" . json_encode($value) . ",";
            PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][result:' . $result, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
        }
        if (strlen($result) > 1) {
            $result = rtrim($result, ",");
        }
        $result = $result . "}";
        return $result;
    }

    public function buildFields()
    {

        $keys = array(
            'messageId',
            'data',
            'title',
            'description',
            'page',
            'params',
            'ringtone'
        );
        foreach ($keys as $key) {
            PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][key:' . $key . '][value:' . json_encode($this->$key) . ']', Constants::HW_PUSH_LOG_DEBUG_LEVEL);
            if (isset($this->$key)) {
                $this->fields[$key] = $this->$key;
            }
        }
        PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . '][buildFields result:' . json_encode($this->fields), Constants::HW_PUSH_LOG_DEBUG_LEVEL);

    }
}