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

    public $type = 'VLAN';
    public $id;
    public $name;
    public $mtu = '1500';
    public $mac_address;
    public $arp;
    public $vlan_id = '1';
    public $interface;
    public $use_service_tag;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['interface', 'string'],
            ['mac_address', 'string'],
            ['arp', 'string'],
            ['vlan_id', 'integer'],
            ['use_service_tag', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/vlan/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Vlan();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->mac_address = $response->getProperty('mac-address');
                $c->arp = $response->getProperty('arp');
                $c->vlan_id = $response->getProperty('vlan-id');
                $c->interface = $response->getProperty('interface');
                $c->use_service_tag = $response->getProperty('use-service-tag');
                $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/vlan/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->mac_address = $response->getProperty('mac-address');
                $this->arp = $response->getProperty('arp');
                $this->vlan_id = $response->getProperty('vlan-id');
                $this->interface = $response->getProperty('interface');
                $this->use_service_tag = (($response->getProperty('use-service-tag') == 'true') ? true : false);
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Vlan();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/vlan/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->mac_address != $c->mac_address) {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->arp != $c->arp) {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->vlan_id != $c->vlan_id) {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->use_service_tag != $c->use_service_tag) {
            $setRequest->setargument('use-service-tag', (($this->use_service_tag == 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/vlan/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/vlan/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->vlan_id != "") {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->use_service_tag != "") {
            $setRequest->setargument('use-service-tag', (($this->use_service_tag == 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/vlan');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
