<?php

namespace app\models\mikrotik\ppp;

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

    public $type = 'l2tp-client';
    public $id;
    public $name;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $connect_to;
    public $user;
    public $password;
    public $profile;
    public $keepalive_timeout;
    public $use_ipsec;
    public $ipsec_secret;
    public $allow_fast_path;
    public $add_default_route;
    public $dial_on_demand;
    public $allow;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['connect_to', 'string'],
            ['user', 'string'],
            ['password', 'string'],
            ['profile', 'string'],
            ['keepalive_timeout', 'string'],
            ['use_ipsec', 'string'],
            ['ipsec_secret', 'string'],
            ['allow_fast_path', 'string'],
            ['add_default_route', 'string'],
            ['dial_on_demand', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getProfiles() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/profile/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 getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/l2tp-client/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new L2tpclient();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->max_mtu = $response->getProperty('max-mtu');
                $c->max_mru = $response->getProperty('max-mru');
                $c->mrru = $response->getProperty('mrru');
                $c->connect_to = $response->getProperty('connect-to');
                $c->user = $response->getProperty('user');
                $c->password = $response->getProperty('password');
                $c->profile = $response->getProperty('profile');
                $c->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $c->use_ipsec = $response->getProperty('use-ipsec');
                $c->ipsec_secret = $response->getProperty('ipsec-secret');
                $c->allow_fast_path = $response->getProperty('allow-fast-path');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->dial_on_demand = $response->getProperty('dial-on-demand');
                $c->allow = $response->getProperty('allow');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/l2tp-client/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->max_mtu = $response->getProperty('max-mtu');
                $this->max_mru = $response->getProperty('max-mru');
                $this->mrru = $response->getProperty('mrru');
                $this->connect_to = $response->getProperty('connect-to');
                $this->user = $response->getProperty('user');
                $this->password = $response->getProperty('password');
                $this->profile = $response->getProperty('profile');
                $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $this->use_ipsec = (($response->getProperty('use-ipsec') == 'true') ? true : false);
                $this->ipsec_secret = $response->getProperty('ipsec-secret');
                $this->allow_fast_path = (($response->getProperty('allow-fast-path') == 'true') ? true : false);
                $this->add_default_route = (($response->getProperty('add-default-route') == 'true') ? true : false);
                $this->dial_on_demand = (($response->getProperty('dial-on-demand') == 'true') ? true : false);
                $this->allow = $response->getProperty('allow');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new L2tpclient();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/l2tp-client/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->max_mtu != $c->max_mtu) {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->max_mru != $c->max_mru) {
            $setRequest->setargument('max-mru', $this->max_mru);
        }
        if ($this->mrru != $c->mrru) {
            $setRequest->setargument('mrru', $this->mrru);
        }
        if ($this->connect_to != $c->connect_to) {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->user != $c->user) {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != $c->password) {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != $c->profile) {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        $setRequest->setargument('use-ipsec', (($this->use_ipsec == true) ? "true" : "false"));
        if ($this->ipsec_secret != $c->ipsec_secret) {
            $setRequest->setargument('ipsec-secret', $this->ipsec_secret);
        }
        $setRequest->setargument('allow-fast-path', (($this->allow_fast_path == true) ? "true" : "false"));
        $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        if ($this->allow != $c->allow) {
            $setRequest->setargument('allow', $this->allow);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/l2tp-client/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/l2tp-client/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/l2tp-client/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->max_mtu != "") {
            $setRequest->setargument('max-mtu', $this->max_mtu);
        }
        if ($this->max_mru != "") {
            $setRequest->setargument('max-mru', $this->max_mru);
        }
        if ($this->mrru != "") {
            $setRequest->setargument('mrru', $this->mrru);
        }
        if ($this->connect_to != "") {
            $setRequest->setargument('connect-to', $this->connect_to);
        }
        if ($this->user != "") {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->profile != "") {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->keepalive_timeout != "") {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->use_ipsec != "") {
            $setRequest->setargument('use-ipsec', (($this->use_ipsec == true) ? "true" : "false"));
        }
        if ($this->ipsec_secret != "") {
            $setRequest->setargument('ipsec-secret', $this->ipsec_secret);
        }
        if ($this->allow_fast_path != "") {
            $setRequest->setargument('allow-fast-path', (($this->allow_fast_path == true) ? "true" : "false"));
        }
        if ($this->add_default_route != "") {
            $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        }
        if ($this->dial_on_demand != "") {
            $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        }
        if ($this->allow != "") {
            $setRequest->setargument('allow', $this->allow);
        }
        $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/l2tp-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
