<?php

namespace app\models\mikrotik\routing\rip;

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

    public $id;
    public $chain;
    public $key_id;
    public $key;
    public $from_date;
    public $from_time;
    public $to_date;
    public $active;

    public function rules() {
        return [
            ['id', 'string'],
            ['chain', 'string'],
            ['key_id', 'string'],
            ['key', 'string'],
            ['from_date', 'string'],
            ['from_time', 'string'],
            ['to_date', 'string'],
            ['active', 'string'],
        ];
    }

    public function getChains() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/keys/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('chain')] = $response->getProperty('chain');
            }
        }
        return $return;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/keys/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Key();
                $c->id = $response->getProperty('.id');
                $c->chain = $response->getProperty('chain');
                $c->key_id = $response->getProperty('key-id');
                $c->key = $response->getProperty('key');
                $c->from_date = $response->getProperty('from-date');
                $c->from_time = $response->getProperty('from-time');
                $c->to_date = $response->getProperty('to-date');
                $c->active = $response->getProperty('active');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/rip/keys/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->chain = $response->getProperty('chain');
                $this->key_id = $response->getProperty('key-id');
                $this->key = $response->getProperty('key');
                $this->from_date = $response->getProperty('from-date');
                $this->from_time = $response->getProperty('from-time');
                $this->to_date = $response->getProperty('to-date');
                $this->active = (($response->getProperty('active') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Key();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/rip/keys/set');
        $setRequest->setargument('.id', $id);
        if ($this->chain != $c->chain) {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->key_id != $c->key_id) {
            $setRequest->setargument('key-id', $this->key_id);
        }
        if ($this->key != $c->key) {
            $setRequest->setargument('key', $this->key);
        }
        if ($this->from_date != $c->from_date) {
            $setRequest->setargument('from-date', $this->from_date);
        }
        if ($this->from_time != $c->from_time) {
            $setRequest->setargument('from-time', $this->from_time);
        }
        if ($this->to_date != $c->to_date) {
            $setRequest->setargument('to-date', $this->to_date);
        }

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/rip/keys/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('/routing/rip/keys/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/rip/keys/add');
        if ($this->chain != "") {
            $setRequest->setargument('chain', $this->chain);
        }
        if ($this->key_id != "") {
            $setRequest->setargument('key-id', $this->key_id);
        }
        if ($this->key != "") {
            $setRequest->setargument('key', $this->key);
        }
        if ($this->from_date != "") {
            $setRequest->setargument('from-date', $this->from_date);
        }
        if ($this->from_time != "") {
            $setRequest->setargument('from-time', $this->from_time);
        }
        if ($this->to_date != "") {
            $setRequest->setargument('to-date', $this->to_date);
        }

        $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('/routing/rip/keys');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
