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

    public $id;
    public $service_name;
    public $interface;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $authentication;
    public $keepalive_timeout;
    public $one_session_per_host;
    public $max_sessions;
    public $pado_delay;
    public $default_profile;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['service_name', 'string'],
            ['interface', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['keepalive_timeout', 'string'],
            ['one_session_per_host', 'string'],
            ['max_sessions', 'string'],
            ['pado_delay', 'string'],
            ['default_profile', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/pppoe-server/server/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Servers();
                $c->id = $response->getProperty('.id');
                $c->service_name = $response->getProperty('service-name');
                $c->interface = $response->getProperty('interface');
                $c->max_mtu = $response->getProperty('max-mtu');
                $c->max_mru = $response->getProperty('max-mru');
                $c->mrru = $response->getProperty('mrru');
                $c->authentication = $response->getProperty('authentication');
                $c->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $c->one_session_per_host = $response->getProperty('one-session-per-host');
                $c->max_sessions = $response->getProperty('max-sessions');
                $c->pado_delay = $response->getProperty('pado-delay');
                $c->default_profile = $response->getProperty('default-profile');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/pppoe-server/server/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->service_name = $response->getProperty('service-name');
                $this->interface = $response->getProperty('interface');
                $this->max_mtu = $response->getProperty('max-mtu');
                $this->max_mru = $response->getProperty('max-mru');
                $this->mrru = $response->getProperty('mrru');
                $this->authentication = $response->getProperty('authentication');
                $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $this->one_session_per_host = (($response->getProperty('one-session-per-host') == 'true') ? true : false);
                $this->max_sessions = $response->getProperty('max-sessions');
                $this->pado_delay = $response->getProperty('pado-delay');
                $this->default_profile = $response->getProperty('default-profile');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Servers();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/pppoe-server/server/set');
        $setRequest->setargument('.id', $id);
        if ($this->service_name != $c->service_name) {
            $setRequest->setargument('service-name', $this->service_name);
        }
        if ($this->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        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->authentication != $c->authentication) {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        $setRequest->setargument('one-session-per-host', (($this->one_session_per_host == true) ? "true" : "false"));
        if ($this->max_sessions != $c->max_sessions) {
            $setRequest->setargument('max-sessions', $this->max_sessions);
        }
        if ($this->pado_delay != $c->pado_delay) {
            $setRequest->setargument('pado-delay', $this->pado_delay);
        }
        if ($this->default_profile != $c->default_profile) {
            $setRequest->setargument('default-profile', $this->default_profile);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/pppoe-server/server/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/pppoe-server/server/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/pppoe-server/server/add');
        if ($this->service_name != "") {
            $setRequest->setargument('service-name', $this->service_name);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        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->authentication != "") {
            $setRequest->setargument('authentication', $this->authentication);
        }
        if ($this->keepalive_timeout != "") {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->one_session_per_host != "") {
            $setRequest->setargument('one-session-per-host', (($this->one_session_per_host == true) ? "true" : "false"));
        }
        if ($this->max_sessions != "") {
            $setRequest->setargument('max-sessions', $this->max_sessions);
        }
        if ($this->pado_delay != "") {
            $setRequest->setargument('pado-delay', $this->pado_delay);
        }
        if ($this->default_profile != "") {
            $setRequest->setargument('default-profile', $this->default_profile);
        }
        $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/pppoe-server/server');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getInterfaces() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/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 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;
    }

}
