<?php

namespace app\models\mikrotik\routing\bfd;

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

    public $id;
    public $interface;
    public $interval;
    public $min_rx;
    public $multiplier;
    public $invalid;
    public $default;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['interval', 'string'],
            ['min_rx', 'string'],
            ['multiplier', 'string'],
            ['invalid', 'string'],
            ['default', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bfd/interface/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Bfd();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->interval = $response->getProperty('interval');
                $c->min_rx = $response->getProperty('min-rx');
                $c->multiplier = $response->getProperty('multiplier');
                $c->invalid = $response->getProperty('invalid');
                $c->default = $response->getProperty('default');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bfd/interface/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->interval = $response->getProperty('interval');
                $this->min_rx = $response->getProperty('min-rx');
                $this->multiplier = $response->getProperty('multiplier');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Bfd();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/bfd/interface/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->interval != $c->interval) {
            $setRequest->setargument('interval', $this->interval);
        }
        if ($this->min_rx != $c->min_rx) {
            $setRequest->setargument('min-rx', $this->min_rx);
        }
        if ($this->multiplier != $c->multiplier) {
            $setRequest->setargument('multiplier', $this->multiplier);
        }

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

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

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

        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->interval != "") {
            $setRequest->setargument('interval', $this->interval);
        }
        if ($this->min_rx != "") {
            $setRequest->setargument('min-rx', $this->min_rx);
        }
        if ($this->multiplier != "") {
            $setRequest->setargument('multiplier', $this->multiplier);
        }

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

}
