<?php

namespace app\models\mikrotik\routing\rip;

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

    public $id;
    public $interface = 'all';
    public $receive;
    public $send;
    public $authentication;
    public $authentication_key;
    public $key_chain = '';
    public $in_prefix_list = '';
    public $out_prefix_list = '';
    public $updates_tx;
    public $updates_rx;
    public $bad_packets;
    public $bad_routes;
    public $disabled;
    public $passive;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['receive', 'string'],
            ['send', 'string'],
            ['authentication', 'string'],
            ['authentication_key', 'string'],
            ['key_chain', 'string'],
            ['in_prefix_list', 'string'],
            ['out_prefix_list', 'string'],
            ['updates_tx', 'string'],
            ['updates_rx', 'string'],
            ['bad_packets', 'string'],
            ['bad_routes', 'string'],
            ['disabled', 'string'],
            ['passive', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/interface/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Interfacesrip();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->receive = $response->getProperty('receive');
                $c->send = $response->getProperty('send');
                $c->authentication = $response->getProperty('authentication');
                $c->authentication_key = $response->getProperty('authentication-key');
                $c->key_chain = $response->getProperty('key-chain');
                $c->in_prefix_list = $response->getProperty('in-prefix-list');
                $c->out_prefix_list = $response->getProperty('out-prefix-list');
                $c->updates_tx = $response->getProperty('updates-tx');
                $c->updates_rx = $response->getProperty('updates-rx');
                $c->bad_packets = $response->getProperty('bad-packets');
                $c->bad_routes = $response->getProperty('bad-routes');
                $c->disabled = $response->getProperty('disabled');
                $c->passive = $response->getProperty('passive');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/interface/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->interface = $response->getProperty('interface');
                $this->receive = $response->getProperty('receive');
                $this->send = $response->getProperty('send');
                $this->authentication = $response->getProperty('authentication');
                $this->authentication_key = $response->getProperty('authentication-key');
                $this->key_chain = $response->getProperty('key-chain');
                $this->in_prefix_list = $response->getProperty('in-prefix-list');
                $this->out_prefix_list = $response->getProperty('out-prefix-list');
                $this->updates_tx = $response->getProperty('updates-tx');
                $this->updates_rx = $response->getProperty('updates-rx');
                $this->bad_packets = $response->getProperty('bad-packets');
                $this->bad_routes = $response->getProperty('bad-routes');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->passive = (($response->getProperty('passive') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Interfacesrip();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/rip/interface/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->receive != $c->receive) {
            $setRequest->setargument('receive', $this->receive);
        }
        if ($this->send != $c->send) {
            $setRequest->setargument('send', $this->send);
        }
        if ($this->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->authentication_key != $c->authentication_key) {
            $setRequest->setargument('authentication-key', $this->authentication_key);
        }
        if ($this->key_chain != $c->key_chain) {
            $setRequest->setargument('key-chain', $this->key_chain);
        }
        if ($this->in_prefix_list != $c->in_prefix_list) {
            $setRequest->setargument('in-prefix-list', $this->in_prefix_list);
        }
        if ($this->out_prefix_list != $c->out_prefix_list) {
            $setRequest->setargument('out-prefix-list', $this->out_prefix_list);
        }
        if ($this->updates_tx != $c->updates_tx) {
            $setRequest->setargument('updates-tx', $this->updates_tx);
        }
        if ($this->updates_rx != $c->updates_rx) {
            $setRequest->setargument('updates-rx', $this->updates_rx);
        }
        if ($this->bad_packets != $c->bad_packets) {
            $setRequest->setargument('bad-packets', $this->bad_packets);
        }
        if ($this->bad_routes != $c->bad_routes) {
            $setRequest->setargument('bad-routes', $this->bad_routes);
        }
        $setRequest->setargument('passive', (($this->passive == true) ? "true" : "false"));
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/rip/interface/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('/routing/rip/interface/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/rip/interface/add');
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->receive != "") {
            $setRequest->setargument('receive', $this->receive);
        }
        if ($this->send != "") {
            $setRequest->setargument('send', $this->send);
        }
        if ($this->authentication != "") {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->authentication_key != "") {
            $setRequest->setargument('authentication-key', $this->authentication_key);
        }
        if ($this->key_chain != "") {
            $setRequest->setargument('key-chain', $this->key_chain);
        }
        if ($this->in_prefix_list != "") {
            $setRequest->setargument('in-prefix-list', $this->in_prefix_list);
        }
        if ($this->out_prefix_list != "") {
            $setRequest->setargument('out-prefix-list', $this->out_prefix_list);
        }
        if ($this->updates_tx != "") {
            $setRequest->setargument('updates-tx', $this->updates_tx);
        }
        if ($this->updates_rx != "") {
            $setRequest->setargument('updates-rx', $this->updates_rx);
        }
        if ($this->bad_packets != "") {
            $setRequest->setargument('bad-packets', $this->bad_packets);
        }
        if ($this->bad_routes != "") {
            $setRequest->setargument('bad-routes', $this->bad_routes);
        }
        if ($this->passive != "") {
            $setRequest->setargument('passive', (($this->passive == true) ? "true" : "false"));
        }
        $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('/routing/rip/interface');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
