<?php

namespace app\models\mikrotik\ip\services;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Services extends Model {

    public $id;
    public $name;
    public $port;
    public $address;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['port', 'string'],
            ['address', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/service/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Services();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->port = $response->getProperty('port');
                $c->address = $response->getProperty('address');
                $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('/ip/service/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->port = $response->getProperty('port');
                $this->address = $response->getProperty('address');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/service/set');
        $setRequest->setargument('.id', $id);

        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->invalid != "") {
            $setRequest->setargument('invalid', (($this->invalid == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

}
