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

    public $id;
    public $transport;
    public $send_targeted;
    public $dynamic;
    public $disabled;
    public $local_transport;
    public $peer;
    public $addresses;

    public function rules() {
        return [
            ['id', 'string'],
            ['transport', 'string'],
            ['send_targeted', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
            ['local_transport', 'string'],
            ['peer', 'string'],
            ['addresses', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/ldp/neighbor/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Ldpneighbor();
                $c->id = $response->getProperty('.id');
                $c->transport = $response->getProperty('transport');
                $c->send_targeted = $response->getProperty('send-targeted');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                $c->local_transport = $response->getProperty('local-transport');
                $c->peer = $response->getProperty('peer');
                $c->addresses = $response->getProperty('addresses');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/ldp/neighbor/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->transport = $response->getProperty('transport');
                $this->send_targeted = (($response->getProperty('send-targeted') == 'true') ? true : false);
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->local_transport = (($response->getProperty('local-transport') == 'true') ? true : false);
                $this->peer = (($response->getProperty('peer') == 'true') ? true : false);
                $this->addresses = (($response->getProperty('addresses') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Ldpneighbor();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/ldp/neighbor/set');
        $setRequest->setargument('.id', $id);
        if ($this->transport != $c->transport) {
            $setRequest->setargument('transport', $this->transport);
        }
        $setRequest->setargument('send-targeted', (($this->send_targeted == true) ? "true" : "false"));

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

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

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

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

}
