<?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 Tree extends Model {

    public $id;
    public $name;
    public $parent;
    public $packet_mark;
    public $limit_at;
    public $queue;
    public $priority;
    public $max_limit;
    public $burst_limit;
    public $burst_threshold;
    public $burst_time;
    public $bucket_size;
    public $bytes;
    public $packets;
    public $dropped;
    public $rate;
    public $packet_rate;
    public $queued_packets;
    public $queued_bytes;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['parent', 'string'],
            ['packet_mark', 'string'],
            ['limit_at', 'string'],
            ['queue', 'string'],
            ['priority', 'string'],
            ['max_limit', 'string'],
            ['burst_limit', 'string'],
            ['burst_threshold', 'string'],
            ['burst_time', 'string'],
            ['bucket_size', 'string'],
            ['bytes', 'string'],
            ['packets', 'string'],
            ['dropped', 'string'],
            ['rate', 'string'],
            ['packet_rate', 'string'],
            ['queued_packets', 'string'],
            ['queued_bytes', 'string'],
            ['invalid', 'string'],
            ['disabled', 'string'],
        ];
    }

    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;
    }

    public function getTreeName() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/queue/tree/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('/queue/tree/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Tree();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->parent = $response->getProperty('parent');
                $c->packet_mark = $response->getProperty('packet-mark');
                $c->limit_at = $response->getProperty('limit-at');
                $c->queue = $response->getProperty('queue');
                $c->priority = $response->getProperty('priority');
                $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->bytes = $response->getProperty('bytes');
                $c->packets = $response->getProperty('packets');
                $c->dropped = $response->getProperty('dropped');
                $c->rate = $response->getProperty('rate');
                $c->packet_rate = $response->getProperty('packet-rate');
                $c->queued_packets = $response->getProperty('queued-packets');
                $c->queued_bytes = $response->getProperty('queued-bytes');
                $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('/queue/tree/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->parent = $response->getProperty('parent');
                $this->packet_mark = $response->getProperty('packet-mark');
                $this->limit_at = $response->getProperty('limit-at');
                $this->queue = $response->getProperty('queue');
                $this->priority = $response->getProperty('priority');
                $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->bytes = $response->getProperty('bytes');
                $this->packets = $response->getProperty('packets');
                $this->dropped = $response->getProperty('dropped');
                $this->rate = $response->getProperty('rate');
                $this->packet_rate = $response->getProperty('packet-rate');
                $this->queued_packets = $response->getProperty('queued-packets');
                $this->queued_bytes = $response->getProperty('queued-bytes');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Tree();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/queue/tree/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->parent != $c->parent) {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->packet_mark != $c->packet_mark) {
            $setRequest->setargument('packet-mark', $this->packet_mark);
        }
        if ($this->limit_at != $c->limit_at) {
            $setRequest->setargument('limit-at', $this->limit_at);
        }
        if ($this->queue != $c->queue) {
            $setRequest->setargument('queue', $this->queue);
        }
        if ($this->priority != $c->priority) {
            $setRequest->setargument('priority', $this->priority);
        }
        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->bucket_size != $c->bucket_size) {
            $setRequest->setargument('bucket-size', $this->bucket_size);
        }
        if ($this->bytes != $c->bytes) {
            $setRequest->setargument('bytes', $this->bytes);
        }
        if ($this->packets != $c->packets) {
            $setRequest->setargument('packets', $this->packets);
        }
        if ($this->dropped != $c->dropped) {
            $setRequest->setargument('dropped', $this->dropped);
        }
        if ($this->rate != $c->rate) {
            $setRequest->setargument('rate', $this->rate);
        }
        if ($this->packet_rate != $c->packet_rate) {
            $setRequest->setargument('packet-rate', $this->packet_rate);
        }
        if ($this->queued_packets != $c->queued_packets) {
            $setRequest->setargument('queued-packets', $this->queued_packets);
        }
        if ($this->queued_bytes != $c->queued_bytes) {
            $setRequest->setargument('queued-bytes', $this->queued_bytes);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/queue/tree/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/tree/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/queue/tree/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->parent != "") {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->packet_mark != "") {
            $setRequest->setargument('packet-mark', $this->packet_mark);
        }
        if ($this->limit_at != "") {
            $setRequest->setargument('limit-at', $this->limit_at);
        }
        if ($this->queue != "") {
            $setRequest->setargument('queue', $this->queue);
        }
        if ($this->priority != "") {
            $setRequest->setargument('priority', $this->priority);
        }
        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->bytes != "") {
            $setRequest->setargument('bytes', $this->bytes);
        }
        if ($this->packets != "") {
            $setRequest->setargument('packets', $this->packets);
        }
        if ($this->dropped != "") {
            $setRequest->setargument('dropped', $this->dropped);
        }
        if ($this->rate != "") {
            $setRequest->setargument('rate', $this->rate);
        }
        if ($this->packet_rate != "") {
            $setRequest->setargument('packet-rate', $this->packet_rate);
        }
        if ($this->queued_packets != "") {
            $setRequest->setargument('queued-packets', $this->queued_packets);
        }
        if ($this->queued_bytes != "") {
            $setRequest->setargument('queued-bytes', $this->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/tree');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
