<?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 Dhcpservernetwork extends Model {

    public $id;
    public $address;
    public $gateway;
    public $dns_server;
    public $wins_server;
    public $ntp_server;
    public $caps_manager;
    public $dhcp_option;
    public $boot_file_name;
    public $domain;
    public $netmask;
    public $next_server;

    public function rules() {
        return [
            ['id', 'string'],
            ['address', 'required'],
            ['address', 'ip', 'subnet' => null],
            ['gateway', 'required'],
            ['gateway', 'ip'],
            ['dns_server', 'ip'],
            ['wins_server', 'ip'],
            ['ntp_server', 'ip'],
            ['caps_manager', 'ip'],
            //['dhcp_option', 'string'],
            ['boot_file_name', 'string'],
            ['domain', 'string'],
            ['netmask', 'string'],
            ['next_server', 'ip'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/network/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dhcpservernetwork();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->gateway = $response->getProperty('gateway');
                $c->dns_server = $response->getProperty('dns-server');
                $c->wins_server = $response->getProperty('wins-server');
                $c->ntp_server = $response->getProperty('ntp-server');
                $c->caps_manager = $response->getProperty('caps-manager');
                $c->dhcp_option = $response->getProperty('dhcp-option');
                $c->boot_file_name = $response->getProperty('boot-file-name');
                $c->domain = $response->getProperty('domain');
                $c->netmask = $response->getProperty('netmask');
                $c->next_server = $response->getProperty('next-server');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/network/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->address = $response->getProperty('address');
                $this->gateway = $response->getProperty('gateway');
                $this->dns_server = $response->getProperty('dns-server');
                $this->wins_server = $response->getProperty('wins-server');
                $this->ntp_server = $response->getProperty('ntp-server');
                $this->caps_manager = $response->getProperty('caps-manager');
                $this->dhcp_option = $response->getProperty('dhcp-option');
                $this->boot_file_name = $response->getProperty('boot-file-name');
                $this->domain = $response->getProperty('domain');
                $this->netmask = $response->getProperty('netmask');
                $this->next_server = $response->getProperty('next-server');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/network/set');

        $setRequest->setargument('.id', $id);
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->gateway != "") {
            $setRequest->setargument('gateway', $this->gateway);
        }
        if ($this->dns_server != "") {
            $setRequest->setargument('dns-server', $this->dns_server);
        }
        if ($this->wins_server != "") {
            $setRequest->setargument('wins-server', $this->wins_server);
        }
        if ($this->ntp_server != "") {
            $setRequest->setargument('ntp-server', $this->ntp_server);
        }
        if ($this->caps_manager != "") {
            $setRequest->setargument('caps-manager', $this->caps_manager);
        }
        if ($this->dhcp_option != "") {
            $setRequest->setargument('dhcp-option', $this->dhcp_option);
        }
        if ($this->boot_file_name != "") {
            $setRequest->setargument('boot-file-name', $this->boot_file_name);
        }
        if ($this->domain != "") {
            $setRequest->setargument('domain', $this->domain);
        }
        if ($this->netmask != "") {
            $setRequest->setargument('netmask', $this->netmask);
        }
        if ($this->next_server != "") {
            $setRequest->setargument('next-server', $this->next_server);
        }
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/network/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->gateway != "") {
            $setRequest->setargument('gateway', $this->gateway);
        }
        if ($this->dns_server != "") {
            $setRequest->setargument('dns-server', $this->dns_server);
        }
        if ($this->wins_server != "") {
            $setRequest->setargument('wins-server', $this->wins_server);
        }
        if ($this->ntp_server != "") {
            $setRequest->setargument('ntp-server', $this->ntp_server);
        }
        if ($this->caps_manager != "") {
            $setRequest->setargument('caps-manager', $this->caps_manager);
        }
        if ($this->dhcp_option != "") {
            $setRequest->setargument('dhcp-option', $this->dhcp_option);
        }
        if ($this->boot_file_name != "") {
            $setRequest->setargument('boot-file-name', $this->boot_file_name);
        }
        if ($this->domain != "") {
            $setRequest->setargument('domain', $this->domain);
        }
        if ($this->netmask != "") {
            $setRequest->setargument('netmask', $this->netmask);
        }
        if ($this->next_server != "") {
            $setRequest->setargument('next-server', $this->next_server);
        }
        $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();

        $setRequest = new RouterOS\Request('/ip/dhcp-server/network/remove');
        $setRequest->setargument('.id', $id);
        return $connect->getConnection()->sendSync($setRequest);
    }

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

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

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

        return $responses;
    }

}
