<?php

namespace app\models\mikrotik\ip\routes;

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

    public $id;
    public $routing_mark;
    public $interfaces;
    public $route_distinguisher;
    public $import_route_targets;
    public $export_route_targets;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['routing_mark', 'string'],
            ['route_distinguisher', 'string'],
            ['import_route_targets', 'string'],
            ['export_route_targets', 'string'],
            ['disabled', 'string'],
        ];
    }

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

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/vrf/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Vrf();
                $c->id = $response->getProperty('.id');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->interfaces = $response->getProperty('interfaces');
                $c->route_distinguisher = $response->getProperty('route-distinguisher');
                $c->import_route_targets = $response->getProperty('import-route-targets');
                $c->export_route_targets = $response->getProperty('export-route-targets');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/vrf/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->routing_mark = $response->getProperty('routing-mark');
                $this->interfaces = $response->getProperty('interfaces');
                $this->route_distinguisher = $response->getProperty('route-distinguisher');
                $this->import_route_targets = $response->getProperty('import-route-targets');
                $this->export_route_targets = $response->getProperty('export-route-targets');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/route/vrf/set');
        $setRequest->setargument('.id', $id);

        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->interfaces != "") {
            $setRequest->setargument('interfaces', $this->interfaces);
        }
        if ($this->route_distinguisher != "") {
            $setRequest->setargument('route-distinguisher', $this->route_distinguisher);
        }
        if ($this->import_route_targets != "") {
            $setRequest->setargument('import-route-targets', $this->import_route_targets);
        }
        if ($this->export_route_targets != "") {
            $setRequest->setargument('export-route-targets', $this->export_route_targets);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/route/vrf/add');

        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->interfaces != "") {
            $setRequest->setargument('interfaces', $this->interfaces);
        }
        if ($this->route_distinguisher != "") {
            $setRequest->setargument('route-distinguisher', $this->route_distinguisher);
        }
        if ($this->import_route_targets != "") {
            $setRequest->setargument('import-route-targets', $this->import_route_targets);
        }
        if ($this->export_route_targets != "") {
            $setRequest->setargument('export-route-targets', $this->export_route_targets);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

}
