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

    public $id;
    public $address;
    public $mac_address;
    public $src_mac_address;
    public $use_mac_address;
    public $use_src_mac;
    public $address_lists;
    public $always_broadcast;
    public $block_access;
    public $client_id;
    public $lease_time;
    public $dhcp_option;
    public $status;
    public $last_seen;
    public $radius;
    public $dynamic;
    public $blocked;
    public $disabled;
    public $active_address;
    public $active_mac_address;
    public $expires_after;
    public $rate_limit;

    public function rules() {
        return [
            ['id', 'string'],
            ['address', 'required'],
            ['address', 'string'],
            ['mac_address', 'required'],
            ['mac_address', 'mac'],
            ['src_mac_address', 'mac'],
            ['use_src_mac', 'string'],
            ['use_mac_address', 'string'],
            ['address_lists', 'string'],
            ['always_broadcast', 'string'],
            ['block_access', 'string'],
            ['client_id', 'string'],
            ['lease_time', 'string'],
            //['dhcp_option', 'string'],
            ['status', 'string'],
            ['rate_limit', 'string'],
            ['last_seen', 'string'],
            ['radius', 'string'],
            ['dynamic', 'string'],
            ['blocked', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/lease/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Dhcpserverleases();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->mac_address = $response->getProperty('mac-address');
                $c->src_mac_address = $response->getProperty('src-mac-address');
                $c->use_mac_address = $response->getProperty('use-mac-address');
                $c->address_lists = $response->getProperty('address-lists');
                $c->always_broadcast = $response->getProperty('always-broadcast');
                $c->block_access = $response->getProperty('block-access');
                $c->client_id = $response->getProperty('client-id');
                $c->rate_limit = $response->getProperty('rate-limit');
                $c->lease_time = $response->getProperty('lease-time');
                $c->active_address = $response->getProperty('active-address');
                $c->active_mac_address = $response->getProperty('active-mac-address');
                $c->dhcp_option = $response->getProperty('dhcp-option');
                $c->expires_after = $response->getProperty('expires_after');
                $c->status = $response->getProperty('status');
                $c->last_seen = $response->getProperty('last-seen');
                $c->radius = $response->getProperty('radius');
                $c->dynamic = $response->getProperty('dynamic');
                $c->blocked = $response->getProperty('blocked');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/lease/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->mac_address = $response->getProperty('mac-address');
                $this->src_mac_address = $response->getProperty('src-mac-address');
                $this->use_mac_address = $response->getProperty('use-mac-address');
                $this->address_lists = $response->getProperty('address-lists');
                $this->always_broadcast = (($response->getProperty('always-broadcast') == 'true') ? true : false);
                $this->block_access = (($response->getProperty('block-access') == 'true') ? true : false);
                $this->client_id = $response->getProperty('client-id');
                $this->lease_time = $response->getProperty('lease-time');
                $this->dhcp_option = $response->getProperty('dhcp-option');
                $this->status = $response->getProperty('status');
                $this->rate_limit = $response->getProperty('rate-limit');
                //$this->use_src_mac = $response->getProperty('use-src-mac');
                $this->last_seen = $response->getProperty('last-seen');
                $this->radius = (($response->getProperty('radius') == 'true') ? true : false);
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->blocked = (($response->getProperty('blocked') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/lease/set');
        $setRequest->setargument('.id', $id);
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->src_mac_address != "") {
            $setRequest->setargument('src-mac-address', $this->src_mac_address);
        }
        if ($this->use_mac_address != "") {
            $setRequest->setargument('use-mac-address', $this->use_mac_address);
        }
        if ($this->use_src_mac != "") {
            //$setRequest->setargument('use-src-mac', (($this->use_src_mac == '0') ? "false" : "true"));
        }
        if ($this->address_lists != "") {
            $setRequest->setargument('address-lists', $this->address_lists);
        }
        if ($this->rate_limit != "") {
            $setRequest->setargument('rate-limit', $this->rate_limit);
        }
        if ($this->always_broadcast == "") {
            $setRequest->setargument('always-broadcast', (($this->always - broadcast == true) ? "true" : "false"));
        }
        if ($this->block_access != "") {
            $setRequest->setargument('block-access', (($this->block_access == true) ? "true" : "false"));
        }
        if ($this->use_src_mac != "") {
            $setRequest->setargument('use_src_mac', $this->use_src_mac);
        }
        if ($this->client_id != "") {
            $setRequest->setargument('client-id', $this->client_id);
        }
        if ($this->lease_time != "") {
            $setRequest->setargument('lease-time', $this->lease_time);
        }
        if ($this->dhcp_option != "") {
            $setRequest->setargument('dhcp-option', $this->dhcp_option);
        }
        if ($this->status != "") {
            $setRequest->setargument('status', $this->status);
        }
        if ($this->last_seen != "") {
            $setRequest->setargument('last-seen', $this->last_seen);
        }
        if ($this->radius != "") {
            $setRequest->setargument('radius', (($this->radius == true) ? "true" : "false"));
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->blocked != "") {
            $setRequest->setargument('blocked', (($this->blocked == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $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 disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/lease/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/lease/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->src_mac_address != "") {
            $setRequest->setargument('src-mac-address', $this->src_mac_address);
        }
        if ($this->use_mac_address != "") {
            $setRequest->setargument('use-mac-address', $this->use_mac_address);
        }
        if ($this->address_lists != "") {
            $setRequest->setargument('address-lists', $this->address_lists);
        }
        if ($this->always_broadcast == "0") {
            $setRequest->setargument('always-broadcast', 'no');
        }
        if ($this->always_broadcast == "1") {
            $setRequest->setargument('always-broadcast', 'yes');
        }
        if ($this->use_src_mac != "") {
            $setRequest->setargument('use_src_mac', $this->use_src_mac);
        }
        if ($this->block_access != "") {
            $setRequest->setargument('block-access', (($this->block_access == '0') ? "false" : "true"));
        }
        if ($this->client_id != "") {
            $setRequest->setargument('client-id', $this->client_id);
        }
        if ($this->rate_limit != "") {
            $setRequest->setargument('rate-limit', $this->rate_limit);
        }
        if ($this->use_src_mac != "") {
            //$setRequest->setargument('use-src-mac', (($this->use_src_mac == '0') ? "false" : "true"));
        }
        if ($this->lease_time != "") {
            $setRequest->setargument('lease-time', $this->lease_time);
        }
        if ($this->dhcp_option != "") {
            $setRequest->setargument('dhcp-option', $this->dhcp_option);
        }
        if ($this->status != "") {
            $setRequest->setargument('status', $this->status);
        }
        if ($this->last_seen != "") {
            $setRequest->setargument('last-seen', $this->last_seen);
        }
        if ($this->radius != "") {
            $setRequest->setargument('radius', (($this->radius == true) ? "true" : "false"));
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->blocked != "") {
            $setRequest->setargument('blocked', (($this->blocked == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $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();

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

}
