<?php

namespace app\models\mikrotik\mpls\mpls;

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 Ldpinterface extends Model {

    public $id;
    public $interface;
    public $hello_interval;
    public $hold_time;
    public $transport_address;
    public $accept_dynamic_neighbors;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['hello_interval', 'string'],
            ['hold_time', 'string'],
            ['transport_address', 'string'],
            ['accept_dynamic_neighbors', 'string'],
            ['invalid', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/ldp/interface/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Ldpinterface();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->hello_interval = $response->getProperty('hello-interval');
                $c->hold_time = $response->getProperty('hold-time');
                $c->transport_address = $response->getProperty('transport-address');
                $c->accept_dynamic_neighbors = $response->getProperty('accept-dynamic-neighbors');
                $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('/mpls/ldp/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->hello_interval = $response->getProperty('hello-interval');
                $this->hold_time = $response->getProperty('hold-time');
                $this->transport_address = $response->getProperty('transport-address');
                $this->accept_dynamic_neighbors = (($response->getProperty('accept-dynamic-neighbors') == '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 Ldpinterface();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/ldp/interface/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->hello_interval != $c->hello_interval) {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->hold_time != $c->hold_time) {
            $setRequest->setargument('hold-time', $this->hold_time);
        }
        if ($this->transport_address != $c->transport_address) {
            $setRequest->setargument('transport-address', $this->transport_address);
        }
        $setRequest->setargument('accept-dynamic-neighbors', (($this->accept_dynamic_neighbors == true) ? "true" : "false"));

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/ldp/interface/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->hello_interval != "") {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->hold_time != "") {
            $setRequest->setargument('hold-time', $this->hold_time);
        }
        if ($this->transport_address != "") {
            $setRequest->setargument('transport-address', $this->transport_address);
        }
        if ($this->accept_dynamic_neighbors != "") {
            $setRequest->setargument('accept-dynamic-neighbors', (($this->accept_dynamic_neighbors == 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('/mpls/ldp/interface');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
