<?php

namespace app\models\mikrotik\routing\bgp;

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

    public $id;
    public $instance;
    public $routing_mark;
    public $redistribute_connected;
    public $redistribute_static;
    public $redistribute_rip;
    public $redistribute_ospf;
    public $redistribute_other_bgp;
    public $in_filter;
    public $out_filter;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['instance', 'string'],
            ['routing_mark', 'string'],
            ['redistribute_connected', 'string'],
            ['redistribute_static', 'string'],
            ['redistribute_rip', 'string'],
            ['redistribute_ospf', 'string'],
            ['redistribute_other_bgp', 'string'],
            ['in_filter', 'string'],
            ['out_filter', 'string'],
            ['invalid', '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('/routing/bgp/instance/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->instance = $response->getProperty('instance');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->redistribute_connected = $response->getProperty('redistribute-connected');
                $c->redistribute_static = $response->getProperty('redistribute-static');
                $c->redistribute_rip = $response->getProperty('redistribute-rip');
                $c->redistribute_ospf = $response->getProperty('redistribute-ospf');
                $c->redistribute_other_bgp = $response->getProperty('redistribute-other-bgp');
                $c->in_filter = $response->getProperty('in-filter');
                $c->out_filter = $response->getProperty('out-filter');
                $c->invalid = $response->getProperty('invalid');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/instance/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 = $response->getProperty('.id');
                $this->instance = $response->getProperty('instance');
                $this->routing_mark = $response->getProperty('routing-mark');
                $this->redistribute_connected = (($response->getProperty('redistribute-connected') == 'true') ? true : false);
                $this->redistribute_static = (($response->getProperty('redistribute-static') == 'true') ? true : false);
                $this->redistribute_rip = (($response->getProperty('redistribute-rip') == 'true') ? true : false);
                $this->redistribute_ospf = (($response->getProperty('redistribute-ospf') == 'true') ? true : false);
                $this->redistribute_other_bgp = (($response->getProperty('redistribute-other-bgp') == 'true') ? true : false);
                $this->in_filter = $response->getProperty('in-filter');
                $this->out_filter = $response->getProperty('out-filter');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Vrf();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/instance/vrf/set');
        $setRequest->setargument('.id', $id);
        if ($this->instance != $c->instance) {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->routing_mark != $c->routing_mark) {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        $setRequest->setargument('redistribute-connected', (($this->redistribute_connected == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-static', (($this->redistribute_static == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-rip', (($this->redistribute_rip == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-ospf', (($this->redistribute_ospf == true) ? "true" : "false"));
        $setRequest->setargument('redistribute-other-bgp', (($this->redistribute_other_bgp == true) ? "true" : "false"));
        if ($this->in_filter != $c->in_filter) {
            $setRequest->setargument('in-filter', $this->in_filter);
        }
        if ($this->out_filter != $c->out_filter) {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/bgp/instance/vrf/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/bgp/instance/vrf/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/instance/vrf/add');
        if ($this->instance != "") {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->redistribute_connected != "") {
            $setRequest->setargument('redistribute-connected', (($this->redistribute_connected == true) ? "true" : "false"));
        }
        if ($this->redistribute_static != "") {
            $setRequest->setargument('redistribute-static', (($this->redistribute_static == true) ? "true" : "false"));
        }
        if ($this->redistribute_rip != "") {
            $setRequest->setargument('redistribute-rip', (($this->redistribute_rip == true) ? "true" : "false"));
        }
        if ($this->redistribute_ospf != "") {
            $setRequest->setargument('redistribute-ospf', (($this->redistribute_ospf == true) ? "true" : "false"));
        }
        if ($this->redistribute_other_bgp != "") {
            $setRequest->setargument('redistribute-other-bgp', (($this->redistribute_other_bgp == true) ? "true" : "false"));
        }
        if ($this->in_filter != "") {
            $setRequest->setargument('in-filter', $this->in_filter);
        }
        if ($this->out_filter != "") {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        $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/bgp/instance/vrf');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
