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

    public $id;
    public $interface = 'all';
    public $cost;
    public $priority;
    public $authentication;
    public $authentication_key;
    public $authentication_key_id;
    public $network_type;
    public $instance_id;
    public $retransmit_interval;
    public $transmit_delay;
    public $hello_interval;
    public $dead_interval;
    public $use_bfd;
    public $inactive;
    public $disabled;
    public $passive;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['cost', 'string'],
            ['priority', 'string'],
            ['authentication', 'string'],
            ['authentication_key', 'string'],
            ['authentication_key_id', 'string'],
            ['network_type', 'string'],
            ['instance_id', 'string'],
            ['retransmit_interval', 'string'],
            ['transmit_delay', 'string'],
            ['hello_interval', 'string'],
            ['dead_interval', 'string'],
            ['use_bfd', 'string'],
            ['inactive', 'string'],
            ['disabled', 'string'],
            ['passive', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/interface/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Interfaces();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->cost = $response->getProperty('cost');
                $c->priority = $response->getProperty('priority');
                $c->authentication = $response->getProperty('authentication');
                $c->authentication_key = $response->getProperty('authentication-key');
                $c->authentication_key_id = $response->getProperty('authentication-key-id');
                $c->network_type = $response->getProperty('network-type');
                $c->instance_id = $response->getProperty('instance-id');
                $c->retransmit_interval = $response->getProperty('retransmit-interval');
                $c->transmit_delay = $response->getProperty('transmit-delay');
                $c->hello_interval = $response->getProperty('hello-interval');
                $c->dead_interval = $response->getProperty('dead-interval');
                $c->use_bfd = $response->getProperty('use-bfd');
                $c->inactive = $response->getProperty('inactive');
                $c->disabled = $response->getProperty('disabled');
                $c->passive = $response->getProperty('passive');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/interface/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->interface = $response->getProperty('interface');
                $this->cost = $response->getProperty('cost');
                $this->priority = $response->getProperty('priority');
                $this->authentication = $response->getProperty('authentication');
                $this->authentication_key = $response->getProperty('authentication-key');
                $this->authentication_key_id = $response->getProperty('authentication-key-id');
                $this->network_type = $response->getProperty('network-type');
                $this->instance_id = $response->getProperty('instance-id');
                $this->retransmit_interval = $response->getProperty('retransmit-interval');
                $this->transmit_delay = $response->getProperty('transmit-delay');
                $this->hello_interval = $response->getProperty('hello-interval');
                $this->dead_interval = $response->getProperty('dead-interval');
                $this->use_bfd = (($response->getProperty('use-bfd') == 'true') ? true : false);
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->passive = (($response->getProperty('passive') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Interfaces();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/interface/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->cost != $c->cost) {
            $setRequest->setargument('cost', $this->cost);
        }
        if ($this->priority != $c->priority) {
            $setRequest->setargument('priority', $this->priority);
        }
        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->network_type != $c->network_type) {
            $setRequest->setargument('network-type', $this->network_type);
        }
        if ($this->instance_id != $c->instance_id) {
            $setRequest->setargument('instance-id', $this->instance_id);
        }
        if ($this->retransmit_interval != $c->retransmit_interval) {
            $setRequest->setargument('retransmit-interval', $this->retransmit_interval);
        }
        if ($this->transmit_delay != $c->transmit_delay) {
            $setRequest->setargument('transmit-delay', $this->transmit_delay);
        }
        if ($this->hello_interval != $c->hello_interval) {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->dead_interval != $c->dead_interval) {
            $setRequest->setargument('dead-interval', $this->dead_interval);
        }
        $setRequest->setargument('use-bfd', (($this->use_bfd == true) ? "true" : "false"));

        $setRequest->setargument('passive', (($this->passive == true) ? "true" : "false"));
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/ospf/interface/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/interface/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/interface/add');
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->cost != "") {
            $setRequest->setargument('cost', $this->cost);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        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->network_type != "") {
            $setRequest->setargument('network-type', $this->network_type);
        }
        if ($this->instance_id != "") {
            $setRequest->setargument('instance-id', $this->instance_id);
        }
        if ($this->retransmit_interval != "") {
            $setRequest->setargument('retransmit-interval', $this->retransmit_interval);
        }
        if ($this->transmit_delay != "") {
            $setRequest->setargument('transmit-delay', $this->transmit_delay);
        }
        if ($this->hello_interval != "") {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->dead_interval != "") {
            $setRequest->setargument('dead-interval', $this->dead_interval);
        }
        if ($this->use_bfd != "") {
            $setRequest->setargument('use-bfd', (($this->use_bfd == true) ? "true" : "false"));
        }

        if ($this->passive != "") {
            $setRequest->setargument('passive', (($this->passive == 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/interface');
        $util->remove(RouterOS\Query::where('.id', $id));
        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;
    }

}
