<?php

namespace app\models\mikrotik\capsman;

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 Aaa extends Model {

    public $mac_format;
    public $mac_mode;
    public $mac_caching;
    public $interim_update;

    public function rules() {
        return [
            ['mac_format', 'string'],
            ['mac_mode', 'string'],
            ['mac_caching', 'string'],
            ['interim_update', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/aaa/print');

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

        $this->mac_format = $response->getProperty('mac-format');
        $this->mac_mode = $response->getProperty('mac-mode');
        $this->mac_caching = $response->getProperty('mac-caching');
        $this->interim_update = $response->getProperty('interim-update');

        return;
    }

    public function update() {
        $c = new Aaa();
        $c->get();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/aaa/set');

        if ($this->mac_format != $c->mac_format) {
            $setRequest->setargument('mac-format', $this->mac_format);
        }
        if ($this->mac_mode != $c->mac_mode) {
            $setRequest->setargument('mac-mode', $this->mac_mode);
        }
        if ($this->mac_caching != $c->mac_caching) {
            $setRequest->setargument('mac-caching', $this->mac_caching);
        }
        if ($this->interim_update != $c->interim_update) {
            $setRequest->setargument('interim-update', $this->interim_update);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/aaa/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $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';
        }
    }

}
