<?php

namespace app\models\mikrotik\mesh;

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

    public $id;
    public $interface;
    public $mesh;
    public $path_cost;
    public $hello_interval;
    public $port_type;
    public $inactive;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['mesh', 'string'],
            ['path_cost', 'string'],
            ['hello_interval', 'string'],
            ['port_type', 'string'],
            ['inactive', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/mesh/port/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Port();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->mesh = $response->getProperty('mesh');
                $c->path_cost = $response->getProperty('path-cost');
                $c->hello_interval = $response->getProperty('hello-interval');
                $c->port_type = $response->getProperty('port-type');
                $c->inactive = $response->getProperty('inactive');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/mesh/port/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->mesh = $response->getProperty('mesh');
                $this->path_cost = $response->getProperty('path-cost');
                $this->hello_interval = $response->getProperty('hello-interval');
                $this->port_type = $response->getProperty('port-type');
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Port();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/mesh/port/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->mesh != $c->mesh) {
            $setRequest->setargument('mesh', $this->mesh);
        }
        if ($this->path_cost != $c->path_cost) {
            $setRequest->setargument('path-cost', $this->path_cost);
        }
        if ($this->hello_interval != $c->hello_interval) {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->port_type != $c->port_type) {
            $setRequest->setargument('port-type', $this->port_type);
        }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/mesh/port/add');

        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->mesh != "") {
            $setRequest->setargument('mesh', $this->mesh);
        }
        if ($this->path_cost != "") {
            $setRequest->setargument('path-cost', $this->path_cost);
        }
        if ($this->hello_interval != "") {
            $setRequest->setargument('hello-interval', $this->hello_interval);
        }
        if ($this->port_type != "") {
            $setRequest->setargument('port-type', $this->port_type);
        }

        $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('/interface/mesh/port');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
