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

    public $id;
    public $name;
    public $start_date;
    public $start_time;
    public $interval;
    public $on_event;
    public $owner;
    public $policy;
    public $run_count;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['start_date', 'string'],
            ['start_time', 'string'],
            ['interval', 'string'],
            ['on_event', 'string'],
            ['owner', 'string'],
            ['run_count', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/scheduler/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Scheduler();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->start_date = $response->getProperty('start-date');
                $c->start_time = $response->getProperty('start-time');
                $c->interval = $response->getProperty('interval');
                $c->on_event = $response->getProperty('on-event');
                $c->owner = $response->getProperty('owner');
                $c->policy = $response->getProperty('policy');
                $c->run_count = $response->getProperty('run-count');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/system/scheduler/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->start_date = $response->getProperty('start-date');
                $this->start_time = $response->getProperty('start-time');
                $this->interval = $response->getProperty('interval');
                $this->on_event = $response->getProperty('on-event');
                $this->owner = $response->getProperty('owner');
                $this->policy = $response->getProperty('policy');
                $this->run_count = $response->getProperty('run-count');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Scheduler();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/system/scheduler/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->start_date != $c->start_date) {
            $setRequest->setargument('start-date', $this->start_date);
        }
        if ($this->start_time != $c->start_time) {
            $setRequest->setargument('start-time', $this->start_time);
        }
        if ($this->interval != $c->interval) {
            $setRequest->setargument('interval', $this->interval);
        }
        if ($this->on_event != $c->on_event) {
            $setRequest->setargument('on-event', $this->on_event);
        }
        if ($this->owner != $c->owner) {
            $setRequest->setargument('owner', $this->owner);
        }
        if ($this->policy != $c->policy) {
            $setRequest->setargument('policy', $this->policy);
        }
        if ($this->run_count != $c->run_count) {
            $setRequest->setargument('run-count', $this->run_count);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/system/scheduler/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/scheduler/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/system/scheduler/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->start_date != "") {
            $setRequest->setargument('start-date', $this->start_date);
        }
        if ($this->start_time != "") {
            $setRequest->setargument('start-time', $this->start_time);
        }
        if ($this->interval != "") {
            $setRequest->setargument('interval', $this->interval);
        }
        if ($this->on_event != "") {
            $setRequest->setargument('on-event', $this->on_event);
        }
        if ($this->owner != "") {
            $setRequest->setargument('owner', $this->owner);
        }
        if ($this->policy != "") {
            $setRequest->setargument('policy', $this->policy);
        }
        if ($this->run_count != "") {
            $setRequest->setargument('run-count', $this->run_count);
        }
        $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/scheduler');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
