<?php

namespace app\models\mikrotik\ip\smb;

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

    public $id;
    public $name;
    public $password;
    public $read_only;
    public $default;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['password', 'string'],
            ['read_only', 'string'],
            ['default', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/smb/users/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Users();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->password = $response->getProperty('password');
                $c->read_only = $response->getProperty('read-only');
                $c->default = $response->getProperty('default');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/smb/users/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->name = $response->getProperty('name');
                $this->password = $response->getProperty('password');
                $this->read_only = (($response->getProperty('read-only') == 'true') ? true : false);
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/smb/users/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->read_only != "") {
            $setRequest->setargument('read-only', (($this->read_only == true) ? "true" : "false"));
        }
        if ($this->default != "") {
            $setRequest->setargument('default', (($this->default == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/smb/users/add');

        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->read_only != "") {
            $setRequest->setargument('read-only', (($this->read_only == true) ? "true" : "false"));
        }
        if ($this->default != "") {
            $setRequest->setargument('default', (($this->default == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip/smb/users');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
