<?php

namespace app\models\mikrotik\interfaces;

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

    public $id;
    public $name;
    public $default_name;
    public $mtu;
    public $l2mtu;
    public $mac_address;
    public $orig_mac_address;
    public $arp;
    public $auto_negotiation;
    public $advertise;
    public $full_duplex;
    public $tx_flow_control;
    public $rx_flow_control;
    public $speed;
    public $master_port;
    public $bandwidth;
    public $switch;
    public $driver_rx_byte;
    public $driver_rx_packet;
    public $driver_tx_byte;
    public $driver_tx_packet;
    public $rx_bytes;
    public $rx_too_short;
    public $rx_64;
    public $rx_65_127;
    public $rx_128_255;
    public $rx_256_511;
    public $rx_512_1023;
    public $rx_1024_1518;
    public $rx_1519_max;
    public $rx_too_long;
    public $rx_broadcast;
    public $rx_pause;
    public $rx_multicast;
    public $rx_fcs_error;
    public $rx_align_error;
    public $rx_fragment;
    public $rx_overflow;
    public $tx_bytes;
    public $tx_64;
    public $tx_65_127;
    public $tx_128_255;
    public $tx_256_511;
    public $tx_512_1023;
    public $tx_1024_1518;
    public $tx_1519_max;
    public $tx_too_long;
    public $tx_broadcast;
    public $tx_pause;
    public $tx_multicast;
    public $tx_underrun;
    public $tx_collision;
    public $tx_excessive_collision;
    public $tx_multiple_collision;
    public $tx_single_collision;
    public $tx_excessive_deferred;
    public $tx_deferred;
    public $tx_late_collision;
    public $running;
    public $disabled;
    public $type = 'Ethernet';

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['default_name', 'string'],
            ['mtu', 'string'],
            ['l2mtu', 'string'],
            ['mac_address', 'string'],
            ['orig_mac_address', 'string'],
            ['arp', 'string'],
            ['auto_negotiation', 'string'],
            ['full_duplex', 'string'],
            ['tx_flow_control', 'string'],
            ['rx_flow_control', 'string'],
            ['speed', 'string'],
            ['master_port', 'string'],
            ['bandwidth', 'string'],
            ['switch', 'string'],
            ['driver_rx_byte', 'string'],
            ['driver_rx_packet', 'string'],
            ['driver_tx_byte', 'string'],
            ['driver_tx_packet', 'string'],
            ['rx_bytes', 'string'],
            ['rx_too_short', 'string'],
            ['rx_64', 'string'],
            ['rx_65_127', 'string'],
            ['rx_128_255', 'string'],
            ['rx_256_511', 'string'],
            ['rx_512_1023', 'string'],
            ['rx_1024_1518', 'string'],
            ['rx_1519_max', 'string'],
            ['rx_too_long', 'string'],
            ['rx_broadcast', 'string'],
            ['rx_pause', 'string'],
            ['rx_multicast', 'string'],
            ['rx_fcs_error', 'string'],
            ['rx_align_error', 'string'],
            ['rx_fragment', 'string'],
            ['rx_overflow', 'string'],
            ['tx_bytes', 'string'],
            ['tx_64', 'string'],
            ['tx_65_127', 'string'],
            ['tx_128_255', 'string'],
            ['tx_256_511', 'string'],
            ['tx_512_1023', 'string'],
            ['tx_1024_1518', 'string'],
            ['tx_1519_max', 'string'],
            ['tx_too_long', 'string'],
            ['tx_broadcast', 'string'],
            ['tx_pause', 'string'],
            ['tx_multicast', 'string'],
            ['tx_underrun', 'string'],
            ['tx_collision', 'string'],
            ['tx_excessive_collision', 'string'],
            ['tx_multiple_collision', 'string'],
            ['tx_single_collision', 'string'],
            ['tx_excessive_deferred', 'string'],
            ['tx_deferred', 'string'],
            ['tx_late_collision', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Ethernet();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->default_name = $response->getProperty('default-name');
                $c->mtu = $response->getProperty('mtu');
                $c->l2mtu = $response->getProperty('l2mtu');
                $c->mac_address = $response->getProperty('mac-address');
                $c->orig_mac_address = $response->getProperty('orig-mac-address');
                $c->arp = $response->getProperty('arp');
                $c->auto_negotiation = $response->getProperty('auto-negotiation');
                $c->advertise = $response->getProperty('advertise');
                $c->full_duplex = $response->getProperty('full-duplex');
                $c->tx_flow_control = $response->getProperty('tx-flow-control');
                $c->rx_flow_control = $response->getProperty('rx-flow-control');
                $c->speed = $response->getProperty('speed');
                $c->master_port = $response->getProperty('master-port');
                $c->bandwidth = $response->getProperty('bandwidth');
                $c->switch = $response->getProperty('switch');
                $c->driver_rx_byte = $response->getProperty('driver-rx-byte');
                $c->driver_rx_packet = $response->getProperty('driver-rx-packet');
                $c->driver_tx_byte = $response->getProperty('driver-tx-byte');
                $c->driver_tx_packet = $response->getProperty('driver-tx-packet');
                $c->rx_bytes = $response->getProperty('rx-bytes');
                $c->rx_too_short = $response->getProperty('rx-too-short');
                $c->rx_64 = $response->getProperty('rx-64');
                $c->rx_65_127 = $response->getProperty('rx-65-127');
                $c->rx_128_255 = $response->getProperty('rx-128-255');
                $c->rx_256_511 = $response->getProperty('rx-256-511');
                $c->rx_512_1023 = $response->getProperty('rx-512-1023');
                $c->rx_1024_1518 = $response->getProperty('rx-1024-1518');
                $c->rx_1519_max = $response->getProperty('rx-1519-max');
                $c->rx_too_long = $response->getProperty('rx-too-long');
                $c->rx_broadcast = $response->getProperty('rx-broadcast');
                $c->rx_pause = $response->getProperty('rx-pause');
                $c->rx_multicast = $response->getProperty('rx-multicast');
                $c->rx_fcs_error = $response->getProperty('rx-fcs-error');
                $c->rx_align_error = $response->getProperty('rx-align-error');
                $c->rx_fragment = $response->getProperty('rx-fragment');
                $c->rx_overflow = $response->getProperty('rx-overflow');
                $c->tx_bytes = $response->getProperty('tx-bytes');
                $c->tx_64 = $response->getProperty('tx-64');
                $c->tx_65_127 = $response->getProperty('tx-65-127');
                $c->tx_128_255 = $response->getProperty('tx-128-255');
                $c->tx_256_511 = $response->getProperty('tx-256-511');
                $c->tx_512_1023 = $response->getProperty('tx-512-1023');
                $c->tx_1024_1518 = $response->getProperty('tx-1024-1518');
                $c->tx_1519_max = $response->getProperty('tx-1519-max');
                $c->tx_too_long = $response->getProperty('tx-too-long');
                $c->tx_broadcast = $response->getProperty('tx-broadcast');
                $c->tx_pause = $response->getProperty('tx-pause');
                $c->tx_multicast = $response->getProperty('tx-multicast');
                $c->tx_underrun = $response->getProperty('tx-underrun');
                $c->tx_collision = $response->getProperty('tx-collision');
                $c->tx_excessive_collision = $response->getProperty('tx-excessive-collision');
                $c->tx_multiple_collision = $response->getProperty('tx-multiple-collision');
                $c->tx_single_collision = $response->getProperty('tx-single-collision');
                $c->tx_excessive_deferred = $response->getProperty('tx-excessive-deferred');
                $c->tx_deferred = $response->getProperty('tx-deferred');
                $c->tx_late_collision = $response->getProperty('tx-late-collision');
                $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/ethernet/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->default_name = $response->getProperty('default-name');
                $this->mtu = $response->getProperty('mtu');
                $this->l2mtu = $response->getProperty('l2mtu');
                $this->mac_address = $response->getProperty('mac-address');
                $this->orig_mac_address = $response->getProperty('orig-mac-address');
                $this->arp = $response->getProperty('arp');
                $this->auto_negotiation = (($response->getProperty('auto-negotiation') == 'true') ? true : false);
                $this->advertise = $response->getProperty('advertise');
                $this->full_duplex = (($response->getProperty('full-duplex') == 'true') ? true : false);
                $this->tx_flow_control = $response->getProperty('tx-flow-control');
                $this->rx_flow_control = $response->getProperty('rx-flow-control');
                $this->speed = $response->getProperty('speed');
                $this->master_port = $response->getProperty('master-port');
                $this->bandwidth = $response->getProperty('bandwidth');
                $this->switch = $response->getProperty('switch');
                $this->driver_rx_byte = $response->getProperty('driver-rx-byte');
                $this->driver_rx_packet = $response->getProperty('driver-rx-packet');
                $this->driver_tx_byte = $response->getProperty('driver-tx-byte');
                $this->driver_tx_packet = $response->getProperty('driver-tx-packet');
                $this->rx_bytes = $response->getProperty('rx-bytes');
                $this->rx_too_short = $response->getProperty('rx-too-short');
                $this->rx_64 = $response->getProperty('rx-64');
                $this->rx_65_127 = $response->getProperty('rx-65-127');
                $this->rx_128_255 = $response->getProperty('rx-128-255');
                $this->rx_256_511 = $response->getProperty('rx-256-511');
                $this->rx_512_1023 = $response->getProperty('rx-512-1023');
                $this->rx_1024_1518 = $response->getProperty('rx-1024-1518');
                $this->rx_1519_max = $response->getProperty('rx-1519-max');
                $this->rx_too_long = $response->getProperty('rx-too-long');
                $this->rx_broadcast = $response->getProperty('rx-broadcast');
                $this->rx_pause = $response->getProperty('rx-pause');
                $this->rx_multicast = $response->getProperty('rx-multicast');
                $this->rx_fcs_error = $response->getProperty('rx-fcs-error');
                $this->rx_align_error = $response->getProperty('rx-align-error');
                $this->rx_fragment = $response->getProperty('rx-fragment');
                $this->rx_overflow = $response->getProperty('rx-overflow');
                $this->tx_bytes = $response->getProperty('tx-bytes');
                $this->tx_64 = $response->getProperty('tx-64');
                $this->tx_65_127 = $response->getProperty('tx-65-127');
                $this->tx_128_255 = $response->getProperty('tx-128-255');
                $this->tx_256_511 = $response->getProperty('tx-256-511');
                $this->tx_512_1023 = $response->getProperty('tx-512-1023');
                $this->tx_1024_1518 = $response->getProperty('tx-1024-1518');
                $this->tx_1519_max = $response->getProperty('tx-1519-max');
                $this->tx_too_long = $response->getProperty('tx-too-long');
                $this->tx_broadcast = $response->getProperty('tx-broadcast');
                $this->tx_pause = $response->getProperty('tx-pause');
                $this->tx_multicast = $response->getProperty('tx-multicast');
                $this->tx_underrun = $response->getProperty('tx-underrun');
                $this->tx_collision = $response->getProperty('tx-collision');
                $this->tx_excessive_collision = $response->getProperty('tx-excessive-collision');
                $this->tx_multiple_collision = $response->getProperty('tx-multiple-collision');
                $this->tx_single_collision = $response->getProperty('tx-single-collision');
                $this->tx_excessive_deferred = $response->getProperty('tx-excessive-deferred');
                $this->tx_deferred = $response->getProperty('tx-deferred');
                $this->tx_late_collision = $response->getProperty('tx-late-collision');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Ethernet();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ethernet/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->l2mtu != $c->l2mtu) {
            $setRequest->setargument('l2mtu', $this->l2mtu);
        }
        if ($this->arp != $c->arp) {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->auto_negotiation != $c->auto_negotiation) {
            $setRequest->setargument('auto-negotiation', (($this->auto_negotiation == true) ? "true" : "false"));
        }
        if ($this->advertise != $c->advertise) {
            $setRequest->setargument('advertise', $this->advertise);
        }
        if ($this->tx_flow_control != $c->tx_flow_control) {
            $setRequest->setargument('tx-flow-control', $this->tx_flow_control);
        }
        if ($this->rx_flow_control != $c->rx_flow_control) {
            $setRequest->setargument('rx-flow-control', $this->rx_flow_control);
        }
        if ($this->master_port != $c->master_port) {
            $setRequest->setargument('master-port', $this->master_port);
        }
        if ($this->bandwidth != $c->bandwidth) {
            $setRequest->setargument('bandwidth', $this->bandwidth);
        }
        $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/ethernet/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function getNames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/getall');
        $getRequest->setArgument('.proplist', 'name');
        $responses = $connect->getConnection()->sendSync($getRequest);

        foreach ($responses as $response) {
            if ($response->getProperty('name') != null) {
                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

}
