<?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 Rip extends Model {

    public $distribute_default;
    public $redistribute_static;
    public $redistribute_connected;
    public $redistribute_ospf;
    public $redistribute_bgp;
    public $metric_default;
    public $metric_static;
    public $metric_connected;
    public $metric_ospf;
    public $metric_bgp;
    public $update_timer;
    public $timeout_timer;
    public $garbage_timer;

    public function rules() {
        return [
            ['distribute_default', 'string'],
            ['redistribute_static', 'string'],
            ['redistribute_connected', 'string'],
            ['redistribute_ospf', 'string'],
            ['redistribute_bgp', 'string'],
            ['metric_default', 'string'],
            ['metric_static', 'string'],
            ['metric_connected', 'string'],
            ['metric_ospf', 'string'],
            ['metric_bgp', 'string'],
            ['update_timer', 'string'],
            ['timeout_timer', 'string'],
            ['garbage_timer', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/print');
        $response = $connect->getConnection()->sendSync($getRequest);

        $this->distribute_default = $response->getProperty('distribute-default');
        $this->redistribute_static = (($response->getProperty('redistribute-static') == 'true') ? true : false);
        $this->redistribute_connected = (($response->getProperty('redistribute-connected') == 'true') ? true : false);
        $this->redistribute_ospf = (($response->getProperty('redistribute-ospf') == 'true') ? true : false);
        $this->redistribute_bgp = (($response->getProperty('redistribute-bgp') == 'true') ? true : false);
        $this->metric_default = $response->getProperty('metric-default');
        $this->metric_static = $response->getProperty('metric-static');
        $this->metric_connected = $response->getProperty('metric-connected');
        $this->metric_ospf = $response->getProperty('metric-ospf');
        $this->metric_bgp = $response->getProperty('metric-bgp');
        $this->update_timer = $response->getProperty('update-timer');
        $this->timeout_timer = $response->getProperty('timeout-timer');
        $this->garbage_timer = $response->getProperty('garbage-timer');

        return;
    }

    public function update() {
        $c = new Rip();
        $c->getAll();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/rip/set');

        if ($this->distribute_default != $c->distribute_default) {
            $setRequest->setargument('distribute-default', $this->distribute_default);
        }
        $setRequest->setargument('redistribute-static', (($this->redistribute_static == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-connected', (($this->redistribute_connected == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-ospf', (($this->redistribute_ospf == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-bgp', (($this->redistribute_bgp == true) ? "true" : "false"));
        if ($this->metric_default != $c->metric_default) {
            $setRequest->setargument('metric-default', $this->metric_default);
        }
        if ($this->metric_static != $c->metric_static) {
            $setRequest->setargument('metric-static', $this->metric_static);
        }
        if ($this->metric_connected != $c->metric_connected) {
            $setRequest->setargument('metric-connected', $this->metric_connected);
        }
        if ($this->metric_ospf != $c->metric_ospf) {
            $setRequest->setargument('metric-ospf', $this->metric_ospf);
        }
        if ($this->metric_bgp != $c->metric_bgp) {
            $setRequest->setargument('metric-bgp', $this->metric_bgp);
        }
        if ($this->update_timer != $c->update_timer) {
            $setRequest->setargument('update-timer', $this->update_timer);
        }
        if ($this->timeout_timer != $c->timeout_timer) {
            $setRequest->setargument('timeout-timer', $this->timeout_timer);
        }
        if ($this->garbage_timer != $c->garbage_timer) {
            $setRequest->setargument('garbage-timer', $this->garbage_timer);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/rip/route/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';
        }
    }

}
