<?php

namespace app\models\mikrotik\radius;

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

    public $id;
    public $service;
    public $called_id;
    public $domain;
    public $address;
    public $secret;
    public $authentication_port;
    public $accounting_port;
    public $timeout;
    public $accounting_backup;
    public $realm;
    public $src_address;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['called_id', 'string'],
            ['domain', 'string'],
            ['address', 'string'],
            ['secret', 'string'],
            ['authentication_port', 'string'],
            ['accounting_port', 'string'],
            ['timeout', 'string'],
            ['accounting_backup', 'string'],
            ['realm', 'string'],
            ['src_address', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/radius/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Radius();
                $c->id = $response->getProperty('.id');
                $c->service = $response->getProperty('service');
                $c->called_id = $response->getProperty('called-id');
                $c->domain = $response->getProperty('domain');
                $c->address = $response->getProperty('address');
                $c->secret = $response->getProperty('secret');
                $c->authentication_port = $response->getProperty('authentication-port');
                $c->accounting_port = $response->getProperty('accounting-port');
                $c->timeout = $response->getProperty('timeout');
                $c->accounting_backup = $response->getProperty('accounting-backup');
                $c->realm = $response->getProperty('realm');
                $c->src_address = $response->getProperty('src-address');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/radius/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $response->getProperty('.id');
                $this->service = $response->getProperty('service');
                $this->called_id = $response->getProperty('called-id');
                $this->domain = $response->getProperty('domain');
                $this->address = $response->getProperty('address');
                $this->secret = $response->getProperty('secret');
                $this->authentication_port = $response->getProperty('authentication-port');
                $this->accounting_port = $response->getProperty('accounting-port');
                $this->timeout = $response->getProperty('timeout');
                $this->accounting_backup = (($response->getProperty('accounting-backup') == 'true') ? true : false);
                $this->realm = $response->getProperty('realm');
                $this->src_address = $response->getProperty('src-address');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Radius();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/radius/set');
        $setRequest->setargument('.id', $id);
        if ($this->service != $c->service) {
            $setRequest->setargument('service', $this->service);
        }
        if ($this->called_id != $c->called_id) {
            $setRequest->setargument('called-id', $this->called_id);
        }
        if ($this->domain != $c->domain) {
            $setRequest->setargument('domain', $this->domain);
        }
        if ($this->address != $c->address) {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->secret != $c->secret) {
            $setRequest->setargument('secret', $this->secret);
        }
        if ($this->authentication_port != $c->authentication_port) {
            $setRequest->setargument('authentication-port', $this->authentication_port);
        }
        if ($this->accounting_port != $c->accounting_port) {
            $setRequest->setargument('accounting-port', $this->accounting_port);
        }
        if ($this->timeout != $c->timeout) {
            $setRequest->setargument('timeout', $this->timeout);
        }
        $setRequest->setargument('accounting-backup', (($this->accounting_backup == true) ? "true" : "false"));
        if ($this->realm != $c->realm) {
            $setRequest->setargument('realm', $this->realm);
        }
        if ($this->src_address != $c->src_address) {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/radius/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';
        }
    }

    public function disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/radius/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function enable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/radius/enable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/radius/add');
        if ($this->service != "") {
            $setRequest->setargument('service', $this->service);
        }
        if ($this->called_id != "") {
            $setRequest->setargument('called-id', $this->called_id);
        }
        if ($this->domain != "") {
            $setRequest->setargument('domain', $this->domain);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->secret != "") {
            $setRequest->setargument('secret', $this->secret);
        }
        if ($this->authentication_port != "") {
            $setRequest->setargument('authentication-port', $this->authentication_port);
        }
        if ($this->accounting_port != "") {
            $setRequest->setargument('accounting-port', $this->accounting_port);
        }
        if ($this->timeout != "") {
            $setRequest->setargument('timeout', $this->timeout);
        }
        if ($this->accounting_backup != "") {
            $setRequest->setargument('accounting-backup', (($this->accounting_backup == true) ? "true" : "false"));
        }
        if ($this->realm != "") {
            $setRequest->setargument('realm', $this->realm);
        }
        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/radius');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
