<?php

namespace app\models\mikrotik\bridge;

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 $bridge;
    public $priority;
    public $path_cost;
    public $edge;
    public $point_to_point;
    public $external_fdb;
    public $horizon;
    public $auto_isolate;
    public $status;
    public $inactive;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['bridge', 'string'],
            ['priority', 'string'],
            ['path_cost', 'string'],
            ['edge', 'string'],
            ['point_to_point', 'string'],
            ['external_fdb', 'string'],
            ['horizon', 'string'],
            ['auto_isolate', 'string'],
            ['status', 'string'],
            ['inactive', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bridge/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->bridge = $response->getProperty('bridge');
                $c->priority = $response->getProperty('priority');
                $c->path_cost = $response->getProperty('path-cost');
                $c->edge = $response->getProperty('edge');
                $c->point_to_point = $response->getProperty('point-to-point');
                $c->external_fdb = $response->getProperty('external-fdb');
                $c->horizon = $response->getProperty('horizon');
                $c->auto_isolate = $response->getProperty('auto-isolate');
                $c->status = $response->getProperty('status');
                $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/bridge/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 = $id;
                $this->interface = $response->getProperty('interface');
                $this->bridge = $response->getProperty('bridge');
                $this->priority = $response->getProperty('priority');
                $this->path_cost = $response->getProperty('path-cost');
                $this->edge = $response->getProperty('edge');
                $this->point_to_point = $response->getProperty('point-to-point');
                $this->external_fdb = $response->getProperty('external-fdb');
                $this->horizon = $response->getProperty('horizon');
                $this->auto_isolate = (($response->getProperty('auto-isolate') == 'true') ? true : false);
                $this->status = $response->getProperty('status');
                $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) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/bridge/port/set');
        $setRequest->setargument('.id', $id);

        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->bridge != "") {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->path_cost != "") {
            $setRequest->setargument('path-cost', $this->path_cost);
        }
        if ($this->edge != "") {
            $setRequest->setargument('edge', $this->edge);
        }
        if ($this->point_to_point != "") {
            $setRequest->setargument('point-to-point', $this->point_to_point);
        }
        if ($this->external_fdb != "") {
            $setRequest->setargument('external-fdb', $this->external_fdb);
        }

        $setRequest->setargument('horizon', $this->horizon);

        if ($this->auto_isolate != "") {
            $setRequest->setargument('auto-isolate', (($this->auto_isolate == true) ? "true" : "false"));
        }
        if ($this->status != "") {
            $setRequest->setargument('status', $this->status);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == true) ? "true" : "false"));
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

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

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

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

        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->bridge != "") {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->path_cost != "") {
            $setRequest->setargument('path-cost', $this->path_cost);
        }
        if ($this->edge != "") {
            $setRequest->setargument('edge', $this->edge);
        }
        if ($this->point_to_point != "") {
            $setRequest->setargument('point-to-point', $this->point_to_point);
        }
        if ($this->external_fdb != "") {
            $setRequest->setargument('external-fdb', $this->external_fdb);
        }
        if ($this->horizon != "") {
            $setRequest->setargument('horizon', $this->horizon);
        }
        if ($this->auto_isolate != "") {
            $setRequest->setargument('auto-isolate', (($this->auto_isolate == true) ? "true" : "false"));
        }
        if ($this->status != "") {
            $setRequest->setargument('status', $this->status);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == true) ? "true" : "false"));
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == 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('/interface/bridge/port');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
