<?php

namespace app\models\mikrotik\ip\dhcpserver;

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

    public $id;
    public $name;
    public $code;
    public $value;
    public $raw_value;

    public function rules() {

        return [
            ['name', 'required'],
            ['code', 'required'],
            ['name', 'string'],
            ['code', 'string'],
            ['value', 'string'],
        ];
    }

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

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

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

            if ($response->getProperty('.id') != null) {
                $c = new Dhcpserveroptions();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->code = $response->getProperty('code');
                $c->value = $response->getProperty('value');
                $c->raw_value = $response->getProperty('raw_value');

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

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

        $getRequest = new RouterOS\Request('/ip/dhcp-server/option/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->code = $response->getProperty('code');
                $this->value = $response->getProperty('value');
                $this->raw_value = $response->getProperty('raw_value');
            }
        }
        return;
    }

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

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

        $responses = $connect->getConnection()->sendSync($getRequest);
        $ret['none'] = 'none';
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {

                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        $return = $ret;
        return $return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/option/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setArgument('name', $this->name);
        $setRequest->setArgument('code', $this->code);
        $setRequest->setArgument('value', $this->value);
        $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/option/add');
        $setRequest->setArgument('name', $this->name);
        $setRequest->setArgument('code', $this->code);
        $setRequest->setArgument('value', $this->value);
        $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 option');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

//
//    public function getAllTest() {
//        $connect = MikrotikConnect::Instance();
//
//        $getRequest = new RouterOS\Request('/ip/dhcp-client/option/print');
//
//        $responses = $connect->getConnection()->sendSync($getRequest);
//
//        return $responses;
//    }
}
