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

    public $id;
    public $interface;
    public $mpls_mtu;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['mpls_mtu', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/interface/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Mplsinterface();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->mpls_mtu = $response->getProperty('mpls-mtu');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/mpls/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->mpls_mtu = $response->getProperty('mpls-mtu');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Mplsinterface();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/interface/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->mpls_mtu != $c->mpls_mtu) {
            $setRequest->setargument('mpls-mtu', $this->mpls_mtu);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/mpls/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/interface/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/mpls/interface/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->mpls_mtu != "") {
            $setRequest->setargument('mpls-mtu', $this->mpls_mtu);
        }
        $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/interface');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
