<?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 Dhcpclientoption 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-client/option/print');

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

            if ($response->getProperty('.id') != null) {
                $c = new Dhcpclientoption();
                $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-client/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 update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-client/option/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setArgument('name', $this->name);
        $setRequest->setArgument('code', $this->code);
        $setRequest->setArgument('value', $this->value);
        $connect->getConnection()->sendSync($setRequest);
    }

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-client/option/add');
        $setRequest->setArgument('name', $this->name);
        $setRequest->setArgument('code', $this->code);
        $setRequest->setArgument('value', $this->value);
        $connect->getConnection()->sendSync($setRequest);
    }

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

        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip dhcp-client 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;
//    }
}
