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

    public $id;
    public $name;
    public $send_dns;
    public $address_pool;
    public $address_prefix_length;
    public $split_include;
    public $default;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['send_dns', 'string'],
            ['address_pool', 'string'],
            ['address_prefix_length', 'string'],
            ['split_include', 'string'],
            ['default', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/mode-config/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Modeconfig();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->send_dns = $response->getProperty('send-dns');
                $c->address_pool = $response->getProperty('address-pool');
                $c->address_prefix_length = $response->getProperty('address-prefix-length');
                $c->split_include = $response->getProperty('split-include');
                $c->default = $response->getProperty('default');
                $return[] = $c;
            }
        }
        return $return;
    }

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

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/ipsec/mode-config/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->send_dns = (($response->getProperty('send-dns') == 'true') ? true : false);
                $this->address_pool = $response->getProperty('address-pool');
                $this->address_prefix_length = $response->getProperty('address-prefix-length');
                $this->split_include = $response->getProperty('split-include');
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Modeconfig();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/mode-config/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        $setRequest->setargument('send-dns', (($this->send_dns == true) ? "true" : "false"));
        if ($this->address_pool != $c->address_pool) {
            $setRequest->setargument('address-pool', $this->address_pool);
        }
        if ($this->address_prefix_length != $c->address_prefix_length) {
            $setRequest->setargument('address-prefix-length', $this->address_prefix_length);
        }
        if ($this->split_include != $c->split_include) {
            $setRequest->setargument('split-include', $this->split_include);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ip/ipsec/mode-config/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/mode-config/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/ipsec/mode-config/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->send_dns != "") {
            $setRequest->setargument('send-dns', (($this->send_dns == true) ? "true" : "false"));
        }
        if ($this->address_pool != "") {
            $setRequest->setargument('address-pool', $this->address_pool);
        }
        if ($this->address_prefix_length != "") {
            $setRequest->setargument('address-prefix-length', $this->address_prefix_length);
        }
        if ($this->split_include != "") {
            $setRequest->setargument('split-include', $this->split_include);
        }
        $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/mode-config');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
