<?php

namespace app\models\mikrotik\system;

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

    public $id;
    public $owner;
    public $policy;
    public $parent;
    public $started;
    public $type;

    public function rules() {
        return [
            ['id', 'string'],
            ['owner', 'string'],
            ['policy', 'string'],
            ['parent', 'string'],
            ['started', 'string'],
            ['type', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/script/job/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Jobs();
                $c->id = $response->getProperty('.id');
                $c->owner = $response->getProperty('owner');
                $c->policy = $response->getProperty('policy');
                $c->parent = $response->getProperty('parent');
                $c->started = $response->getProperty('started');
                $c->type = $response->getProperty('type');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/script/job/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->owner = $response->getProperty('owner');
                $this->policy = $response->getProperty('policy');
                $this->parent = $response->getProperty('parent');
                $this->started = $response->getProperty('started');
                $this->type = $response->getProperty('type');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Jobs();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/system/script/job/set');
        $setRequest->setargument('.id', $id);
        if ($this->owner != $c->owner) {
            $setRequest->setargument('owner', $this->owner);
        }
        if ($this->policy != $c->policy) {
            $setRequest->setargument('policy', $this->policy);
        }
        if ($this->parent != $c->parent) {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->started != $c->started) {
            $setRequest->setargument('started', $this->started);
        }
        if ($this->type != $c->type) {
            $setRequest->setargument('type', $this->type);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/system/script/job/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/system/script/job/add');
        if ($this->owner != "") {
            $setRequest->setargument('owner', $this->owner);
        }
        if ($this->policy != "") {
            $setRequest->setargument('policy', $this->policy);
        }
        if ($this->parent != "") {
            $setRequest->setargument('parent', $this->parent);
        }
        if ($this->started != "") {
            $setRequest->setargument('started', $this->started);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }
        $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('/system/script/job');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
