<?php


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

use App\Libs\Push\HuaWei\Admin\Msg\ValidatorUtil;
use Exception;

class LightSetting
{
    private $color;

    private $light_on_duration;

    private $light_off_duration;

    private $fields;

    public function __construct()
    {
        $this->color  = array();
        $this->fields = array();
    }

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

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

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

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

    public function buildFields()
    {
        try {
            $this->check_parameter();
        } catch (Exception $e) {
            echo $e;
        }
        $keys = array(
            'color',
            'light_on_duration',
            'light_off_duration'
        );
        foreach ($keys as $key) {
            if (isset($this->$key)) {
                $this->fields[$key] = $this->$key;
            }
        }
    }

    private function check_parameter()
    {
        if (empty($this->color)) {
            throw new Exception("color must be selected when light_settings is set");
        }
        if (empty($this->light_on_duration)) {
            throw new Exception("light_on_duration must be selected when light_settings is set");
        }
        if (empty($this->light_off_duration)) {
            throw new Exception("light_off_duration must be selected when light_settings is set");
        }
        $LIGTH_DURATION_PATTERN = "/\\d+|\\d+[sS]|\\d+.\\d{1,9}|\\d+.\\d{1,9}[sS]/";
        if (FALSE == ValidatorUtil::validatePattern($LIGTH_DURATION_PATTERN, $this->light_on_duration)) {
            throw new Exception("light_on_duration format is wrong");
        }
        if (FALSE == ValidatorUtil::validatePattern($LIGTH_DURATION_PATTERN, $this->light_off_duration)) {
            throw new Exception("light_off_duration format is wrong");
        }


    }
}