<?php

namespace app\models\mikrotik\ppp;

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

    public $id;
    public $name;
    public $local_address;
    public $remote_address;
    public $bridge;
    public $bridge_port_priority;
    public $bridge_path_cost;
    public $session_timeout;
    public $idle_timeout;
    public $use_mpls;
    public $use_compression;
    public $use_encryption;
    public $only_one;
    public $change_tcp_mss;
    public $use_upnp;
    public $rate_limit;
    public $insert_queue_before;
    public $parent_queue;
    public $queue_type;
    public $incoming_filter;
    public $outgoing_filter;
    public $address_list;
    public $dns_server;
    public $wins_server;
    public $on_up;
    public $on_down;
    public $default;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['local_address', 'string'],
            ['remote_address', 'string'],
            ['bridge', 'string'],
            ['bridge_port_priority', 'string'],
            ['bridge_path_cost', 'string'],
            ['session_timeout', 'string'],
            ['idle_timeout', 'string'],
            ['use_mpls', 'string'],
            ['use_compression', 'string'],
            ['use_encryption', 'string'],
            ['only_one', 'string'],
            ['change_tcp_mss', 'string'],
            ['use_upnp', 'string'],
            ['rate_limit', 'string'],
            ['insert_queue_before', 'string'],
            ['parent_queue', 'string'],
            ['queue_type', 'string'],
            ['incoming_filter', 'string'],
            ['outgoing_filter', 'string'],
            ['address_list', 'string'],
            ['dns_server', 'string'],
            ['wins_server', 'string'],
            ['on_up', 'string'],
            ['on_down', 'string'],
            ['default', 'string'],
        ];
    }

    public function getPoolNames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/pool/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

    public function getAddress() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/address-list/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('list')] = $response->getProperty('list');
            }
        }
        return $return;
    }

    public function getBridges() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bridge/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/profile/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Profiles();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->local_address = $response->getProperty('local-address');
                $c->remote_address = $response->getProperty('remote-address');
                $c->bridge = $response->getProperty('bridge');
                $c->bridge_port_priority = $response->getProperty('bridge-port-priority');
                $c->bridge_path_cost = $response->getProperty('bridge-path-cost');
                $c->session_timeout = $response->getProperty('session-timeout');
                $c->idle_timeout = $response->getProperty('idle-timeout');
                $c->use_mpls = $response->getProperty('use-mpls');
                $c->use_compression = $response->getProperty('use-compression');
                $c->use_encryption = $response->getProperty('use-encryption');
                $c->only_one = $response->getProperty('only-one');
                $c->change_tcp_mss = $response->getProperty('change-tcp-mss');
                $c->use_upnp = $response->getProperty('use-upnp');
                $c->rate_limit = $response->getProperty('rate-limit');
                $c->insert_queue_before = $response->getProperty('insert-queue-before');
                $c->parent_queue = $response->getProperty('parent-queue');
                $c->queue_type = $response->getProperty('queue-type');
                $c->incoming_filter = $response->getProperty('incoming-filter');
                $c->outgoing_filter = $response->getProperty('outgoing-filter');
                $c->address_list = $response->getProperty('address-list');
                $c->dns_server = $response->getProperty('dns-server');
                $c->wins_server = $response->getProperty('wins-server');
                $c->on_up = $response->getProperty('on-up');
                $c->on_down = $response->getProperty('on-down');
                $c->default = $response->getProperty('default');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/profile/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->local_address = $response->getProperty('local-address');
                $this->remote_address = $response->getProperty('remote-address');
                $this->bridge = $response->getProperty('bridge');
                $this->bridge_port_priority = $response->getProperty('bridge-port-priority');
                $this->bridge_path_cost = $response->getProperty('bridge-path-cost');
                $this->session_timeout = $response->getProperty('session-timeout');
                $this->idle_timeout = $response->getProperty('idle-timeout');
                $this->use_mpls = $response->getProperty('use-mpls');
                $this->use_compression = $response->getProperty('use-compression');
                $this->use_encryption = $response->getProperty('use-encryption');
                $this->only_one = $response->getProperty('only-one');
                $this->change_tcp_mss = $response->getProperty('change-tcp-mss');
                $this->use_upnp = $response->getProperty('use-upnp');
                $this->rate_limit = $response->getProperty('rate-limit');
                $this->insert_queue_before = $response->getProperty('insert-queue-before');
                $this->parent_queue = $response->getProperty('parent-queue');
                $this->queue_type = $response->getProperty('queue-type');
                $this->incoming_filter = $response->getProperty('incoming-filter');
                $this->outgoing_filter = $response->getProperty('outgoing-filter');
                $this->address_list = $response->getProperty('address-list');
                $this->dns_server = $response->getProperty('dns-server');
                $this->wins_server = $response->getProperty('wins-server');
                $this->on_up = $response->getProperty('on-up');
                $this->on_down = $response->getProperty('on-down');
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Profiles();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ppp/profile/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->local_address != $c->local_address) {
            if ($this->local_address == '') {
                $unset[] = 'local-address';
            } else {
                $setRequest->setargument('local-address', $this->local_address);
            }
        }
        if ($this->remote_address != $c->remote_address) {
            if ($this->remote_address == '') {
                $unset[] = 'remote-address';
            } else {
                $setRequest->setargument('remote-address', $this->remote_address);
            }
        }
        if ($this->bridge != $c->bridge) {
            if ($this->bridge == '') {
                $unset[] = 'bridge';
            } else {
                $setRequest->setargument('bridge', $this->bridge);
            }
        }
        if ($this->bridge_port_priority != $c->bridge_port_priority) {
            if ($this->bridge_port_priority == '') {
                $unset[] = 'bridge-port-priority';
            } else {
                $setRequest->setargument('bridge-port-priority', $this->bridge_port_priority);
            }
        }
        if ($this->bridge_path_cost != $c->bridge_path_cost) {
            if ($this->bridge_path_cost == '') {
                $unset[] = 'bridge-path-cost';
            } else {
                $setRequest->setargument('bridge-path-cost', $this->bridge_path_cost);
            }
        }
        if ($this->session_timeout != $c->session_timeout) {
            if ($this->session_timeout == '') {
                $unset[] = 'session-timeout';
            } else {
                $setRequest->setargument('session-timeout', $this->session_timeout);
            }
        }
        if ($this->idle_timeout != $c->idle_timeout) {
            if ($this->idle_timeout == '') {
                $unset[] = 'idle-timeout';
            } else {
                $setRequest->setargument('idle-timeout', $this->idle_timeout);
            }
        }
        if ($this->use_mpls != $c->use_mpls) {
            $setRequest->setargument('use-mpls', $this->use_mpls);
        }
        if ($this->use_compression != $c->use_compression) {
            $setRequest->setargument('use-compression', $this->use_compression);
        }
        if ($this->use_encryption != $c->use_encryption) {
            $setRequest->setargument('use-encryption', $this->use_encryption);
        }
        if ($this->only_one != $c->only_one) {
            $setRequest->setargument('only-one', $this->only_one);
        }
        if ($this->change_tcp_mss != $c->change_tcp_mss) {
            $setRequest->setargument('change-tcp-mss', $this->change_tcp_mss);
        }
        if ($this->use_upnp != $c->use_upnp) {
            $setRequest->setargument('use-upnp', $this->use_upnp);
        }
        if ($this->rate_limit != $c->rate_limit) {
            if ($this->rate_limit == '') {
                $unset[] = 'rate-limit';
            } else {
                $setRequest->setargument('rate-limit', $this->rate_limit);
            }
        }
        if ($this->insert_queue_before != $c->insert_queue_before) {
            if ($this->insert_queue_before == '') {
                $unset[] = 'insert-queue-before';
            } else {
                $setRequest->setargument('insert-queue-before', $this->insert_queue_before);
            }
        }
        if ($this->parent_queue != $c->parent_queue) {
            if ($this->parent_queue == '') {
                $unset[] = 'parent-queue';
            } else {
                $setRequest->setargument('parent-queue', $this->parent_queue);
            }
        }
        if ($this->queue_type != $c->queue_type) {
            if ($this->queue_type == '') {
                $unset[] = 'queue-type';
            } else {
                $setRequest->setargument('queue-type', $this->queue_type);
            }
        }
        if ($this->incoming_filter != $c->incoming_filter) {
            if ($this->incoming_filter == '') {
                $unset[] = 'incoming-filter';
            } else {
                $setRequest->setargument('incoming-filter', $this->incoming_filter);
            }
        }
        if ($this->outgoing_filter != $c->outgoing_filter) {
            if ($this->outgoing_filter == '') {
                $unset[] = 'outgoing-filter';
            } else {
                $setRequest->setargument('outgoing-filter', $this->outgoing_filter);
            }
        }
        if ($this->address_list != $c->address_list) {
            $setRequest->setargument('address-list', $this->address_list);
        }
        if ($this->dns_server != $c->dns_server) {
            if ($this->dns_server == '') {
                $unset[] = 'dns-server';
            } else {
                $setRequest->setargument('dns-server', $this->dns_server);
            }
        }
        if ($this->wins_server != $c->wins_server) {
            if ($this->wins_server == '') {
                $unset[] = 'wins-server';
            } else {
                $setRequest->setargument('wins-server', $this->wins_server);
            }
        }
        if ($this->on_up != $c->on_up) {
            $setRequest->setargument('on-up', $this->on_up);
        }
        if ($this->on_down != $c->on_down) {
            $setRequest->setargument('on-down', $this->on_down);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ppp/profile/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('/ppp/profile/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ppp/profile/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->local_address != "") {
            $setRequest->setargument('local-address', $this->local_address);
        }
        if ($this->remote_address != "") {
            $setRequest->setargument('remote-address', $this->remote_address);
        }
        if ($this->bridge != "") {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->bridge_port_priority != "") {
            $setRequest->setargument('bridge-port-priority', $this->bridge_port_priority);
        }
        if ($this->bridge_path_cost != "") {
            $setRequest->setargument('bridge-path-cost', $this->bridge_path_cost);
        }
        if ($this->session_timeout != "") {
            $setRequest->setargument('session-timeout', $this->session_timeout);
        }
        if ($this->idle_timeout != "") {
            $setRequest->setargument('idle-timeout', $this->idle_timeout);
        }
        if ($this->use_mpls != "") {
            $setRequest->setargument('use-mpls', $this->use_mpls);
        }
        if ($this->use_compression != "") {
            $setRequest->setargument('use-compression', $this->use_compression);
        }
        if ($this->use_encryption != "") {
            $setRequest->setargument('use-encryption', $this->use_encryption);
        }
        if ($this->only_one != "") {
            $setRequest->setargument('only-one', $this->only_one);
        }
        if ($this->change_tcp_mss != "") {
            $setRequest->setargument('change-tcp-mss', $this->change_tcp_mss);
        }
        if ($this->use_upnp != "") {
            $setRequest->setargument('use-upnp', $this->use_upnp);
        }
        if ($this->rate_limit != "") {
            $setRequest->setargument('rate-limit', $this->rate_limit);
        }
        if ($this->insert_queue_before != "") {
            $setRequest->setargument('insert-queue-before', $this->insert_queue_before);
        }
        if ($this->parent_queue != "") {
            $setRequest->setargument('parent-queue', $this->parent_queue);
        }
        if ($this->queue_type != "") {
            $setRequest->setargument('queue-type', $this->queue_type);
        }
        if ($this->incoming_filter != "") {
            $setRequest->setargument('incoming-filter', $this->incoming_filter);
        }
        if ($this->outgoing_filter != "") {
            $setRequest->setargument('outgoing-filter', $this->outgoing_filter);
        }
        if ($this->address_list != "") {
            $setRequest->setargument('address-list', $this->address_list);
        }
        if ($this->dns_server != "") {
            $setRequest->setargument('dns-server', $this->dns_server);
        }
        if ($this->wins_server != "") {
            $setRequest->setargument('wins-server', $this->wins_server);
        }
        if ($this->on_up != "") {
            $setRequest->setargument('on-up', $this->on_up);
        }
        if ($this->on_down != "") {
            $setRequest->setargument('on-down', $this->on_down);
        }
        $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('/ppp/profile');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getTypes() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/queue/type/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {

                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

    public function getQueues() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/queue/simple/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $return;
    }

}
