<?php

namespace app\models\mikrotik\interfaces;

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

    public $id;
    public $name;
    public $mtu;
    public $actual_mtu;
    public $l2mtu;
    public $local_address;
    public $remote_address;
    public $dscp;
    public $clamp_tcp_mss;
    public $dont_fragment;
    public $running;
    public $disabled;
    public $keepalive;
    public $ipsec_secret;
    public $type = 'IP Tunnel';

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['local_address', 'ip'],
            ['remote_address', 'ip'],
            ['local_address', 'required'],
            ['remote_address', 'required'],
            ['dscp', 'string'],
            ['clamp_tcp_mss', 'string'],
            ['dont_fragment', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
            ['keepalive', 'string'],
            ['ipsec_secret', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ipip/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Ip();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->actual_mtu = $response->getProperty('actual-mtu');
                $c->l2mtu = $response->getProperty('l2mtu');
                $c->local_address = $response->getProperty('local-address');
                $c->remote_address = $response->getProperty('remote-address');
                $c->dscp = $response->getProperty('dscp');
                $c->dont_fragment = $response->getProperty('dont-fragment');
                $c->clamp_tcp_mss = $response->getProperty('clamp-tcp-mss');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $c->keepalive = $response->getProperty('keepalive');
                $c->ipsec_secret = $response->getProperty('ipsec-secret');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ipip/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->name = $response->getProperty('name');
                $this->mtu = $response->getProperty('mtu');
                $this->actual_mtu = $response->getProperty('actual-mtu');
                $this->l2mtu = $response->getProperty('l2mtu');
                $this->local_address = $response->getProperty('local-address');
                $this->remote_address = $response->getProperty('remote-address');
                $this->dscp = $response->getProperty('dscp');
                $this->clamp_tcp_mss = (($response->getProperty('clamp-tcp-mss') == 'true') ? true : false);
                $this->dont_fragment = $response->getProperty('dont-fragment');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->keepalive = $response->getProperty('keepalive');
                $this->ipsec_secret = $response->getProperty('ipsec-secret');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Ip();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ipip/set');
        $setRequest->setargument('.id', $id);

        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != $c->mtu) {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->actual_mtu != $c->actual_mtu) {
            $setRequest->setargument('actual-mtu', $this->actual_mtu);
        }
        if ($this->l2mtu != $c->l2mtu) {
            $setRequest->setargument('l2mtu', $this->l2mtu);
        }
        if ($this->local_address != $c->local_address) {
            $setRequest->setargument('local-address', $this->local_address);
        }
        if ($this->remote_address != $c->remote_address) {
            $setRequest->setargument('remote-address', $this->remote_address);
        }
        if ($this->dscp != $c->dscp) {
            $setRequest->setargument('dscp', $this->dscp);
        }
        if ($this->clamp_tcp_mss != $c->clamp_tcp_mss) {
            $setRequest->setargument('clamp-tcp-mss', (($this->clamp_tcp_mss == true) ? "true" : "false"));
        }
        if ($this->dont_fragment != $c->dont_fragment) {
            $setRequest->setargument('dont-fragment', $this->dont_fragment);
        }
        if ($this->running != $c->running) {
            $setRequest->setargument('running', $this->running);
        }
        if ($this->disabled != $c->disabled) {
            $setRequest->setargument('disabled', $this->disabled);
        }
        if ($this->keepalive != $c->keepalive) {
            if ($this->keepalive == '') {
                $unset[] = 'keepalive';
            } else {
                $setRequest->setargument('keepalive', $this->keepalive);
            }
        }
        if ($this->ipsec_secret != $c->ipsec_secret) {
            if ($this->ipsec_secret == '') {
                $unset[] = 'ipsec-secret';
            } else {
                $setRequest->setargument('ipsec-secret', $this->ipsec_secret);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/ipip/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('/interface/ipip/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ipip/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != "") {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->actual_mtu != "") {
            $setRequest->setargument('actual-mtu', $this->actual_mtu);
        }
        if ($this->l2mtu != "") {
            $setRequest->setargument('l2mtu', $this->l2mtu);
        }

        if ($this->local_address != "") {
            $setRequest->setargument('local-address', $this->local_address);
        }
        if ($this->remote_address != "") {
            $setRequest->setargument('remote-address', $this->remote_address);
        }

        if ($this->dscp != "") {
            $setRequest->setargument('dscp', $this->dscp);
        }
        if ($this->clamp_tcp_mss != "") {
            $setRequest->setargument('clamp-tcp-mss', (($this->clamp_tcp_mss == true) ? "true" : "false"));
        }
        if ($this->dont_fragment != "") {
            $setRequest->setargument('dont-fragment', (($this->dont_fragment == true) ? "true" : "false"));
        }
        if ($this->running != "") {
            $setRequest->setargument('running', (($this->running == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        if ($this->keepalive != "") {
            $setRequest->setargument('keepalive', $this->keepalive);
        }
        if ($this->ipsec_secret != "") {
            $setRequest->setargument('ipsec-secret', $this->ipsec_secret);
        }
        $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('/interface/ipip');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
