<?php


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


use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
use Exception;
use App\Libs\Push\HuaWei\Admin\Constants;
use App\Libs\Push\HuaWei\Admin\PushLogConfig;

class AndroidConfig
{
    private $collapse_key;

    private $category;

    private $urgency;

    private $ttl;

    private $bi_tag;

    private $fast_app_target;

    private $notification;

    private $fields;

    private $data;

    public function __construct()
    {
        $this->urgency      = null;
        $this->notification = null;
        $this->fields       = array();
    }

    public function getFields()
    {
        return $this->fields;
    }

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

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

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

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

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

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

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

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

    public function buildFields()
    {
        try {
            $this->check_parameter();
        } catch (Exception $e) {
//             echo $e;
            PushLogConfig::getSingleInstance()->LogMessage($e, Constants::HW_PUSH_LOG_ERROR_LEVEL);
            return;
        }
        $keys = array(
            'collapse_key',
            'category',
            'urgency',
            'ttl',
            'bi_tag',
            'fast_app_target',
            'notification',
            'data'
        );
        foreach ($keys as $key) {
            if (isset($this->$key)) {
                $this->fields[$key] = $this->$key;
            }
        }
    }

    private function check_parameter()
    {
        if (!empty($this->collapse_key) && ($this->collapse_key < -1 || $this->collapse_key > 100)) {
            throw new Exception("Collapse Key should be [-1, 100]");
        }
        if (!empty($this->fast_app_target) && !in_array($this->fast_app_target, array(
                1,
                2
            ))) {
            throw new Exception("Invalid fast app target type[one of 1 or 2]");
        }

        if (!empty($this->urgency) && !in_array($this->urgency, array(
                AndroidConfigDeliveryPriority::PRIORITY_HIGH,
                AndroidConfigDeliveryPriority::PRIORITY_NORMAL
            ))) {
            throw new Exception("delivery priority shouid be [HIGH, NOMAL]");
        }

        if (!empty($this->ttl)) {
            if (FALSE == ValidatorUtil::validatePattern(Constants::TTL_PATTERN, $this->ttl)) {
                throw new Exception("The TTL's format is wrong");
            }
        }
    }

    public function get_all_vars()
    {
        var_dump(get_object_vars($this));
    }
}