<?php

namespace app\models\mikrotik\ip\tftp;

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

    public $id;
    public $ip_addresses;
    public $req_filename;
    public $real_filename;
    public $allow;
    public $read_only;
    public $allow_rollover;
    public $allow_overwrite;
    public $reading_window_size;
    public $hits;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['ip_addresses', 'string'],
            ['req_filename', 'string'],
            ['real_filename', 'string'],
            ['allow', 'string'],
            ['read_only', 'string'],
            ['hits', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/tftp/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new tftp();
                $c->id = $response->getProperty('.id');
                $c->ip_addresses = $response->getProperty('ip-addresses');
                $c->req_filename = $response->getProperty('req-filename');
                $c->real_filename = $response->getProperty('real-filename');
                $c->allow = $response->getProperty('allow');
                $c->read_only = $response->getProperty('read-only');
                $c->allow_rollover = $response->getProperty('allow-rollover');
                $c->allow_overwrite = $response->getProperty('allow-overwrite');
                $c->reading_window_size = $response->getProperty('reading-window-size');
                $c->hits = $response->getProperty('hits');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/tftp/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->ip_addresses = $response->getProperty('ip-addresses');
                $this->req_filename = $response->getProperty('req-filename');
                $this->real_filename = $response->getProperty('real-filename');
                $this->allow = (($response->getProperty('allow') == 'true') ? true : false);
                $this->read_only = (($response->getProperty('read-only') == 'true') ? true : false);
                $this->allow_rollover = (($response->getProperty('allow-rollover') == 'true') ? true : false);
                $this->allow_overwrite = (($response->getProperty('allow-overwrite') == 'true') ? true : false);
                $this->reading_window_size = $response->getProperty('reading-window-size');
                $this->hits = $response->getProperty('hits');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return $this;
    }

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

        if ($this->ip_addresses != "") {
            $setRequest->setargument('ip-addresses', $this->ip_addresses);
        }
        if ($this->req_filename != "") {
            $setRequest->setargument('req-filename', $this->req_filename);
        }
        if ($this->real_filename != "") {
            $setRequest->setargument('real-filename', $this->real_filename);
        }
        if ($this->allow != "") {
            $setRequest->setargument('allow', (($this->allow == true) ? "true" : "false"));
        }
        if ($this->read_only != "") {
            $setRequest->setargument('read-only', (($this->read_only == true) ? "true" : "false"));
        }
        if ($this->allow_rollover != "") {
            $setRequest->setargument('allow-rollover', (($this->allow_rollover == true) ? "true" : "false"));
        }
        if ($this->allow_overwrite != "") {
            $setRequest->setargument('allow-overwrite', (($this->allow_overwrite == true) ? "true" : "false"));
        }
        if ($this->reading_window_size != "") {
            $setRequest->setargument('reading-window-size', $this->reading_window_size);
        }

        return $connect->getConnection()->sendSync($setRequest);
    }

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

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

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

        if ($this->ip_addresses != "") {
            $setRequest->setargument('ip-addresses', $this->ip_addresses);
        }
        if ($this->req_filename != "") {
            $setRequest->setargument('req-filename', $this->req_filename);
        }
        if ($this->real_filename != "") {
            $setRequest->setargument('real-filename', $this->real_filename);
        }
        if ($this->allow != "") {
            $setRequest->setargument('allow', (($this->allow == true) ? "true" : "false"));
        }
        if ($this->read_only != "") {
            $setRequest->setargument('read-only', (($this->read_only == true) ? "true" : "false"));
        }
        if ($this->allow_rollover != "") {
            $setRequest->setargument('allow-rollover', (($this->allow_rollover == true) ? "true" : "false"));
        }
        if ($this->allow_overwrite != "") {
            $setRequest->setargument('allow-overwrite', (($this->allow_overwrite == true) ? "true" : "false"));
        }
        if ($this->reading_window_size != "") {
            $setRequest->setargument('reading-window-size', $this->reading_window_size);
        }
        if ($this->hits != "") {
            $setRequest->setargument('hits', $this->hits);
        }
        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/tftp');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
