<?php

namespace app\models\mikrotik\system;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Ntp extends Model {

    public $enabled;
    public $primary_ntp;
    public $secondary_ntp;
    public $server_dns_names;
    public $mode;
    public $poll_interval;
    public $active_server;
    public $last_bad_packet_from;
    public $last_bad_packet_before;
    public $last_bad_packet_reason;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['primary_ntp', 'string'],
            ['secondary_ntp', 'string'],
            ['server_dns_names', 'string'],
            ['mode', 'string'],
            ['poll_interval', 'string'],
            ['active_server', 'string'],
            ['last_bad_packet_from', 'string'],
            ['last_bad_packet_before', 'string'],
            ['last_bad_packet_reason', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/ntp/client/print');

        $response = $connect->getConnection()->sendSync($getRequest);

        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->primary_ntp = $response->getProperty('primary-ntp');
        $this->secondary_ntp = $response->getProperty('secondary-ntp');
        $this->server_dns_names = $response->getProperty('server-dns-names');
        $this->mode = $response->getProperty('mode');
        $this->poll_interval = $response->getProperty('poll-interval');
        $this->active_server = $response->getProperty('active-server');
        $this->last_bad_packet_from = $response->getProperty('last-bad-packet-from');
        $this->last_bad_packet_before = $response->getProperty('last-bad-packet-before');
        $this->last_bad_packet_reason = $response->getProperty('last-bad-packet-reason');

        return;
    }

    public function update() {
        $c = new Ntp();
        $c->get();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/system/ntp/client/set');

        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->primary_ntp != $c->primary_ntp) {
            $setRequest->setargument('primary-ntp', $this->primary_ntp);
        }
        if ($this->secondary_ntp != $c->secondary_ntp) {
            $setRequest->setargument('secondary-ntp', $this->secondary_ntp);
        }
        if ($this->server_dns_names != $c->server_dns_names) {
            $setRequest->setargument('server-dns-names', $this->server_dns_names);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/system/ntp/client/unset');
            foreach ($unset as $value) {

                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

}
