<?php

namespace app\models\mikrotik\routing\bgp;

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

    public $id;
    public $instance;
    public $prefix;
    public $summary_only;
    public $inherit_attributes;
    public $include_igp;
    public $attribute_filter;
    public $suppress_filter;
    public $advertise_filter;
    public $active;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['instance', 'string'],
            ['prefix', 'string'],
            ['summary_only', 'string'],
            ['inherit_attributes', 'string'],
            ['include_igp', 'string'],
            ['attribute_filter', 'string'],
            ['suppress_filter', 'string'],
            ['advertise_filter', 'string'],
            ['active', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/aggregate/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Aggregate();
                $c->id = $response->getProperty('.id');
                $c->instance = $response->getProperty('instance');
                $c->prefix = $response->getProperty('prefix');
                $c->summary_only = $response->getProperty('summary-only');
                $c->inherit_attributes = $response->getProperty('inherit-attributes');
                $c->include_igp = $response->getProperty('include-igp');
                $c->attribute_filter = $response->getProperty('attribute-filter');
                $c->suppress_filter = $response->getProperty('suppress-filter');
                $c->advertise_filter = $response->getProperty('advertise-filter');
                $c->active = $response->getProperty('active');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/aggregate/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->instance = $response->getProperty('instance');
                $this->prefix = $response->getProperty('prefix');
                $this->summary_only = (($response->getProperty('summary-only') == 'true') ? true : false);
                $this->inherit_attributes = (($response->getProperty('inherit-attributes') == 'true') ? true : false);
                $this->include_igp = (($response->getProperty('include-igp') == 'true') ? true : false);
                $this->attribute_filter = $response->getProperty('attribute-filter');
                $this->suppress_filter = $response->getProperty('suppress-filter');
                $this->advertise_filter = $response->getProperty('advertise-filter');
                $this->active = (($response->getProperty('active') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Aggregate();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/aggregate/set');
        $setRequest->setargument('.id', $id);
        if ($this->instance != $c->instance) {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->prefix != $c->prefix) {
            $setRequest->setargument('prefix', $this->prefix);
        }
        $setRequest->setargument('summary-only', (($this->summary_only == true) ? "true" : "false"));
        $setRequest->setargument('inherit-attributes', (($this->inherit_attributes == true) ? "true" : "false"));
        $setRequest->setargument('include-igp', (($this->include_igp == true) ? "true" : "false"));
        if ($this->attribute_filter != $c->attribute_filter) {
            $setRequest->setargument('attribute-filter', $this->attribute_filter);
        }
        if ($this->suppress_filter != $c->suppress_filter) {
            $setRequest->setargument('suppress-filter', $this->suppress_filter);
        }
        if ($this->advertise_filter != $c->advertise_filter) {
            $setRequest->setargument('advertise-filter', $this->advertise_filter);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/bgp/aggregate/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('/routing/bgp/aggregate/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bgp/aggregate/add');
        if ($this->instance != "") {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->prefix != "") {
            $setRequest->setargument('prefix', $this->prefix);
        }
        if ($this->summary_only != "") {
            $setRequest->setargument('summary-only', (($this->summary_only == true) ? "true" : "false"));
        }
        if ($this->inherit_attributes != "") {
            $setRequest->setargument('inherit-attributes', (($this->inherit_attributes == true) ? "true" : "false"));
        }
        if ($this->include_igp != "") {
            $setRequest->setargument('include-igp', (($this->include_igp == true) ? "true" : "false"));
        }
        if ($this->attribute_filter != "") {
            $setRequest->setargument('attribute-filter', $this->attribute_filter);
        }
        if ($this->suppress_filter != "") {
            $setRequest->setargument('suppress-filter', $this->suppress_filter);
        }
        if ($this->advertise_filter != "") {
            $setRequest->setargument('advertise-filter', $this->advertise_filter);
        }

        $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('/routing/bgp/aggregate');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
