<?php

namespace app\models\mikrotik;

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 $default_name;
    public $mtu;
    public $l2mtu;
    public $mac_address;
    public $orig_mac_address;
    public $arp;
    public $auto_negotiation;
    public $full_duplex;
    public $speed;
    public $master_port;
    public $bandwidth;
    public $switch;
    public $running;
    public $disabled;
    public $name;
    public $type = 'Ethernet';

    public function setInterface($id) {
        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/ethernet/getall');
        $printRequest->setQuery(RouterOS\Query::where('.id', $id));
        $response = $connect->getConnection()->sendSync($printRequest);

        $this->id = $id;
        $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');
        $this->full_duplex = $response->getProperty('full-duplex');
        $this->speed = $response->getProperty('speed');
        $this->master_port = $response->getProperty('master-port');
        $this->bandwidth = $response->getProperty('bandwidth');
        $this->switch = $response->getProperty('switch');
        $this->running = $response->getProperty('running');
        $this->disabled = $response->getProperty('disabled');
        $this->name = $response->getProperty('name');

        return;
    }

    public function getInterface() {
        $ret['.id'] = $this->id;
        $ret['default_name'] = $this->default_name;
        $ret['mtu'] = $this->mtu;
        $ret['l2mtu'] = $this->l2mtu;
        $ret['mac_address'] = $this->mac_address;
        $ret['orig_mac_address'] = $this->orig_mac_address;
        $ret['arp'] = $this->arp;
        $ret['auto_negotiation'] = $this->auto_negotiation;
        $ret['full_duplex'] = $this->full_duplex;
        $ret['speed'] = $this->speed;
        $ret['master_port'] = $this->master_port;
        $ret['bandwidth'] = $this->bandwidth;
        $ret['switch'] = $this->switch;
        $ret['running'] = $this->running;
        $ret['disabled'] = $this->disabled;
        $ret['name'] = $this->name;


        return $ret;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/ethernet/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);

        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret['.id'] = $response->getProperty('.id');
                $ret['default_name'] = $response->getProperty('default-name');
                $ret['mtu'] = $response->getProperty('mtu');
                $ret['l2mtu'] = $response->getProperty('l2mtu');
                $ret['mac_address'] = $response->getProperty('mac-address');
                $ret['orig_mac_address'] = $response->getProperty('orig-mac-address');
                $ret['arp'] = $response->getProperty('arp');
                $ret['auto_negotiation'] = $response->getProperty('auto-negotiation');
                $ret['full_duplex'] = $response->getProperty('full-duplex');
                $ret['speed'] = $response->getProperty('speed');
                $ret['master_port'] = $response->getProperty('master-port');
                $ret['bandwidth'] = $response->getProperty('bandwidth');
                $ret['switch'] = $response->getProperty('switch');
                $ret['running'] = $response->getProperty('running');
                $ret['disabled'] = $response->getProperty('disabled');
                $ret['name'] = $response->getProperty('name');
                $type[] = $ret;
            }
        }

        return $type;
    }

    public function disableEther($id) {
        if ($this->disabled == 'true') {
            $action = 'enable';
        } else {
            $action = 'disable';
        }
        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/interface/' . $action);
        $printRequest->setArgument('numbers', $id);
        $response = $connect->getConnection()->sendSync($printRequest);

        return $response;
    }

    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;
    }

    public function update($param) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/set');


        foreach ($param as $name => $value) {
            if($name == 'id')                continue;
            if($name == 'disabled')                continue;
            $getRequest->setArgument(str_replace ('_' , '-', $name), $value);
        }
        $getRequest->setArgument('numbers', $param['id']);
        $ret = $connect->getConnection()->sendSync($getRequest);
        return $ret;
    }

}
?> 
