<?php

namespace app\models\mikrotik\ip\ipsec;

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

    public $id;
    public $name;
    public $auth_algorithms;
    public $enc_algorithms;
    public $lifetime;
    public $pfs_group;
    public $default;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['lifetime', 'string'],
            ['pfs_group', 'string'],
            ['default', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/proposal/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Proposals();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->auth_algorithms = $response->getProperty('auth-algorithms');
                $c->enc_algorithms = $response->getProperty('enc-algorithms');
                $c->lifetime = $response->getProperty('lifetime');
                $c->pfs_group = $response->getProperty('pfs-group');
                $c->default = $response->getProperty('default');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/proposal/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->auth_algorithms = $response->getProperty('auth-algorithms');
                $this->enc_algorithms = $response->getProperty('enc-algorithms');
                $this->lifetime = $response->getProperty('lifetime');
                $this->pfs_group = $response->getProperty('pfs-group');
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Proposals();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/proposal/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->auth_algorithms != $c->auth_algorithms) {
            $setRequest->setargument('auth-algorithms', $this->auth_algorithms);
        }
        if ($this->enc_algorithms != $c->enc_algorithms) {
            $setRequest->setargument('enc-algorithms', $this->enc_algorithms);
        }
        if ($this->lifetime != $c->lifetime) {
            $setRequest->setargument('lifetime', $this->lifetime);
        }
        if ($this->pfs_group != $c->pfs_group) {
            $setRequest->setargument('pfs-group', $this->pfs_group);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/ipsec/proposal/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('/ip/ipsec/proposal/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/proposal/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->auth_algorithms != "") {
            $setRequest->setargument('auth-algorithms', $this->auth_algorithms);
        }
        if ($this->enc_algorithms != "") {
            $setRequest->setargument('enc-algorithms', $this->enc_algorithms);
        }
        if ($this->lifetime != "") {
            $setRequest->setargument('lifetime', $this->lifetime);
        }
        if ($this->pfs_group != "") {
            $setRequest->setargument('pfs-group', $this->pfs_group);
        }
        $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('/ip/ipsec/proposal');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
