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

    public $id;
    public $neighbor_id;
    public $transit_area;
    public $authentication;
    public $authentication_key;
    public $authentication_key_id;
    public $instance_id;
    public $use_bfd;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['neighbor_id', 'string'],
            ['transit_area', 'string'],
            ['authentication', 'string'],
            ['authentication_key', 'string'],
            ['authentication_key_id', 'string'],
            ['instance_id', 'string'],
            ['use_bfd', 'string'],
            ['invalid', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/virtual-link/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Links();
                $c->id = $response->getProperty('.id');
                $c->neighbor_id = $response->getProperty('neighbor-id');
                $c->transit_area = $response->getProperty('transit-area');
                $c->authentication = $response->getProperty('authentication');
                $c->authentication_key = $response->getProperty('authentication-key');
                $c->authentication_key_id = $response->getProperty('authentication-key-id');
                $c->instance_id = $response->getProperty('instance-id');
                $c->use_bfd = $response->getProperty('use-bfd');
                $c->invalid = $response->getProperty('invalid');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/virtual-link/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->neighbor_id = $response->getProperty('neighbor-id');
                $this->transit_area = $response->getProperty('transit-area');
                $this->authentication = $response->getProperty('authentication');
                $this->authentication_key = $response->getProperty('authentication-key');
                $this->authentication_key_id = $response->getProperty('authentication-key-id');
                $this->instance_id = $response->getProperty('instance-id');
                $this->use_bfd = (($response->getProperty('use-bfd') == 'true') ? true : false);
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Links();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/virtual-link/set');
        $setRequest->setargument('.id', $id);
        if ($this->neighbor_id != $c->neighbor_id) {
            $setRequest->setargument('neighbor-id', $this->neighbor_id);
        }
        if ($this->transit_area != $c->transit_area) {
            $setRequest->setargument('transit-area', $this->transit_area);
        }
        if ($this->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->authentication_key != $c->authentication_key) {
            $setRequest->setargument('authentication-key', $this->authentication_key);
        }
        if ($this->authentication_key_id != $c->authentication_key_id) {
            $setRequest->setargument('authentication-key-id', $this->authentication_key_id);
        }
        if ($this->instance_id != $c->instance_id) {
            $setRequest->setargument('instance-id', $this->instance_id);
        }
        $setRequest->setargument('use-bfd', (($this->use_bfd == true) ? "true" : "false"));
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/ospf/virtual-link/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/virtual-link/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/virtual-link/add');
        if ($this->neighbor_id != "") {
            $setRequest->setargument('neighbor-id', $this->neighbor_id);
        }
        if ($this->transit_area != "") {
            $setRequest->setargument('transit-area', $this->transit_area);
        }
        if ($this->authentication != "") {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->authentication_key != "") {
            $setRequest->setargument('authentication-key', $this->authentication_key);
        }
        if ($this->authentication_key_id != "") {
            $setRequest->setargument('authentication-key-id', $this->authentication_key_id);
        }
        if ($this->instance_id != "") {
            $setRequest->setargument('instance-id', $this->instance_id);
        }
        if ($this->use_bfd != "") {
            $setRequest->setargument('use-bfd', (($this->use_bfd == 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('/routing/ospf/virtual-link');
        $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;
    }

}
