<?php

namespace app\models\mikrotik\ip\packing;

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

    public $id;
    public $interface;
    public $packing;
    public $unpacking;
    public $aggregated_size;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['packing', 'string'],
            ['unpacking', 'string'],
            ['aggregated_size', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/packing/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Packing();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->packing = $response->getProperty('packing');
                $c->unpacking = $response->getProperty('unpacking');
                $c->aggregated_size = $response->getProperty('aggregated-size');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/packing/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->interface = $response->getProperty('interface');
                $this->packing = $response->getProperty('packing');
                $this->unpacking = $response->getProperty('unpacking');
                $this->aggregated_size = $response->getProperty('aggregated-size');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/packing/set');
        $setRequest->setargument('.id', $id);
        if ($this->id != "") {
            $setRequest->setargument('.id', $id);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->packing != "") {
            $setRequest->setargument('packing', $this->packing);
        }
        if ($this->unpacking != "") {
            $setRequest->setargument('unpacking', $this->unpacking);
        }
        if ($this->aggregated_size != "") {
            $setRequest->setargument('aggregated-size', $this->aggregated_size);
        }
        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/packing/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

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

        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->packing != "") {
            $setRequest->setargument('packing', $this->packing);
        }
        if ($this->unpacking != "") {
            $setRequest->setargument('unpacking', $this->unpacking);
        }
        if ($this->aggregated_size != "") {
            $setRequest->setargument('aggregated-size', $this->aggregated_size);
        }
        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/packing');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
