<?php

namespace app\models\mikrotik\routing\prefix;

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

    public $id;
    public $chain;
    public $prefix;
    public $prefix_length;
    public $invert_match;
    public $action;
    public $set_metric;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['chain', 'string'],
            ['prefix', 'string'],
            ['prefix_length', 'string'],
            ['invert_match', 'string'],
            ['action', 'string'],
            ['set_metric', 'string'],
            ['disabled', 'string'],
        ];
    }

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

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/prefix-lists/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Prefix();
                $c->id = $response->getProperty('.id');
                $c->chain = $response->getProperty('chain');
                $c->prefix = $response->getProperty('prefix');
                $c->prefix_length = $response->getProperty('prefix-length');
                $c->invert_match = $response->getProperty('invert-match');
                $c->action = $response->getProperty('action');
                $c->set_metric = $response->getProperty('set-metric');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/prefix-lists/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->chain = $response->getProperty('chain');
                $this->prefix = $response->getProperty('prefix');
                $this->prefix_length = $response->getProperty('prefix-length');
                $this->invert_match = (($response->getProperty('invert-match') == 'true') ? true : false);
                $this->action = $response->getProperty('action');
                $this->set_metric = $response->getProperty('set-metric');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Prefix();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/prefix-lists/set');
        $setRequest->setargument('.id', $id);
        if ($this->chain != $c->chain) {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->prefix != $c->prefix) {
            $setRequest->setargument('prefix', $this->prefix);
        }
        if ($this->prefix_length != $c->prefix_length) {
            $setRequest->setargument('prefix-length', $this->prefix_length);
        }
        $setRequest->setargument('invert-match', (($this->invert_match == true) ? "true" : "false"));
        if ($this->action != $c->action) {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->set_metric != $c->set_metric) {
            $setRequest->setargument('set-metric', $this->set_metric);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/prefix-lists/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/prefix-lists/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/prefix-lists/add');
        if ($this->chain != "") {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->prefix != "") {
            $setRequest->setargument('prefix', $this->prefix);
        }
        if ($this->prefix_length != "") {
            $setRequest->setargument('prefix-length', $this->prefix_length);
        }
        if ($this->invert_match != "") {
            $setRequest->setargument('invert-match', (($this->invert_match == true) ? "true" : "false"));
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->set_metric != "") {
            $setRequest->setargument('set-metric', $this->set_metric);
        }
        $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/prefix-lists');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
