<?php

namespace app\models\mikrotik\queues;

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

    public $id;
    public $name;
    public $target;
    public $dst;
    public $parent;
    public $packet_marks;
    public $priority;
    public $queue;
    public $limit_at;
    public $max_limit;
    public $burst_limit;
    public $burst_threshold;
    public $burst_time;
    public $bucket_size;
    public $total_priority;
    public $total_limit_at;
    public $total_max_limit;
    public $total_burst_limit;
    public $total_burst_threshold;
    public $total_burst_time;
    public $total_queue;
    public $bytes;
    public $total_bytes;
    public $packets;
    public $total_packets;
    public $dropped;
    public $total_dropped;
    public $rate;
    public $total_rate;
    public $packet_rate;
    public $total_packet_rate;
    public $queued_packets;
    public $total_queued_packets;
    public $queued_bytes;
    public $total_queued_bytes;
    public $invalid;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['target', 'string'],
            ['dst', 'string'],
            ['parent', 'string'],
            ['packet_marks', 'string'],
            ['priority', 'string'],
            ['queue', 'string'],
            ['limit_at', 'string'],
            ['max_limit', 'string'],
            ['burst_limit', 'string'],
            ['burst_threshold', 'string'],
            ['burst_time', 'string'],
            ['bucket_size', 'string'],
            ['total_priority', 'string'],
            ['total_limit_at', 'string'],
            ['total_max_limit', 'string'],
            ['total_burst_limit', 'string'],
            ['total_burst_threshold', 'string'],
            ['total_burst_time', 'string'],
            ['total_queue', 'string'],
            ['bytes', 'string'],
            ['total_bytes', 'string'],
            ['packets', 'string'],
            ['total_packets', 'string'],
            ['dropped', 'string'],
            ['total_dropped', 'string'],
            ['rate', 'string'],
            ['total_rate', 'string'],
            ['packet_rate', 'string'],
            ['total_packet_rate', 'string'],
            ['queued_packets', 'string'],
            ['total_queued_packets', 'string'],
            ['queued_bytes', 'string'],
            ['total_queued_bytes', 'string'],
            ['invalid', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $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) {
                $c = new Simple();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->target = $response->getProperty('target');
                $c->dst = $response->getProperty('dst');
                $c->parent = $response->getProperty('parent');
                $c->packet_marks = $response->getProperty('packet-marks');
                $c->priority = $response->getProperty('priority');
                $c->queue = $response->getProperty('queue');
                $c->limit_at = $response->getProperty('limit-at');
                $c->max_limit = $response->getProperty('max-limit');
                $c->burst_limit = $response->getProperty('burst-limit');
                $c->burst_threshold = $response->getProperty('burst-threshold');
                $c->burst_time = $response->getProperty('burst-time');
                $c->bucket_size = $response->getProperty('bucket-size');
                $c->total_priority = $response->getProperty('total-priority');
                $c->total_limit_at = $response->getProperty('total-limit-at');
                $c->total_max_limit = $response->getProperty('total-max-limit');
                $c->total_burst_limit = $response->getProperty('total-burst-limit');
                $c->total_burst_threshold = $response->getProperty('total-burst-threshold');
                $c->total_burst_time = $response->getProperty('total-burst-time');
                $c->total_queue = $response->getProperty('total-queue');
                $c->bytes = $response->getProperty('bytes');
                $c->total_bytes = $response->getProperty('total-bytes');
                $c->packets = $response->getProperty('packets');
                $c->total_packets = $response->getProperty('total-packets');
                $c->dropped = $response->getProperty('dropped');
                $c->total_dropped = $response->getProperty('total-dropped');
                $c->rate = $response->getProperty('rate');
                $c->total_rate = $response->getProperty('total-rate');
                $c->packet_rate = $response->getProperty('packet-rate');
                $c->total_packet_rate = $response->getProperty('total-packet-rate');
                $c->queued_packets = $response->getProperty('queued-packets');
                $c->total_queued_packets = $response->getProperty('total-queued-packets');
                $c->queued_bytes = $response->getProperty('queued-bytes');
                $c->total_queued_bytes = $response->getProperty('total-queued-bytes');
                $c->invalid = $response->getProperty('invalid');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/queue/simple/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->target = $response->getProperty('target');
                $this->dst = $response->getProperty('dst');
                $this->parent = $response->getProperty('parent');
                $this->packet_marks = $response->getProperty('packet-marks');
                $this->priority = $response->getProperty('priority');
                $this->queue = $response->getProperty('queue');
                $this->limit_at = $response->getProperty('limit-at');
                $this->max_limit = $response->getProperty('max-limit');
                $this->burst_limit = $response->getProperty('burst-limit');
                $this->burst_threshold = $response->getProperty('burst-threshold');
                $this->burst_time = $response->getProperty('burst-time');
                $this->bucket_size = $response->getProperty('bucket-size');
                $this->total_priority = $response->getProperty('total-priority');
                $this->total_limit_at = $response->getProperty('total-limit-at');
                $this->total_max_limit = $response->getProperty('total-max-limit');
                $this->total_burst_limit = $response->getProperty('total-burst-limit');
                $this->total_burst_threshold = $response->getProperty('total-burst-threshold');
                $this->total_burst_time = $response->getProperty('total-burst-time');
                $this->total_queue = $response->getProperty('total-queue');
                $this->bytes = $response->getProperty('bytes');
                $this->total_bytes = $response->getProperty('total-bytes');
                $this->packets = $response->getProperty('packets');
                $this->total_packets = $response->getProperty('total-packets');
                $this->dropped = $response->getProperty('dropped');
                $this->total_dropped = $response->getProperty('total-dropped');
                $this->rate = $response->getProperty('rate');
                $this->total_rate = $response->getProperty('total-rate');
                $this->packet_rate = $response->getProperty('packet-rate');
                $this->total_packet_rate = $response->getProperty('total-packet-rate');
                $this->queued_packets = $response->getProperty('queued-packets');
                $this->total_queued_packets = $response->getProperty('total-queued-packets');
                $this->queued_bytes = $response->getProperty('queued-bytes');
                $this->total_queued_bytes = $response->getProperty('total-queued-bytes');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Simple();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/queue/simple/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->target != $c->target) {
            if ($this->target == '') {
                $unset[] = 'target';
            } else {
                $setRequest->setargument('target', $this->target);
            }
        }
        if ($this->dst != $c->dst) {
            if ($this->dst == '') {
                $unset[] = 'dst';
            } else {
                $setRequest->setargument('dst', $this->dst);
            }
        }
        if ($this->max_limit != $c->max_limit) {
            $setRequest->setargument('max-limit', $this->max_limit);
        }
        if ($this->burst_limit != $c->burst_limit) {
            $setRequest->setargument('burst-limit', $this->burst_limit);
        }
        if ($this->burst_threshold != $c->burst_threshold) {
            $setRequest->setargument('burst-threshold', $this->burst_threshold);
        }
        if ($this->burst_time != $c->burst_time) {
            $setRequest->setargument('burst-time', $this->burst_time);
        }
        if ($this->packet_marks != $c->packet_marks) {
            $setRequest->setargument('packet-marks', $this->packet_marks);
        }
        if ($this->limit_at != $c->limit_at) {
            $setRequest->setargument('limit-at', $this->limit_at);
        }
        if ($this->priority != $c->priority) {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->bucket_size != $c->bucket_size) {
            $setRequest->setargument('bucket-size', $this->bucket_size);
        }
        if ($this->queue != $c->queue) {
            $setRequest->setargument('queue', $this->queue);
        }
        if ($this->parent != $c->parent) {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->total_limit_at != $c->total_limit_at) {
            $setRequest->setargument('total-limit-at', $this->total_limit_at);
        }
        if ($this->total_max_limit != $c->total_max_limit) {
            $setRequest->setargument('total-max-limit', $this->total_max_limit);
        }
        if ($this->total_priority != $c->total_priority) {
            $setRequest->setargument('total-priority', $this->total_priority);
        }
        if ($this->total_burst_limit != $c->total_burst_limit) {
            $setRequest->setargument('total-burst-limit', $this->total_burst_limit);
        }
        if ($this->total_burst_threshold != $c->total_burst_threshold) {
            $setRequest->setargument('total-burst-threshold', $this->total_burst_threshold);
        }
        if ($this->total_burst_time != $c->total_burst_time) {
            $setRequest->setargument('total-burst-time', $this->total_burst_time);
        }
        if ($this->total_queue != $c->total_queue) {
            $setRequest->setargument('total-queue', $this->total_queue);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/queue/simple/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('/queue/simple/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/queue/simple/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->target != "") {
            $setRequest->setargument('target', $this->target);
        }
        if ($this->dst != "") {
            $setRequest->setargument('dst', $this->dst);
        }
        if ($this->parent != "") {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->packet_marks != "") {
            $setRequest->setargument('packet-marks', $this->packet_marks);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        if ($this->queue != "") {
            $setRequest->setargument('queue', $this->queue);
        }
        if ($this->limit_at != "") {
            $setRequest->setargument('limit-at', $this->limit_at);
        }
        if ($this->max_limit != "") {
            $setRequest->setargument('max-limit', $this->max_limit);
        }
        if ($this->burst_limit != "") {
            $setRequest->setargument('burst-limit', $this->burst_limit);
        }
        if ($this->burst_threshold != "") {
            $setRequest->setargument('burst-threshold', $this->burst_threshold);
        }
        if ($this->burst_time != "") {
            $setRequest->setargument('burst-time', $this->burst_time);
        }
        if ($this->bucket_size != "") {
            $setRequest->setargument('bucket-size', $this->bucket_size);
        }
        if ($this->total_priority != "") {
            $setRequest->setargument('total-priority', $this->total_priority);
        }
        if ($this->total_limit_at != "") {
            $setRequest->setargument('total-limit-at', $this->total_limit_at);
        }
        if ($this->total_max_limit != "") {
            $setRequest->setargument('total-max-limit', $this->total_max_limit);
        }
        if ($this->total_burst_limit != "") {
            $setRequest->setargument('total-burst-limit', $this->total_burst_limit);
        }
        if ($this->total_burst_threshold != "") {
            $setRequest->setargument('total-burst-threshold', $this->total_burst_threshold);
        }
        if ($this->total_burst_time != "") {
            $setRequest->setargument('total-burst-time', $this->total_burst_time);
        }
        if ($this->total_queue != "") {
            $setRequest->setargument('total-queue', $this->total_queue);
        }
        if ($this->bytes != "") {
            $setRequest->setargument('bytes', $this->bytes);
        }
        if ($this->total_bytes != "") {
            $setRequest->setargument('total-bytes', $this->total_bytes);
        }
        if ($this->packets != "") {
            $setRequest->setargument('packets', $this->packets);
        }
        if ($this->total_packets != "") {
            $setRequest->setargument('total-packets', $this->total_packets);
        }
        if ($this->dropped != "") {
            $setRequest->setargument('dropped', $this->dropped);
        }
        if ($this->total_dropped != "") {
            $setRequest->setargument('total-dropped', $this->total_dropped);
        }
        if ($this->rate != "") {
            $setRequest->setargument('rate', $this->rate);
        }
        if ($this->total_rate != "") {
            $setRequest->setargument('total-rate', $this->total_rate);
        }
        if ($this->packet_rate != "") {
            $setRequest->setargument('packet-rate', $this->packet_rate);
        }
        if ($this->total_packet_rate != "") {
            $setRequest->setargument('total-packet-rate', $this->total_packet_rate);
        }
        if ($this->queued_packets != "") {
            $setRequest->setargument('queued-packets', $this->queued_packets);
        }
        if ($this->total_queued_packets != "") {
            $setRequest->setargument('total-queued-packets', $this->total_queued_packets);
        }
        if ($this->queued_bytes != "") {
            $setRequest->setargument('queued-bytes', $this->queued_bytes);
        }
        if ($this->total_queued_bytes != "") {
            $setRequest->setargument('total-queued-bytes', $this->total_queued_bytes);
        }
        $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('/queue/simple');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getNames() {
        $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;
    }

    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 getInterfaces() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/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;
    }

}
