<?php

namespace app\models\mikrotik\mpls\traffic;

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

    public $id;
    public $name;
    public $use_cspf;
    public $setup_priority;
    public $holding_priority;
    public $record_route;
    public $affinity_include_all;
    public $affinity_include_any;
    public $affinity_exclude;
    public $reoptimize_interval;
    public $hops;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['use_cspf', 'string'],
            ['setup_priority', 'string'],
            ['holding_priority', 'string'],
            ['record_route', 'string'],
            ['affinity_include_all', 'string'],
            ['affinity_include_any', 'string'],
            ['affinity_exclude', 'string'],
            ['reoptimize_interval', 'string'],
            ['hops', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Tunnel();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->use_cspf = $response->getProperty('use-cspf');
                $c->setup_priority = $response->getProperty('setup-priority');
                $c->holding_priority = $response->getProperty('holding-priority');
                $c->record_route = $response->getProperty('record-route');
                $c->affinity_include_all = $response->getProperty('affinity-include-all');
                $c->affinity_include_any = $response->getProperty('affinity-include-any');
                $c->affinity_exclude = $response->getProperty('affinity-exclude');
                $c->reoptimize_interval = $response->getProperty('reoptimize-interval');
                $c->hops = $response->getProperty('hops');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function names() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $ret = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {

                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/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->use_cspf = (($response->getProperty('use-cspf') == 'true') ? true : false);
                $this->setup_priority = $response->getProperty('setup-priority');
                $this->holding_priority = $response->getProperty('holding-priority');
                $this->record_route = (($response->getProperty('record-route') == 'true') ? true : false);
                $this->affinity_include_all = $response->getProperty('affinity-include-all');
                $this->affinity_include_any = $response->getProperty('affinity-include-any');
                $this->affinity_exclude = $response->getProperty('affinity-exclude');
                $this->reoptimize_interval = $response->getProperty('reoptimize-interval');
                $this->hops = $response->getProperty('hops');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Tunnel();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        $setRequest->setargument('use-cspf', (($this->use_cspf == true) ? "true" : "false"));
        if ($this->setup_priority != $c->setup_priority) {
            if ($this->setup_priority == '') {
                $unset[] = 'setup-priority';
            } else {
                $setRequest->setargument('setup-priority', $this->setup_priority);
            }
        }
        if ($this->holding_priority != $c->holding_priority) {
            if ($this->holding_priority == '') {
                $unset[] = 'holding-priority';
            } else {
                $setRequest->setargument('holding-priority', $this->holding_priority);
            }
        }
        $setRequest->setargument('record-route', (($this->record_route == true) ? "true" : "false"));
        if ($this->affinity_include_all != $c->affinity_include_all) {
            if ($this->affinity_include_all == '') {
                $unset[] = 'affinity-include-all';
            } else {
                $setRequest->setargument('affinity-include-all', $this->affinity_include_all);
            }
        }
        if ($this->affinity_include_any != $c->affinity_include_any) {
            if ($this->affinity_include_any == '') {
                $unset[] = 'affinity-include-any';
            } else {
                $setRequest->setargument('affinity-include-any', $this->affinity_include_any);
            }
        }
        if ($this->affinity_exclude != $c->affinity_exclude) {
            if ($this->affinity_exclude == '') {
                $unset[] = 'affinity-exclude';
            } else {
                $setRequest->setargument('affinity-exclude', $this->affinity_exclude);
            }
        }
        if ($this->reoptimize_interval != $c->reoptimize_interval) {
            if ($this->reoptimize_interval == '') {
                $unset[] = 'reoptimize-interval';
            } else {
                $setRequest->setargument('reoptimize-interval', $this->reoptimize_interval);
            }
        }
        if ($this->hops != $c->hops) {
            $setRequest->setargument('hops', $this->hops);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/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('/mpls/traffic-eng/tunnel-path/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function enable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/enable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/traffic-eng/tunnel-path/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->use_cspf != "") {
            $setRequest->setargument('use-cspf', (($this->use_cspf == true) ? "true" : "false"));
        }
        if ($this->setup_priority != "") {
            $setRequest->setargument('setup-priority', $this->setup_priority);
        }
        if ($this->holding_priority != "") {
            $setRequest->setargument('holding-priority', $this->holding_priority);
        }
        if ($this->record_route != "") {
            $setRequest->setargument('record-route', (($this->record_route == true) ? "true" : "false"));
        }
        if ($this->affinity_include_all != "") {
            $setRequest->setargument('affinity-include-all', $this->affinity_include_all);
        }
        if ($this->affinity_include_any != "") {
            $setRequest->setargument('affinity-include-any', $this->affinity_include_any);
        }
        if ($this->affinity_exclude != "") {
            $setRequest->setargument('affinity-exclude', $this->affinity_exclude);
        }
        if ($this->reoptimize_interval != "") {
            $setRequest->setargument('reoptimize-interval', $this->reoptimize_interval);
        }
        if ($this->hops != "") {
            $setRequest->setargument('hops', $this->hops);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $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('/mpls/traffic-eng/tunnel-path');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
