<?php

namespace app\models\mikrotik\mpls\vpls;

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

    public $id;
    public $name;
    public $route_distinguisher;
    public $vpls_id;
    public $import_route_targets;
    public $export_route_targets;
    public $l2router_id;
    public $use_control_word;
    public $pw_mtu;
    public $bridge;
    public $bridge_cost;
    public $bridge_horizon;
    public $inactive;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['route_distinguisher', 'string'],
            ['vpls_id', 'string'],
            ['import_route_targets', 'string'],
            ['export_route_targets', 'string'],
            ['l2router_id', 'string'],
            ['use_control_word', 'string'],
            ['pw_mtu', 'string'],
            ['bridge', 'string'],
            ['bridge_cost', 'string'],
            ['bridge_horizon', 'string'],
            ['inactive', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/vpls/cisco-bgp-vpls/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Cisco();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->route_distinguisher = $response->getProperty('route-distinguisher');
                $c->vpls_id = $response->getProperty('vpls-id');
                $c->import_route_targets = $response->getProperty('import-route-targets');
                $c->export_route_targets = $response->getProperty('export-route-targets');
                $c->l2router_id = $response->getProperty('l2router-id');
                $c->use_control_word = $response->getProperty('use-control-word');
                $c->pw_mtu = $response->getProperty('pw-mtu');
                $c->bridge = $response->getProperty('bridge');
                $c->bridge_cost = $response->getProperty('bridge-cost');
                $c->bridge_horizon = $response->getProperty('bridge-horizon');
                $c->inactive = $response->getProperty('inactive');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/vpls/cisco-bgp-vpls/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->route_distinguisher = $response->getProperty('route-distinguisher');
                $this->vpls_id = $response->getProperty('vpls-id');
                $this->import_route_targets = $response->getProperty('import-route-targets');
                $this->export_route_targets = $response->getProperty('export-route-targets');
                $this->l2router_id = $response->getProperty('l2router-id');
                $this->use_control_word = (($response->getProperty('use-control-word') == 'true') ? true : false);
                $this->pw_mtu = $response->getProperty('pw-mtu');
                $this->bridge = $response->getProperty('bridge');
                $this->bridge_cost = $response->getProperty('bridge-cost');
                $this->bridge_horizon = $response->getProperty('bridge-horizon');
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Cisco();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/vpls/cisco-bgp-vpls/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->route_distinguisher != $c->route_distinguisher) {
            if ($this->route_distinguisher == '') {
                $unset[] = 'route-distinguisher';
            } else {
                $setRequest->setargument('route-distinguisher', $this->route_distinguisher);
            }
        }
        if ($this->vpls_id != $c->vpls_id) {
            if ($this->vpls_id == '') {
                $unset[] = 'vpls-id';
            } else {
                $setRequest->setargument('vpls-id', $this->vpls_id);
            }
        }
        if ($this->import_route_targets != $c->import_route_targets) {
            if ($this->import_route_targets == '') {
                $unset[] = 'import-route-targets';
            } else {
                $setRequest->setargument('import-route-targets', $this->import_route_targets);
            }
        }
        if ($this->export_route_targets != $c->export_route_targets) {
            if ($this->export_route_targets == '') {
                $unset[] = 'export-route-targets';
            } else {
                $setRequest->setargument('export-route-targets', $this->export_route_targets);
            }
        }
        if ($this->l2router_id != $c->l2router_id) {
            $setRequest->setargument('l2router-id', $this->l2router_id);
        }

        if ($this->pw_mtu != $c->pw_mtu) {
            $setRequest->setargument('pw-mtu', $this->pw_mtu);
        }
        if ($this->bridge != $c->bridge) {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->bridge_cost != $c->bridge_cost) {
            $setRequest->setargument('bridge-cost', $this->bridge_cost);
        }
        if ($this->bridge_horizon != $c->bridge_horizon) {
            $setRequest->setargument('bridge-horizon', $this->bridge_horizon);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/vpls/cisco-bgp-vpls/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('/interface/vpls/cisco-bgp-vpls/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/vpls/cisco-bgp-vpls/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->route_distinguisher != "") {
            $setRequest->setargument('route-distinguisher', $this->route_distinguisher);
        }
        if ($this->vpls_id != "") {
            $setRequest->setargument('vpls-id', $this->vpls_id);
        }
        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->l2router_id != "") {
            $setRequest->setargument('l2router-id', $this->l2router_id);
        }

        if ($this->pw_mtu != "") {
            $setRequest->setargument('pw-mtu', $this->pw_mtu);
        }
        if ($this->bridge != "") {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->bridge_cost != "") {
            $setRequest->setargument('bridge-cost', $this->bridge_cost);
        }
        if ($this->bridge_horizon != "") {
            $setRequest->setargument('bridge-horizon', $this->bridge_horizon);
        }

        $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('/interface/vpls/cisco-bgp-vpls');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
