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

    public $id;
    public $name;
    public $directory;
    public $max_sessions;
    public $inactive;
    public $default;
    public $disabled;
    public $comment;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['directory', 'string'],
            ['max_sessions', 'string'],
            ['inactive', 'string'],
            ['default', 'string'],
            ['disabled', 'string'],
            ['comment', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/smb/shares/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Shares();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->directory = $response->getProperty('directory');
                $c->max_sessions = $response->getProperty('max-sessions');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/smb/shares/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->directory = $response->getProperty('directory');
                $this->max_sessions = $response->getProperty('max-sessions');
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->comment = $response->getProperty('comment');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/smb/shares/set');
        $setRequest->setargument('.id', $id);

        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->directory != "") {
            $setRequest->setargument('directory', $this->directory);
        }
        if ($this->max_sessions != "") {
            $setRequest->setargument('max-sessions', $this->max_sessions);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == 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"));
        }
        if ($this->comment != "") {
            $setRequest->setargument('comment', $this->comment);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/smb/shares/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->directory != "") {
            $setRequest->setargument('directory', $this->directory);
        }
        if ($this->max_sessions != "") {
            $setRequest->setargument('max-sessions', $this->max_sessions);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == 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"));
        }
        if ($this->comment != "") {
            $setRequest->setargument('comment', $this->comment);
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

}
