<?php

namespace app\models\mikrotik\ip\dhcpclient;

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

    public $id;
    public $interface;
    public $add_default_route;
    public $default_route_distance;
    public $use_peer_dns;
    public $use_peer_ntp;
    public $dhcp_options;
    public $status;
    public $address;
    public $gateway;
    public $dhcp_server;
    public $primary_dns;
    public $secondary_dns;
    public $primary_ntp;
    public $expires_after;
    public $invalid;
    public $disabled;

    public function rules() {

        return [
            ['interface', 'string'],
            ['use_peer_dns', 'string'],
            ['use_peer_ntp', 'string'],
            //['dhcp_options', 'string'],
            ['add_default_route', 'string'],
            ['default_route_distance', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/dhcp-client/getall');

        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $c = new Dhcpclient();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->default_route_distance = $response->getProperty('default-route-distance');
                $c->use_peer_dns = $response->getProperty('use-peer-dns');
                $c->use_peer_ntp = $response->getProperty('use-peer-ntp');
                $c->dhcp_options = $response->getProperty('dhcp-options');
                $c->status = $response->getProperty('status');
                $c->address = $response->getProperty('address');
                $c->gateway = $response->getProperty('gateway');
                $c->dhcp_server = $response->getProperty('dhcp-server');
                $c->primary_dns = $response->getProperty('primary-dns');
                $c->secondary_dns = $response->getProperty('secondary-dns');
                $c->primary_ntp = $response->getProperty('primary-ntp');
                $c->expires_after = $response->getProperty('expires-after');
                $c->invalid = $response->getProperty('invalid');
                $c->disabled = $response->getProperty('disabled');

                $return[] = $c;
            }
        }
        return $return;
    }

    public function getDHCP($id) {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/dhcp-client/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->add_default_route = $response->getProperty('add-default-route');
                $this->default_route_distance = $response->getProperty('default-route-distance');
                $this->use_peer_dns = (($response->getProperty('use-peer-dns') == 'true') ? true : false);
                $this->use_peer_ntp = (($response->getProperty('use-peer-ntp') == 'true') ? true : false);
                $this->dhcp_options = $response->getProperty('dhcp-options');
                $this->status = $response->getProperty('status');
                $this->address = $response->getProperty('address');
                $this->gateway = $response->getProperty('gateway');
                $this->dhcp_server = $response->getProperty('dhcp-server');
                $this->primary_dns = $response->getProperty('primary-dns');
                $this->secondary_dns = $response->getProperty('secondary-dns');
                $this->primary_ntp = $response->getProperty('primary-ntp');
                $this->expires_after = $response->getProperty('expires-after');
                $this->invalid = $response->getProperty('invalid');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-client/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setArgument('interface', $this->interface);
        $setRequest->setArgument('add-default-route', $this->add_default_route);
        if ($this->add_default_route == 'yes') {
            $setRequest->setArgument('default-route-distance', $this->default_route_distance);
        }
        $setRequest->setArgument('use-peer-dns', (($this->use_peer_dns) ? 'true' : 'false'));
        $setRequest->setArgument('use-peer-ntp', (($this->use_peer_ntp) ? 'true' : 'false'));
        $setRequest->setArgument('dhcp-options', $this->dhcp_options);
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-client/add');
        $setRequest->setArgument('interface', $this->interface);
        $setRequest->setArgument('add-default-route', $this->add_default_route);
        if ($this->add_default_route == 'yes') {
            $setRequest->setArgument('default-route-distance', $this->default_route_distance);
        }
        $setRequest->setArgument('disabled', 'false');
        $setRequest->setArgument('use-peer-dns', (($this->use_peer_dns) ? 'true' : 'false'));
        $setRequest->setArgument('use-peer-ntp', (($this->use_peer_ntp) ? 'true' : 'false'));
        $setRequest->setArgument('dhcp-options', $this->dhcp_options);
        $connect->getConnection()->sendSync($setRequest);
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();

        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip dhcp-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

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

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

    public function getAllTest() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('getall');

        $responses = $connect->getConnection()->sendSync($getRequest);

        return $responses;
    }

    public function getNames() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/dhcp-client/option/print');

        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $c[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        $return = $c;
        return $return;
    }

}
