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

    public $type = 'Bridge';
    public $id;
    public $name;
    public $mtu = '1500';
    public $actual_mtu;
    public $l2mtu;
    public $arp = 'enabled';
    public $mac_address;
    public $protocol_mode;
    public $priority;
    public $auto_mac;
    public $admin_mac;
    public $max_message_age;
    public $forward_delay = '15s';
    public $transmit_hold_count = '6';
    public $ageing_time = '5m';
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['actual_mtu', 'string'],
            ['l2mtu', 'string'],
            ['arp', 'string'],
            ['mac_address', 'string'],
            ['protocol_mode', 'string'],
            ['priority', 'string'],
            ['auto_mac', 'string'],
            ['admin_mac', 'string'],
            ['max_message_age', 'string'],
            ['forward_delay', 'string'],
            ['transmit_hold_count', 'string'],
            ['ageing_time', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getNames() {

        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bridge/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $ret = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bridge/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Bridge();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->actual_mtu = $response->getProperty('actual-mtu');
                $c->l2mtu = $response->getProperty('l2mtu');
                $c->arp = $response->getProperty('arp');
                $c->mac_address = $response->getProperty('mac-address');
                $c->protocol_mode = $response->getProperty('protocol-mode');
                $c->priority = $response->getProperty('priority');
                $c->auto_mac = $response->getProperty('auto-mac');
                $c->admin_mac = $response->getProperty('admin-mac');
                $c->max_message_age = $response->getProperty('max-message-age');
                $c->forward_delay = $response->getProperty('forward-delay');
                $c->transmit_hold_count = $response->getProperty('transmit-hold-count');
                $c->ageing_time = $response->getProperty('ageing-time');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bridge/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->name = $response->getProperty('name');
                $this->mtu = $response->getProperty('mtu');
                $this->actual_mtu = $response->getProperty('actual-mtu');
                $this->l2mtu = $response->getProperty('l2mtu');
                $this->arp = $response->getProperty('arp');
                $this->mac_address = $response->getProperty('mac-address');
                $this->protocol_mode = $response->getProperty('protocol-mode');
                $this->priority = $response->getProperty('priority');
                $this->auto_mac = (($response->getProperty('auto-mac') == 'true') ? true : false);
                $this->admin_mac = $response->getProperty('admin-mac');
                $this->max_message_age = $response->getProperty('max-message-age');
                $this->forward_delay = $response->getProperty('forward-delay');
                $this->transmit_hold_count = $response->getProperty('transmit-hold-count');
                $this->ageing_time = $response->getProperty('ageing-time');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Bridge();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/bridge/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != $c->mtu) {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->arp != $c->arp) {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->protocol_mode != $c->protocol_mode) {
            $setRequest->setargument('protocol-mode', $this->protocol_mode);
        }
        if ($this->priority != $c->priority) {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->admin_mac != $c->admin_mac) {
            $setRequest->setargument('admin-mac', $this->admin_mac);
        }
        if ($this->max_message_age != $c->max_message_age) {
            $setRequest->setargument('max-message-age', $this->max_message_age);
        }
        if ($this->forward_delay != $c->forward_delay) {
            $setRequest->setargument('forward-delay', $this->forward_delay);
        }
        if ($this->transmit_hold_count != $c->transmit_hold_count) {
            $setRequest->setargument('transmit-hold-count', $this->transmit_hold_count);
        }
        if ($this->ageing_time != $c->ageing_time) {
            $setRequest->setargument('ageing-time', $this->ageing_time);
        }

        $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/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

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

        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != "") {
            $setRequest->setargument('mtu', $this->mtu);
        }

        if ($this->arp != "") {
            $setRequest->setargument('arp', $this->arp);
        }

        if ($this->protocol_mode != "") {
            $setRequest->setargument('protocol-mode', $this->protocol_mode);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->auto_mac != "") {
            $setRequest->setargument('auto-mac', (($this->auto_mac == true) ? "true" : "false"));
        }
        if ($this->admin_mac != "") {
            $setRequest->setargument('admin-mac', $this->admin_mac);
        }
        if ($this->max_message_age != "") {
            $setRequest->setargument('max-message-age', $this->max_message_age);
        }
        if ($this->forward_delay != "") {
            $setRequest->setargument('forward-delay', $this->forward_delay);
        }
        if ($this->transmit_hold_count != "") {
            $setRequest->setargument('transmit-hold-count', $this->transmit_hold_count);
        }
        if ($this->ageing_time != "") {
            $setRequest->setargument('ageing-time', $this->ageing_time);
        }

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

}
