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

    public $id;
    public $name;
    public $as;
    public $router_id;
    public $redistribute_connected;
    public $redistribute_static;
    public $redistribute_rip;
    public $redistribute_ospf;
    public $redistribute_other_bgp;
    public $out_filter;
    public $confederation;
    public $cluster_id;
    public $confederation_peers;
    public $client_to_client_reflection;
    public $ignore_as_path_len;
    public $routing_table;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['as', 'string'],
            ['router_id', 'string'],
            ['redistribute_connected', 'string'],
            ['redistribute_static', 'string'],
            ['redistribute_rip', 'string'],
            ['redistribute_ospf', 'string'],
            ['redistribute_other_bgp', 'string'],
            ['out_filter', 'string'],
            ['confederation', 'string'],
            ['cluster_id', 'string'],
            ['confederation_peers', 'string'],
            ['client_to_client_reflection', 'string'],
            ['ignore_as_path_len', 'string'],
            ['routing_table', 'string'],
            ['disabled', 'string'],
        ];
    }

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

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/instance/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Instances();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->as = $response->getProperty('as');
                $c->router_id = $response->getProperty('router-id');
                $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->out_filter = $response->getProperty('out-filter');
                $c->confederation = $response->getProperty('confederation');
                $c->cluster_id = $response->getProperty('cluster-id');
                $c->confederation_peers = $response->getProperty('confederation-peers');
                $c->client_to_client_reflection = $response->getProperty('client-to-client-reflection');
                $c->ignore_as_path_len = $response->getProperty('ignore-as-path-len');
                $c->routing_table = $response->getProperty('routing-table');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/instance/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->name = $response->getProperty('name');
                $this->as = $response->getProperty('as');
                $this->router_id = $response->getProperty('router-id');
                $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->out_filter = $response->getProperty('out-filter');
                $this->confederation = $response->getProperty('confederation');
                $this->cluster_id = $response->getProperty('cluster-id');
                $this->confederation_peers = $response->getProperty('confederation-peers');
                $this->client_to_client_reflection = (($response->getProperty('client-to-client-reflection') == 'true') ? true : false);
                $this->ignore_as_path_len = (($response->getProperty('ignore-as-path-len') == 'true') ? true : false);
                $this->routing_table = $response->getProperty('routing-table');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Instances();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/instance/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->as != $c->as) {
            $setRequest->setargument('as', $this->as);
        }
        if ($this->router_id != $c->router_id) {
            $setRequest->setargument('router-id', $this->router_id);
        }
        $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->out_filter != $c->out_filter) {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        if ($this->confederation != $c->confederation) {
            if ($this->confederation == '') {
                $unset[] = 'confederation';
            } else {
                $setRequest->setargument('confederation', $this->confederation);
            }
        }
        if ($this->cluster_id != $c->cluster_id) {
            $setRequest->setargument('cluster-id', $this->cluster_id);
        }
        if ($this->confederation_peers != $c->confederation_peers) {
            $setRequest->setargument('confederation-peers', $this->confederation_peers);
        }
        $setRequest->setargument('client-to-client-reflection', (($this->client_to_client_reflection == true) ? "true" : "false"));
        $setRequest->setargument('ignore-as-path-len', (($this->ignore_as_path_len == true) ? "true" : "false"));
        if ($this->routing_table != $c->routing_table) {
            $setRequest->setargument('routing-table', $this->routing_table);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/bgp/instance/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/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/instance/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->as != "") {
            $setRequest->setargument('as', $this->as);
        }
        if ($this->router_id != "") {
            $setRequest->setargument('router-id', $this->router_id);
        }
        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->out_filter != "") {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        if ($this->confederation != "") {
            $setRequest->setargument('confederation', $this->confederation);
        }
        if ($this->cluster_id != "") {
            $setRequest->setargument('cluster-id', $this->cluster_id);
        }
        if ($this->confederation_peers != "") {
            $setRequest->setargument('confederation-peers', $this->confederation_peers);
        }
        if ($this->client_to_client_reflection != "") {
            $setRequest->setargument('client-to-client-reflection', (($this->client_to_client_reflection == true) ? "true" : "false"));
        }
        if ($this->ignore_as_path_len != "") {
            $setRequest->setargument('ignore-as-path-len', (($this->ignore_as_path_len == true) ? "true" : "false"));
        }
        if ($this->routing_table != "") {
            $setRequest->setargument('routing-table', $this->routing_table);
        }
        $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');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
