<?php

namespace app\models\mikrotik\ip\routes;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Routes extends Model {

    public $id;
    public $active;
    public $disabled;
    public $distance;
    public $dst_address;
    public $dynamic;
    public $gateway;
    public $gateway_status;
    public $check_gateway;
    public $pref_src;
    public $route_tag;
    public $routing_mark;
    public $scope;
    public $static;
    public $target_scope;
    public $type;
    public $vrf_interface;
    public $ospf_metric;
    public $ospf_type;

    public function rules() {
        return [
            ['id', 'string'],
            ['active', 'string'],
            ['disabled', 'string'],
            ['distance', 'string'],
            ['dst_address', 'ip', 'subnet' => null],
            ['dynamic', 'string'],
            ['gateway', 'string'],
            ['gateway_status', 'string'],
            ['check_gateway', 'string'],
            ['pref_src', 'ip'],
            ['route_tag', 'string'],
            ['routing_mark', 'string'],
            ['scope', 'string'],
            ['static', 'string'],
            ['target_scope', 'string'],
            ['vrf_interface', 'string'],
            ['ospf_metric', 'string'],
            ['ospf_type', 'string'],
            ['type', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Routes();
                $c->id = $response->getProperty('.id');
                $c->active = $response->getProperty('active');
                $c->disabled = $response->getProperty('disabled');
                $c->distance = $response->getProperty('distance');
                $c->dst_address = $response->getProperty('dst-address');
                $c->dynamic = $response->getProperty('dynamic');
                $c->gateway = $response->getProperty('gateway');
                $c->gateway_status = $response->getProperty('gateway-status');
                $c->check_gateway = $response->getProperty('check-gateway');
                $c->pref_src = $response->getProperty('pref-src');
                $c->route_tag = $response->getProperty('route-tag');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->scope = $response->getProperty('scope');
                $c->static = $response->getProperty('static');
                $c->target_scope = $response->getProperty('target-scope');
                $c->vrf_interface = $response->getProperty('vrf-interface');
                $c->ospf_metric = $response->getProperty('ospf-metric');
                $c->ospf_type = $response->getProperty('ospf-type');
                $c->type = $response->getProperty('type');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->active = (($response->getProperty('active') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->distance = $response->getProperty('distance');
                $this->dst_address = $response->getProperty('dst-address');
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->gateway = $response->getProperty('gateway');
                $this->gateway_status = $response->getProperty('gateway-status');
                $this->check_gateway = $response->getProperty('check-gateway');
                $this->pref_src = $response->getProperty('pref-src');
                $this->route_tag = $response->getProperty('route-tag');
                $this->routing_mark = $response->getProperty('routing-mark');
                $this->scope = $response->getProperty('scope');
                $this->static = (($response->getProperty('static') == 'true') ? true : false);
                $this->target_scope = $response->getProperty('target-scope');
                $this->vrf_interface = $response->getProperty('vrf-interface');
                $this->ospf_metric = $response->getProperty('ospf-metric');
                $this->ospf_type = $response->getProperty('ospf-type');
                $this->type = $response->getProperty('type');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Routes();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/route/set');
        $setRequest->setargument('.id', $id);
        if ($this->active != $c->active) {
            $setRequest->setargument('active', $this->active);
        }


        if ($this->dst_address != $c->dst_address) {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->gateway_status != $c->gateway_status) {
            $setRequest->setargument('gateway-status', $this->gateway_status);
        }
        if ($this->route_tag != $c->route_tag) {
            $setRequest->setargument('route-tag', $this->route_tag);
        }

        if ($this->scope != $c->scope) {
            $setRequest->setargument('scope', $this->scope);
        }
        if ($this->static != $c->static) {
            $setRequest->setargument('static', $this->static);
        }
        if ($this->target_scope != $c->target_scope) {
            $setRequest->setargument('target-scope', $this->target_scope);
        }
        if ($this->type != $c->type) {
            $setRequest->setargument('type', $this->type);
        }
        if ($this->vrf_interface != $c->vrf_interface) {
            $setRequest->setargument('vrf-interface', $this->vrf_interface);
        }
        if ($this->ospf_metric != $c->ospf_metric) {
            $setRequest->setargument('ospf-metric', $this->ospf_metric);
        }
        if ($this->ospf_type != $c->ospf_type) {
            $setRequest->setargument('ospf-type', $this->ospf_type);
        }

        if ($this->gateway != $c->gateway) {
            if ($this->gateway == '') {
                $unset[] = 'gateway';
            } else {
                $setRequest->setargument('gateway', $this->gateway);
            }
        }
        if ($this->distance != $c->distance) {
            if ($this->distance == '') {
                $unset[] = 'distance';
            } else {
                $setRequest->setargument('distance', $this->distance);
            }
        }
        if ($this->pref_src != $c->pref_src) {
            if ($this->pref_src == '') {
                $unset[] = 'pref-src';
            } else {
                $setRequest->setargument('pref-src', $this->pref_src);
            }
        }
        if ($this->routing_mark != $c->routing_mark) {
            if ($this->routing_mark == '') {
                $unset[] = 'routing-mark';
            } else {
                $setRequest->setargument('routing-mark', $this->routing_mark);
            }
        }
        if ($this->check_gateway != $c->check_gateway) {
            if ($this->check_gateway == '') {
                $unset[] = 'check-gateway';
            } else {
                $setRequest->setargument('check-gateway', $this->check_gateway);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/route/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('/ip/route/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/route/add');

        if ($this->active != "") {
            $setRequest->setargument('active', (($this->active == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        if ($this->distance != "") {
            $setRequest->setargument('distance', $this->distance);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->gateway != "") {
            $setRequest->setargument('gateway', $this->gateway);
        }

        if ($this->check_gateway != "") {
            $setRequest->setargument('check-gateway', $this->check_gateway);
        }
        if ($this->pref_src != "") {
            $setRequest->setargument('pref-src', $this->pref_src);
        }
        if ($this->route_tag != "") {
            $setRequest->setargument('route-tag', $this->route_tag);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->scope != "") {
            $setRequest->setargument('scope', $this->scope);
        }
        if ($this->static != "") {
            $setRequest->setargument('static', (($this->static == true) ? "true" : "false"));
        }
        if ($this->target_scope != "") {
            $setRequest->setargument('target-scope', $this->target_scope);
        }
        if ($this->vrf_interface != "") {
            $setRequest->setargument('vrf-interface', $this->vrf_interface);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }
        $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('/ip/route');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
