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

    public $id;
    public $name;
    public $type;
    public $mirror_source;
    public $mirror_target;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['type', 'string'],
            ['mirror_source', 'string'],
            ['mirror_target', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Switchm();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->type = $response->getProperty('type');
                $c->mirror_source = $response->getProperty('mirror-source');
                $c->mirror_target = $response->getProperty('mirror-target');
                //$c->switch_all_ports = $response->getProperty('switch-all-ports');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ethernet/switch/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->name = $response->getProperty('name');
                $this->type = $response->getProperty('type');
                $this->mirror_source = $response->getProperty('mirror-source');
                $this->mirror_target = $response->getProperty('mirror-target');
            }
        }
        return;
    }

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

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

        if ($this->mirror_source != $c->mirror_source) {
            $setRequest->setargument('mirror-source', $this->mirror_source);
        }
        if ($this->mirror_target != $c->mirror_target) {
            $setRequest->setargument('mirror-target', $this->mirror_target);
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

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

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

}
