<?php

namespace app\models\mikrotik\routing\ospf;

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 $router_id;
    public $distribute_default;
    public $redistribute_connected;
    public $redistribute_static;
    public $redistribute_rip;
    public $redistribute_bgp;
    public $redistribute_other_ospf;
    public $metric_default;
    public $metric_connected;
    public $metric_static;
    public $metric_rip;
    public $metric_bgp;
    public $metric_other_ospf;
    public $mpls_te_area;
    public $mpls_te_router_id;
    public $in_filter;
    public $out_filter;
    public $routing_table;
    public $state;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['router_id', 'string'],
            ['distribute_default', 'string'],
            ['redistribute_connected', 'string'],
            ['redistribute_static', 'string'],
            ['redistribute_rip', 'string'],
            ['redistribute_bgp', 'string'],
            ['redistribute_other_ospf', 'string'],
            ['metric_default', 'string'],
            ['metric_connected', 'string'],
            ['metric_static', 'string'],
            ['metric_rip', 'string'],
            ['metric_bgp', 'string'],
            ['metric_other_ospf', 'string'],
            ['mpls_te_area', 'string'],
            ['mpls_te_router_id', 'string'],
            ['in_filter', 'string'],
            ['out_filter', 'string'],
            ['routing_table', 'string'],
            ['state', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/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->router_id = $response->getProperty('router-id');
                $c->distribute_default = $response->getProperty('distribute-default');
                $c->redistribute_connected = $response->getProperty('redistribute-connected');
                $c->redistribute_static = $response->getProperty('redistribute-static');
                $c->redistribute_rip = $response->getProperty('redistribute-rip');
                $c->redistribute_bgp = $response->getProperty('redistribute-bgp');
                $c->redistribute_other_ospf = $response->getProperty('redistribute-other-ospf');
                $c->metric_default = $response->getProperty('metric-default');
                $c->metric_connected = $response->getProperty('metric-connected');
                $c->metric_static = $response->getProperty('metric-static');
                $c->metric_rip = $response->getProperty('metric-rip');
                $c->metric_bgp = $response->getProperty('metric-bgp');
                $c->metric_other_ospf = $response->getProperty('metric-other-ospf');
                $c->mpls_te_area = $response->getProperty('mpls-te-area');
                $c->mpls_te_router_id = $response->getProperty('mpls-te-router-id');
                $c->in_filter = $response->getProperty('in-filter');
                $c->out_filter = $response->getProperty('out-filter');
                $c->routing_table = $response->getProperty('routing-table');
                $c->state = $response->getProperty('state');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/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->router_id = $response->getProperty('router-id');
                $this->distribute_default = $response->getProperty('distribute-default');
                $this->redistribute_connected = $response->getProperty('redistribute-connected');
                $this->redistribute_static = $response->getProperty('redistribute-static');
                $this->redistribute_rip = $response->getProperty('redistribute-rip');
                $this->redistribute_bgp = $response->getProperty('redistribute-bgp');
                $this->redistribute_other_ospf = $response->getProperty('redistribute-other-ospf');
                $this->metric_default = $response->getProperty('metric-default');
                $this->metric_connected = $response->getProperty('metric-connected');
                $this->metric_static = $response->getProperty('metric-static');
                $this->metric_rip = $response->getProperty('metric-rip');
                $this->metric_bgp = $response->getProperty('metric-bgp');
                $this->metric_other_ospf = $response->getProperty('metric-other-ospf');
                $this->mpls_te_area = $response->getProperty('mpls-te-area');
                $this->mpls_te_router_id = $response->getProperty('mpls-te-router-id');
                $this->in_filter = $response->getProperty('in-filter');
                $this->out_filter = $response->getProperty('out-filter');
                $this->routing_table = $response->getProperty('routing-table');
                $this->state = $response->getProperty('state');
                $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/ospf/instance/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->router_id != $c->router_id) {
            $setRequest->setargument('router-id', $this->router_id);
        }
        if ($this->distribute_default != $c->distribute_default) {
            $setRequest->setargument('distribute-default', $this->distribute_default);
        }
        if ($this->redistribute_connected != $c->redistribute_connected) {
            $setRequest->setargument('redistribute-connected', $this->redistribute_connected);
        }
        if ($this->redistribute_static != $c->redistribute_static) {
            $setRequest->setargument('redistribute-static', $this->redistribute_static);
        }
        if ($this->redistribute_rip != $c->redistribute_rip) {
            $setRequest->setargument('redistribute-rip', $this->redistribute_rip);
        }
        if ($this->redistribute_bgp != $c->redistribute_bgp) {
            $setRequest->setargument('redistribute-bgp', $this->redistribute_bgp);
        }
        if ($this->redistribute_other_ospf != $c->redistribute_other_ospf) {
            $setRequest->setargument('redistribute-other-ospf', $this->redistribute_other_ospf);
        }
        if ($this->metric_default != $c->metric_default) {
            $setRequest->setargument('metric-default', $this->metric_default);
        }
        if ($this->metric_connected != $c->metric_connected) {
            $setRequest->setargument('metric-connected', $this->metric_connected);
        }
        if ($this->metric_static != $c->metric_static) {
            $setRequest->setargument('metric-static', $this->metric_static);
        }
        if ($this->metric_rip != $c->metric_rip) {
            $setRequest->setargument('metric-rip', $this->metric_rip);
        }
        if ($this->metric_bgp != $c->metric_bgp) {
            $setRequest->setargument('metric-bgp', $this->metric_bgp);
        }
        if ($this->metric_other_ospf != $c->metric_other_ospf) {
            $setRequest->setargument('metric-other-ospf', $this->metric_other_ospf);
        }
        if ($this->mpls_te_area != $c->mpls_te_area) {
            if ($this->mpls_te_area == '') {
                $unset[] = 'mpls-te-area';
            } else {
                $setRequest->setargument('mpls-te-area', $this->mpls_te_area);
            }
        }
        if ($this->mpls_te_router_id != $c->mpls_te_router_id) {
            if ($this->mpls_te_router_id == '') {
                $unset[] = 'mpls-te-router-id';
            } else {
                $setRequest->setargument('mpls-te-router-id', $this->mpls_te_router_id);
            }
        }
        if ($this->in_filter != $c->in_filter) {
            $setRequest->setargument('in-filter', $this->in_filter);
        }
        if ($this->out_filter != $c->out_filter) {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        if ($this->routing_table != $c->routing_table) {
            if ($this->routing_table == '') {
                $unset[] = 'routing-table';
            } else {
                $setRequest->setargument('routing-table', $this->routing_table);
            }
        }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/instance/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->router_id != "") {
            $setRequest->setargument('router-id', $this->router_id);
        }
        if ($this->distribute_default != "") {
            $setRequest->setargument('distribute-default', $this->distribute_default);
        }
        if ($this->redistribute_connected != "") {
            $setRequest->setargument('redistribute-connected', $this->redistribute_connected);
        }
        if ($this->redistribute_static != "") {
            $setRequest->setargument('redistribute-static', $this->redistribute_static);
        }
        if ($this->redistribute_rip != "") {
            $setRequest->setargument('redistribute-rip', $this->redistribute_rip);
        }
        if ($this->redistribute_bgp != "") {
            $setRequest->setargument('redistribute-bgp', $this->redistribute_bgp);
        }
        if ($this->redistribute_other_ospf != "") {
            $setRequest->setargument('redistribute-other-ospf', $this->redistribute_other_ospf);
        }
        if ($this->metric_default != "") {
            $setRequest->setargument('metric-default', $this->metric_default);
        }
        if ($this->metric_connected != "") {
            $setRequest->setargument('metric-connected', $this->metric_connected);
        }
        if ($this->metric_static != "") {
            $setRequest->setargument('metric-static', $this->metric_static);
        }
        if ($this->metric_rip != "") {
            $setRequest->setargument('metric-rip', $this->metric_rip);
        }
        if ($this->metric_bgp != "") {
            $setRequest->setargument('metric-bgp', $this->metric_bgp);
        }
        if ($this->metric_other_ospf != "") {
            $setRequest->setargument('metric-other-ospf', $this->metric_other_ospf);
        }
        if ($this->mpls_te_area != "") {
            $setRequest->setargument('mpls-te-area', $this->mpls_te_area);
        }
        if ($this->mpls_te_router_id != "") {
            $setRequest->setargument('mpls-te-router-id', $this->mpls_te_router_id);
        }
        if ($this->in_filter != "") {
            $setRequest->setargument('in-filter', $this->in_filter);
        }
        if ($this->out_filter != "") {
            $setRequest->setargument('out-filter', $this->out_filter);
        }
        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/ospf/instance');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getAreanames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/area/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 names() {

        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

}
