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

    public $id;
    public $switch;
    public $mac_address;
    public $ports;
    public $copy_to_cpu;
    public $redirect_to_cpu;
    public $drop;
    public $mirror;
    public $vlan_id;
    public $share_vlan_learned;

    public function rules() {
        return [
            ['id', 'string'],
            ['switch', 'string'],
            ['mac_address', 'string'],
            ['copy_to_cpu', 'string'],
            ['redirect_to_cpu', 'string'],
            ['drop', 'string'],
            ['mirror', 'string'],
            ['vlan_id', 'string'],
            ['share_vlan_learned', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/host/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Host();
                $c->id = $response->getProperty('.id');
                $c->switch = $response->getProperty('switch');
                $c->mac_address = $response->getProperty('mac-address');
                $c->ports = $response->getProperty('ports');
                $c->copy_to_cpu = $response->getProperty('copy-to-cpu');
                $c->redirect_to_cpu = $response->getProperty('redirect-to-cpu');
                $c->drop = $response->getProperty('drop');
                $c->mirror = $response->getProperty('mirror');
                $c->vlan_id = $response->getProperty('vlan-id');
                $c->share_vlan_learned = $response->getProperty('share-vlan-learned');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/host/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->switch = $response->getProperty('switch');
                $this->mac_address = $response->getProperty('mac-address');
                $this->ports = $response->getProperty('ports');
                $this->copy_to_cpu = (($response->getProperty('copy-to-cpu') == 'true') ? true : false);
                $this->redirect_to_cpu = (($response->getProperty('redirect-to-cpu') == 'true') ? true : false);
                $this->drop = (($response->getProperty('drop') == 'true') ? true : false);
                $this->mirror = (($response->getProperty('mirror') == 'true') ? true : false);
                $this->vlan_id = $response->getProperty('vlan-id');
                $this->share_vlan_learned = (($response->getProperty('share-vlan-learned') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Host();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ethernet/switch/host/set');
        $setRequest->setargument('.id', $id);
        if ($this->switch != $c->switch) {

            $setRequest->setargument('switch', $this->switch);
        }
        if ($this->mac_address != $c->mac_address) {

            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->ports != $c->ports) {

            $setRequest->setargument('ports', $this->ports);
        }
        $setRequest->setargument('copy-to-cpu', (($this->copy_to_cpu == true) ? "true" : "false"));
        $setRequest->setargument('redirect-to-cpu', (($this->redirect_to_cpu == true) ? "true" : "false"));
        $setRequest->setargument('drop', (($this->drop == true) ? "true" : "false"));
        $setRequest->setargument('mirror', (($this->mirror == true) ? "true" : "false"));
        if ($this->vlan_id != $c->vlan_id) {

            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        $setRequest->setargument('share-vlan-learned', (($this->share_vlan_learned == true) ? "true" : "false"));

        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ethernet/switch/host/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->switch != "") {
            $setRequest->setargument('switch', $this->switch);
        }
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->ports != "") {
            $setRequest->setargument('ports', $this->ports);
        }
        if ($this->copy_to_cpu != "") {
            $setRequest->setargument('copy-to-cpu', (($this->copy_to_cpu == true) ? "true" : "false"));
        }
        if ($this->redirect_to_cpu != "") {
            $setRequest->setargument('redirect-to-cpu', (($this->redirect_to_cpu == true) ? "true" : "false"));
        }
        if ($this->drop != "") {
            $setRequest->setargument('drop', (($this->drop == true) ? "true" : "false"));
        }
        if ($this->mirror != "") {
            $setRequest->setargument('mirror', (($this->mirror == true) ? "true" : "false"));
        }
        if ($this->vlan_id != "") {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        if ($this->share_vlan_learned != "") {
            $setRequest->setargument('share-vlan-learned', (($this->share_vlan_learned == 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/host');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
