<?php

namespace app\models\mikrotik\ip\dhcprelay;

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

    public $id;
    public $name;
    public $interface;
    public $dhcp_server;
    public $delay_threshold = '00:00:00';
    public $local_address = null;
    public $add_relay_info;
    public $invalid;
    public $disabled;

    public function rules() {
        return [
            ['name', 'required'],
            ['name', 'string'],
            ['interface', 'required'],
            ['interface', 'string'],
            ['dhcp_server', 'required'],
            ['dhcp_server', 'ip'],
            ['delay_threshold', 'string'],
            //['delay_threshold', 'date', 'format' => 'H:m:s'],
            ['local_address', 'ip'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-relay/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dhcprelay();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->interface = $response->getProperty('interface');
                $c->dhcp_server = $response->getProperty('dhcp-server');
                $c->delay_threshold = $response->getProperty('delay-threshold');
                $c->local_address = $response->getProperty('local-address');
                $c->add_relay_info = $response->getProperty('add-relay-info');
                $c->invalid = $response->getProperty('invalid');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-relay/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->name = $response->getProperty('name');
                $this->interface = $response->getProperty('interface');
                $this->dhcp_server = $response->getProperty('dhcp-server');
                $this->delay_threshold = $response->getProperty('delay-threshold');
                $this->local_address = $response->getProperty('local-address');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-relay/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setargument('name', $this->name);
        $setRequest->setargument('interface', $this->interface);
        $setRequest->setargument('dhcp-server', $this->dhcp_server);
        if ($this->delay_threshold != 'none') {
            $setRequest->setargument('delay-threshold', $this->delay_threshold);
        }
        if ($this->local_address != '0.0.0.0') {
            $setRequest->setargument('local-address', $this->local_address);
        }
        $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-relay/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function reset() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-relay/reset-counters');
        return $connect->getConnection()->sendSync($setRequest);
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-relay/add');
        $setRequest->setargument('name', $this->name);
        $setRequest->setargument('interface', $this->interface);
        $setRequest->setargument('dhcp-server', $this->dhcp_server);
        if ($this->delay_threshold != '') {
            $setRequest->setargument('delay-threshold', $this->delay_threshold);
        }
        if ($this->local_address != '') {
            $setRequest->setargument('local-address', $this->local_address);
        }
        $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "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-relay');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
