<?php

namespace app\models\mikrotik\ip\socks;

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 Access extends Model {

    public $id;
    public $src_address;
    public $src_port;
    public $dst_address;
    public $dst_port;
    public $action;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['src_address', 'string'],
            ['src_port', 'string'],
            ['dst_address', 'string'],
            ['dst_port', 'string'],
            ['action', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/socks/access/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new access();
                $c->id = $response->getProperty('.id');
                $c->src_address = $response->getProperty('src-address');
                $c->src_port = $response->getProperty('src-port');
                $c->dst_address = $response->getProperty('dst-address');
                $c->dst_port = $response->getProperty('dst-port');
                $c->action = $response->getProperty('action');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/socks/access/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->src_address = $response->getProperty('src-address');
                $this->src_port = $response->getProperty('src-port');
                $this->dst_address = $response->getProperty('dst-address');
                $this->dst_port = $response->getProperty('dst-port');
                $this->action = $response->getProperty('action');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

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

        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->src_port != "") {
            $setRequest->setargument('src-port', $this->src_port);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        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/socks/access/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/socks/access/add');

        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->src_port != "") {
            $setRequest->setargument('src-port', $this->src_port);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip/socks/access');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
