<?php

namespace app\models\mikrotik\ip\webproxy;

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 $action;
    public $dst_address;
    public $dst_host;
    public $dst_port;
    public $local_port;
    public $method;
    public $path;
    public $redirect_to;
    public $src_address;
    public $disabled;

    public function rules() {
        return [
            ['action', 'string'],
            ['dst_address', 'ip'],
            ['dst_host', 'string'],
            ['dst_port', 'integer'],
            ['local_port', 'integer'],
            ['method', 'string'],
            ['path', 'string'],
            ['redirect_to', 'string'],
            ['src_address', 'ip'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/proxy/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->action = $response->getProperty('action');
                $c->dst_address = $response->getProperty('dst-address');
                $c->dst_host = $response->getProperty('dst-host');
                $c->dst_port = $response->getProperty('dst-port');
                $c->local_port = $response->getProperty('local-port');
                $c->method = $response->getProperty('method');
                $c->path = $response->getProperty('path');
                $c->redirect_to = $response->getProperty('redirect-to');
                $c->src_address = $response->getProperty('src-address');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/proxy/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->action = $response->getProperty('action');
                $this->dst_address = $response->getProperty('dst-address');
                $this->dst_host = $response->getProperty('dst-host');
                $this->dst_port = $response->getProperty('dst-port');
                $this->local_port = $response->getProperty('local-port');
                $this->method = $response->getProperty('method');
                $this->path = $response->getProperty('path');
                $this->redirect_to = $response->getProperty('redirect-to');
                $this->src_address = $response->getProperty('src-address');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

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

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

        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }

        if ($this->dst_address != $c->dst_address) {
            if ($this->dst_address == '') {
                $unset[] = 'dst-address';
            } else {
                $setRequest->setargument('dst-address', $this->dst_address);
            }
        }
        if ($this->dst_host != $c->dst_host) {
            if ($this->dst_host == '') {
                $unset[] = 'dst-host';
            } else {
                $setRequest->setargument('dst-host', $this->dst_host);
            }
        }
        if ($this->local_port != $c->local_port) {
            if ($this->local_port == '') {
                $unset[] = 'local-port';
            } else {
                $setRequest->setargument('local-port', $this->local_port);
            }
        }
        if ($this->method != $c->method) {
            if ($this->method == '') {
                $unset[] = 'method';
            } else {
                $setRequest->setargument('method', $this->method);
            }
        }
        if ($this->path != $c->path) {
            if ($this->path == '') {
                $unset[] = 'path';
            } else {
                $setRequest->setargument('path', $this->path);
            }
        }
        if ($this->redirect_to != $c->redirect_to) {
            if ($this->redirect_to == '') {
                $unset[] = 'redirect-to';
            } else {
                $setRequest->setargument('redirect-to', $this->redirect_to);
            }
        }
        if ($this->src_address != $c->src_address) {
            if ($this->src_address == '') {
                $unset[] = 'src-address';
            } else {
                $setRequest->setargument('src-address', $this->src_address);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/proxy/access/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

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

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

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

        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->dst_host != "") {
            $setRequest->setargument('dst-host', $this->dst_host);
        }
        if ($this->dst_port != "") {
            $setRequest->setargument('dst-port', $this->dst_port);
        }
        if ($this->local_port != "") {
            $setRequest->setargument('local-port', $this->local_port);
        }
        if ($this->method != "") {
            $setRequest->setargument('method', $this->method);
        }
        if ($this->path != "") {
            $setRequest->setargument('path', $this->path);
        }
        if ($this->redirect_to != "") {
            $setRequest->setargument('redirect-to', $this->redirect_to);
        }
        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', $this->disabled);
        }
        $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('/ip/proxy/access');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
