<?php

namespace app\models\mikrotik\ip\snmp;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Communities extends Model {

    public $id;
    public $name;
    public $addresses = '0.0.0.0/0';
    public $security = 'none';
    public $read_access;
    public $write_access = 'true';
    public $authentication_protocol = 'MD5';
    public $encryption_protocol = 'DES';
    public $authentication_password;
    public $encryption_password;
    public $default;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['addresses', 'string'],
            ['security', 'string'],
            ['read_access', 'string'],
            ['write_access', 'string'],
            ['authentication_protocol', 'string'],
            ['encryption_protocol', 'string'],
            ['authentication_password', 'string'],
            ['encryption_password', 'string'],
            ['default', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/snmp/community/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Communities();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->addresses = $response->getProperty('addresses');
                $c->security = $response->getProperty('security');
                $c->read_access = $response->getProperty('read-access');
                $c->write_access = $response->getProperty('write-access');
                $c->authentication_protocol = $response->getProperty('authentication-protocol');
                $c->encryption_protocol = $response->getProperty('encryption-protocol');
                $c->authentication_password = $response->getProperty('authentication-password');
                $c->encryption_password = $response->getProperty('encryption-password');
                $c->default = $response->getProperty('default');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/snmp/community/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->name = $response->getProperty('name');
                $this->addresses = $response->getProperty('addresses');
                $this->security = $response->getProperty('security');
                $this->read_access = (($response->getProperty('read-access') == 'true') ? true : false);
                $this->write_access = (($response->getProperty('write-access') == 'true') ? true : false);
                $this->authentication_protocol = $response->getProperty('authentication-protocol');
                $this->encryption_protocol = $response->getProperty('encryption-protocol');
                $this->authentication_password = $response->getProperty('authentication-password');
                $this->encryption_password = $response->getProperty('encryption-password');
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/snmp/community/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->addresses != "") {
            $setRequest->setargument('addresses', $this->addresses);
        }
        if ($this->security != "") {
            $setRequest->setargument('security', $this->security);
        }
        if ($this->read_access != "") {
            $setRequest->setargument('read-access', (($this->read_access == true) ? "true" : "false"));
        }
        if ($this->write_access != "") {
            $setRequest->setargument('write-access', (($this->write_access == true) ? "true" : "false"));
        }
        if ($this->authentication_protocol != "") {
            $setRequest->setargument('authentication-protocol', $this->authentication_protocol);
        }
        if ($this->encryption_protocol != "") {
            $setRequest->setargument('encryption-protocol', $this->encryption_protocol);
        }
        if ($this->authentication_password != "") {
            $setRequest->setargument('authentication-password', $this->authentication_password);
        }
        if ($this->encryption_password != "") {
            $setRequest->setargument('encryption-password', $this->encryption_password);
        }
        if ($this->default != "") {
            $setRequest->setargument('default', (($this->default == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/snmp/community/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->addresses != "") {
            $setRequest->setargument('addresses', $this->addresses);
        }
        if ($this->security != "") {
            $setRequest->setargument('security', $this->security);
        }
        if ($this->read_access != "") {
            $setRequest->setargument('read-access', (($this->read_access == true) ? "true" : "false"));
        }
        if ($this->write_access != "") {
            $setRequest->setargument('write-access', (($this->write_access == true) ? "true" : "false"));
        }
        if ($this->authentication_protocol != "") {
            $setRequest->setargument('authentication-protocol', $this->authentication_protocol);
        }
        if ($this->encryption_protocol != "") {
            $setRequest->setargument('encryption-protocol', $this->encryption_protocol);
        }
        if ($this->authentication_password != "") {
            $setRequest->setargument('authentication-password', $this->authentication_password);
        }
        if ($this->encryption_password != "") {
            $setRequest->setargument('encryption-password', $this->encryption_password);
        }
        if ($this->default != "") {
            $setRequest->setargument('default', (($this->default == true) ? "true" : "false"));
        }
        $ret = $connect->getConnection()->sendSync($setRequest);
        return $ret;
    }

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

    public function getNames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/snmp/community/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $ret = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

}
