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

    public $id;
    public $name;
    public $interface;
    public $lease_time = '72:00:00';
    public $address_pool;
    public $bootp_support;
    public $src_address = '0.0.0.0';
    public $add_arp;
    public $relay;
    public $authoritative;
    public $always_broadcast;
    public $use_radius;
    public $lease_script;
    public $invalid;
    public $disabled;
    public $delay_threshold;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'required'],
            ['name', 'string'],
            ['interface', 'required'],
            ['interface', 'string'],
            ['lease_time', 'required'],
            ['lease_time', 'string'],
            ['address_pool', 'required'],
            ['address_pool', 'string'],
            ['bootp_support', 'string'],
            ['src_address', 'ip'],
            ['add_arp', 'string'],
            ['relay', 'ip'],
            ['authoritative', 'string'],
            ['always_broadcast', 'string'],
            ['use_radius', 'string'],
            ['lease_script', 'string'],
            ['delay_threshold', 'string'],
            ['invalid', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dhcpserver();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->interface = $response->getProperty('interface');
                $c->lease_time = $response->getProperty('lease-time');
                $c->address_pool = $response->getProperty('address-pool');
                $c->delay_threshold = $response->getProperty('delay-threshold');
                $c->bootp_support = $response->getProperty('bootp-support');
                $c->src_address = $response->getProperty('src-address');
                $c->add_arp = $response->getProperty('add-arp');
                $c->relay = $response->getProperty('relay');
                $c->authoritative = $response->getProperty('authoritative');
                $c->always_broadcast = $response->getProperty('always-broadcast');
                $c->use_radius = $response->getProperty('use-radius');
                $c->lease_script = $response->getProperty('lease-script');
                $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-server/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->lease_time = $response->getProperty('lease-time');
                $this->address_pool = $response->getProperty('address-pool');
                $this->delay_threshold = $response->getProperty('delay-threshold');
                $this->bootp_support = $response->getProperty('bootp-support');
                $this->src_address = $response->getProperty('src-address');
                $this->add_arp = (($response->getProperty('add-arp') == 'true') ? true : false);
                $this->relay = $response->getProperty('relay');
                $this->authoritative = $response->getProperty('authoritative');
                $this->always_broadcast = (($response->getProperty('always-broadcast') == 'true') ? true : false);
                $this->use_radius = (($response->getProperty('use-radius') == 'true') ? true : false);
                $this->lease_script = $response->getProperty('lease-script');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Dhcpserver();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/set');
        $setRequest->setargument('.id', $this->id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->lease_time != $c->lease_time) {
            $setRequest->setargument('lease-time', $this->lease_time);
        }
        if ($this->address_pool != $c->address_pool) {
            $setRequest->setargument('address-pool', $this->address_pool);
        }
        if ($this->bootp_support != $c->bootp_support) {
            $setRequest->setargument('bootp-support', $this->bootp_support);
        }
        if ($this->src_address != $c->src_address) {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->add_arp != $c->add_arp) {
            $setRequest->setargument('add-arp', (($this->add_arp == true) ? "true" : "false"));
        }
        if ($this->relay != $c->relay) {
            $setRequest->setargument('relay', $this->relay);
        }
        if ($this->authoritative != $c->authoritative) {
            $setRequest->setargument('authoritative', $this->authoritative);
        }
        if ($this->always_broadcast != $c->always_broadcast) {
            $setRequest->setargument('always-broadcast', (($this->always_broadcast == true) ? "true" : "false"));
        }
        if ($this->use_radius != $c->use_radius) {
            $setRequest->setargument('use-radius', (($this->use_radius == true) ? "true" : "false"));
        }
        if ($this->lease_script != $c->lease_script) {
            $setRequest->setargument('lease-script', $this->lease_script);
        }
        if ($this->invalid != $c->invalid) {
            $setRequest->setargument('invalid', $this->invalid);
        }
        if ($this->disabled != $c->disabled) {
            $setRequest->setargument('disabled', $this->disabled);
        }
        if ($this->delay_threshold != $c->delay_threshold) {
            $setRequest->setargument('delay-threshold', $this->delay_threshold);
        }
        $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/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/add');
        $setRequest->setargument('name', $this->name);
        $setRequest->setargument('interface', $this->interface);
        $setRequest->setargument('lease-time', $this->lease_time);
        $setRequest->setargument('address-pool', $this->address_pool);
        if ($this->delay_threshold != '') {
            $setRequest->setargument('delay-threshold', $this->delay_threshold);
        }
        $setRequest->setargument('bootp-support', $this->bootp_support);
        if ($this->src_address != '') {
            $setRequest->setargument('src-address', $this->src_address);
        }
        $setRequest->setargument('add-arp', (($this->add_arp == true) ? "true" : "false"));
        if ($this->relay != '') {
            $setRequest->setargument('relay', $this->relay);
        }
        $setRequest->setargument('authoritative', $this->authoritative);
        $setRequest->setargument('always-broadcast', (($this->always_broadcast == true) ? "true" : "false"));
        $setRequest->setargument('use-radius', (($this->use_radius == true) ? "true" : "false"));
        $setRequest->setargument('lease-script', $this->lease_script);
        $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');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
