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

    public $type = 'Traffic Eng Interface';
    public $id;
    public $name;
    public $mtu;
    public $disable_running_check;
    public $from_address;
    public $to_address;
    public $bandwidth;
    public $primary_path;
    public $secondary_paths;
    public $primary_retry_interval;
    public $setup_priority;
    public $holding_priority;
    public $record_route;
    public $affinity_include_all;
    public $affinity_include_any;
    public $affinity_exclude;
    public $reoptimize_interval;
    public $bandwidth_limit;
    public $auto_bandwidth_range;
    public $auto_bandwidth_reserve;
    public $auto_bandwidth_avg_interval;
    public $auto_bandwidth_update_interval;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['disable_running_check', 'string'],
            ['from_address', 'string'],
            ['to_address', 'string'],
            ['bandwidth', 'string'],
            ['primary_path', 'string'],
            ['primary_retry_interval', 'string'],
            ['setup_priority', 'string'],
            ['holding_priority', 'string'],
            ['record_route', 'string'],
            ['affinity_include_all', 'string'],
            ['affinity_include_any', 'string'],
            ['affinity_exclude', 'string'],
            ['reoptimize_interval', 'string'],
            ['bandwidth_limit', 'string'],
            ['auto_bandwidth_range', 'string'],
            ['auto_bandwidth_reserve', 'string'],
            ['auto_bandwidth_avg_interval', 'string'],
            ['auto_bandwidth_update_interval', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/traffic-eng/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Traffic();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->disable_running_check = $response->getProperty('disable-running-check');
                $c->from_address = $response->getProperty('from-address');
                $c->to_address = $response->getProperty('to-address');
                $c->bandwidth = $response->getProperty('bandwidth');
                $c->primary_path = $response->getProperty('primary-path');
                $c->secondary_paths = $response->getProperty('secondary-paths');
                $c->primary_retry_interval = $response->getProperty('primary-retry-interval');
                $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->bandwidth_limit = $response->getProperty('bandwidth-limit');
                $c->auto_bandwidth_range = $response->getProperty('auto-bandwidth-range');
                $c->auto_bandwidth_reserve = $response->getProperty('auto-bandwidth-reserve');
                $c->auto_bandwidth_avg_interval = $response->getProperty('auto-bandwidth-avg-interval');
                $c->auto_bandwidth_update_interval = $response->getProperty('auto-bandwidth-update-interval');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/traffic-eng/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->mtu = $response->getProperty('mtu');
                $this->disable_running_check = (($response->getProperty('disable-running-check') == 'true') ? true : false);
                $this->from_address = $response->getProperty('from-address');
                $this->to_address = $response->getProperty('to-address');
                $this->bandwidth = $response->getProperty('bandwidth');
                $this->primary_path = $response->getProperty('primary-path');
                $this->secondary_paths = $response->getProperty('secondary-paths');
                $this->primary_retry_interval = $response->getProperty('primary-retry-interval');
                $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->bandwidth_limit = $response->getProperty('bandwidth-limit');
                $this->auto_bandwidth_range = $response->getProperty('auto-bandwidth-range');
                $this->auto_bandwidth_reserve = $response->getProperty('auto-bandwidth-reserve');
                $this->auto_bandwidth_avg_interval = $response->getProperty('auto-bandwidth-avg-interval');
                $this->auto_bandwidth_update_interval = $response->getProperty('auto-bandwidth-update-interval');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Traffic();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/traffic-eng/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != $c->mtu) {
            $setRequest->setargument('mtu', $this->mtu);
        }
        $setRequest->setargument('disable-running-check', (($this->disable_running_check == true) ? "true" : "false"));
        if ($this->from_address != $c->from_address) {
            $setRequest->setargument('from-address', $this->from_address);
        }
        if ($this->to_address != $c->to_address) {
            $setRequest->setargument('to-address', $this->to_address);
        }
        if ($this->bandwidth != $c->bandwidth) {
            $setRequest->setargument('bandwidth', $this->bandwidth);
        }
        if ($this->primary_path != $c->primary_path) {
            $setRequest->setargument('primary-path', $this->primary_path);
        }
        if ($this->secondary_paths != $c->secondary_paths) {
            $setRequest->setargument('secondary-paths', $this->secondary_paths);
        }
        if ($this->primary_retry_interval != $c->primary_retry_interval) {
            $setRequest->setargument('primary-retry-interval', $this->primary_retry_interval);
        }
        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->bandwidth_limit != $c->bandwidth_limit) {
            $setRequest->setargument('bandwidth-limit', $this->bandwidth_limit);
        }
        if ($this->auto_bandwidth_range != $c->auto_bandwidth_range) {
            $setRequest->setargument('auto-bandwidth-range', $this->auto_bandwidth_range);
        }
        if ($this->auto_bandwidth_reserve != $c->auto_bandwidth_reserve) {
            $setRequest->setargument('auto-bandwidth-reserve', $this->auto_bandwidth_reserve);
        }
        if ($this->auto_bandwidth_avg_interval != $c->auto_bandwidth_avg_interval) {
            $setRequest->setargument('auto-bandwidth-avg-interval', $this->auto_bandwidth_avg_interval);
        }
        if ($this->auto_bandwidth_update_interval != $c->auto_bandwidth_update_interval) {
            $setRequest->setargument('auto-bandwidth-update-interval', $this->auto_bandwidth_update_interval);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/traffic-eng/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/traffic-eng/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/traffic-eng/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != "") {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->disable_running_check != "") {
            $setRequest->setargument('disable-running-check', (($this->disable_running_check == true) ? "true" : "false"));
        }
        if ($this->from_address != "") {
            $setRequest->setargument('from-address', $this->from_address);
        }
        if ($this->to_address != "") {
            $setRequest->setargument('to-address', $this->to_address);
        }
        if ($this->bandwidth != "") {
            $setRequest->setargument('bandwidth', $this->bandwidth);
        }
        if ($this->primary_path != "") {
            $setRequest->setargument('primary-path', $this->primary_path);
        }
        if ($this->secondary_paths != "") {
            $setRequest->setargument('secondary-paths', $this->secondary_paths);
        }
        if ($this->primary_retry_interval != "") {
            $setRequest->setargument('primary-retry-interval', $this->primary_retry_interval);
        }
        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->bandwidth_limit != "") {
            $setRequest->setargument('bandwidth-limit', $this->bandwidth_limit);
        }
        if ($this->auto_bandwidth_range != "") {
            $setRequest->setargument('auto-bandwidth-range', $this->auto_bandwidth_range);
        }
        if ($this->auto_bandwidth_reserve != "") {
            $setRequest->setargument('auto-bandwidth-reserve', $this->auto_bandwidth_reserve);
        }
        if ($this->auto_bandwidth_avg_interval != "") {
            $setRequest->setargument('auto-bandwidth-avg-interval', $this->auto_bandwidth_avg_interval);
        }
        if ($this->auto_bandwidth_update_interval != "") {
            $setRequest->setargument('auto-bandwidth-update-interval', $this->auto_bandwidth_update_interval);
        }


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

}
