<?php

namespace app\models\mikrotik\ip\dhcpserver;

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

    public $id;
    public $alert_timeout;
    public $interface;
    public $on_alert;
    public $valid_server;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['alert_timeout', 'string'],
            //['interface', 'required'],
            ['interface', 'string'],
            ['on_alert', 'string'],
                //['valid_server', 'mac'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/alert/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dhcpserveralerts();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->alert_timeout = $response->getProperty('alert-timeout');
                $c->on_alert = $response->getProperty('on-alert');
                $c->valid_server = $response->getProperty('valid-server');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/alert/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->alert_timeout = $response->getProperty('alert-timeout');
                $this->on_alert = $response->getProperty('on-alert');
                $this->valid_server = $response->getProperty('valid-server');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/alert/set');
        $setRequest->setargument('.id', $id);
        $setRequest->setargument('interface', $this->interface);
        $setRequest->setargument('alert-timeout', $this->alert_timeout);
        $setRequest->setargument('on-alert', $this->on_alert);
        $setRequest->setargument('valid-server', $this->valid_server);
        $setRequest->setargument('disabled', $this->disabled);
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/alert/add');
        $setRequest->setargument('interface', $this->interface);
        $setRequest->setargument('alert-timeout', $this->alert_timeout);
        $setRequest->setargument('on-alert', $this->on_alert);
        $setRequest->setargument('valid-server', $this->valid_server);
        $setRequest->setargument('disabled', 'false');
        $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('/ip/dhcp-server/alert');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
