<?php

namespace app\models\mikrotik\switchm;

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 $id;
    public $switch;
    public $vlan_id;
    public $ports;
    public $independent_learning;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['switch', 'string'],
            ['vlan_id', 'string'],
            ['independent_learning', 'string'],
            ['invalid', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/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->switch = $response->getProperty('switch');
                $c->vlan_id = $response->getProperty('vlan-id');
                $c->ports = $response->getProperty('ports');
                $c->independent_learning = $response->getProperty('independent-learning');
                $c->invalid = $response->getProperty('invalid');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/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 = $response->getProperty('.id');
                $this->switch = $response->getProperty('switch');
                $this->vlan_id = $response->getProperty('vlan-id');
                $this->ports = $response->getProperty('ports');
                $this->independent_learning = (($response->getProperty('independent-learning') == 'true') ? true : false);
                $this->invalid = (($response->getProperty('invalid') == '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/ethernet/switch/vlan/set');
        $setRequest->setargument('.id', $id);

        if ($this->switch != $c->switch) {
            $setRequest->setargument('switch', $this->switch);
        }
        if ($this->vlan_id != $c->vlan_id) {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        if ($this->ports != $c->ports) {
            $setRequest->setargument('ports', $this->ports);
        }

        $setRequest->setargument('independent-learning', (($this->independent_learning == 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/ethernet/switch/vlan/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ethernet/switch/vlan/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->switch != "") {
            $setRequest->setargument('switch', $this->switch);
        }
        if ($this->vlan_id != "") {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        if ($this->ports != "") {
            $setRequest->setargument('ports', $this->ports);
        }
        if ($this->independent_learning != "") {
            $setRequest->setargument('independent-learning', (($this->independent_learning == true) ? "true" : "false"));
        }
        if ($this->invalid != "") {
            $setRequest->setargument('invalid', (($this->invalid == 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/ethernet/switch/vlan');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
