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

    public $type = 'ppp-client';
    public $id;
    public $name;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $port;
    public $data_channel;
    public $info_channel;
    public $apn;
    public $pin;
    public $user;
    public $password;
    public $remote_address;
    public $profile;
    public $phone;
    public $dial_command;
    public $modem_init;
    public $null_modem;
    public $dial_on_demand;
    public $add_default_route;
    public $default_route_distance;
    public $use_peer_dns;
    public $keepalive_timeout;
    public $allow;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['port', 'string'],
            ['data_channel', 'string'],
            ['info_channel', 'string'],
            ['apn', 'string'],
            ['pin', 'string'],
            ['user', 'string'],
            ['password', 'string'],
            ['remote_address', 'string'],
            ['profile', 'string'],
            ['phone', 'string'],
            ['dial_command', 'string'],
            ['modem_init', 'string'],
            ['null_modem', 'string'],
            ['dial_on_demand', 'string'],
            ['add_default_route', 'string'],
            ['default_route_distance', 'string'],
            ['use_peer_dns', 'string'],
            ['keepalive_timeout', 'string'],
            ['allow', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/ppp-client/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Pppclient();
                $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->port = $response->getProperty('port');
                $c->data_channel = $response->getProperty('data-channel');
                $c->info_channel = $response->getProperty('info-channel');
                $c->apn = $response->getProperty('apn');
                $c->pin = $response->getProperty('pin');
                $c->user = $response->getProperty('user');
                $c->password = $response->getProperty('password');
                $c->remote_address = $response->getProperty('remote-address');
                $c->profile = $response->getProperty('profile');
                $c->phone = $response->getProperty('phone');
                $c->dial_command = $response->getProperty('dial-command');
                $c->modem_init = $response->getProperty('modem-init');
                $c->null_modem = $response->getProperty('null-modem');
                $c->dial_on_demand = $response->getProperty('dial-on-demand');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->default_route_distance = $response->getProperty('default-route-distance');
                $c->use_peer_dns = $response->getProperty('use-peer-dns');
                $c->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $c->allow = $response->getProperty('allow');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function getPorts() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/port/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('/interface/ppp-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->port = $response->getProperty('port');
                $this->data_channel = $response->getProperty('data-channel');
                $this->info_channel = $response->getProperty('info-channel');
                $this->apn = $response->getProperty('apn');
                $this->pin = $response->getProperty('pin');
                $this->user = $response->getProperty('user');
                $this->password = $response->getProperty('password');
                $this->remote_address = $response->getProperty('remote-address');
                $this->profile = $response->getProperty('profile');
                $this->phone = $response->getProperty('phone');
                $this->dial_command = $response->getProperty('dial-command');
                $this->modem_init = $response->getProperty('modem-init');
                $this->null_modem = (($response->getProperty('null-modem') == 'true') ? true : false);
                $this->dial_on_demand = (($response->getProperty('dial-on-demand') == 'true') ? true : false);
                $this->add_default_route = (($response->getProperty('add-default-route') == 'true') ? true : false);
                $this->default_route_distance = $response->getProperty('default-route-distance');
                $this->use_peer_dns = (($response->getProperty('use-peer-dns') == 'true') ? true : false);
                $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $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 Pppclient();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ppp-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->port != $c->port) {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->data_channel != $c->data_channel) {
            $setRequest->setargument('data-channel', $this->data_channel);
        }
        if ($this->info_channel != $c->info_channel) {
            $setRequest->setargument('info-channel', $this->info_channel);
        }
        if ($this->apn != $c->apn) {
            $setRequest->setargument('apn', $this->apn);
        }
        if ($this->pin != $c->pin) {
            $setRequest->setargument('pin', $this->pin);
        }
        if ($this->user != $c->user) {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != $c->password) {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->remote_address != $c->remote_address) {
            $setRequest->setargument('remote-address', $this->remote_address);
        }
        if ($this->profile != $c->profile) {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->phone != $c->phone) {
            $setRequest->setargument('phone', $this->phone);
        }
        if ($this->dial_command != $c->dial_command) {
            $setRequest->setargument('dial-command', $this->dial_command);
        }
        if ($this->modem_init != $c->modem_init) {
            $setRequest->setargument('modem-init', $this->modem_init);
        }
        $setRequest->setargument('null-modem', (($this->null_modem == true) ? "true" : "false"));
        $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        if ($this->default_route_distance != $c->default_route_distance) {
            $setRequest->setargument('default-route-distance', $this->default_route_distance);
        }
        $setRequest->setargument('use-peer-dns', (($this->use_peer_dns == true) ? "true" : "false"));
        if ($this->keepalive_timeout != $c->keepalive_timeout) {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        if ($this->allow != $c->allow) {
            $setRequest->setargument('allow', $this->allow);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/ppp-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/ppp-client/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/ppp-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->port != "") {
            $setRequest->setargument('port', $this->port);
        }
        if ($this->data_channel != "") {
            $setRequest->setargument('data-channel', $this->data_channel);
        }
        if ($this->info_channel != "") {
            $setRequest->setargument('info-channel', $this->info_channel);
        }
        if ($this->apn != "") {
            $setRequest->setargument('apn', $this->apn);
        }
        if ($this->pin != "") {
            $setRequest->setargument('pin', $this->pin);
        }
        if ($this->user != "") {
            $setRequest->setargument('user', $this->user);
        }
        if ($this->password != "") {
            $setRequest->setargument('password', $this->password);
        }
        if ($this->remote_address != "") {
            $setRequest->setargument('remote-address', $this->remote_address);
        }
        if ($this->profile != "") {
            $setRequest->setargument('profile', $this->profile);
        }
        if ($this->phone != "") {
            $setRequest->setargument('phone', $this->phone);
        }
        if ($this->dial_command != "") {
            $setRequest->setargument('dial-command', $this->dial_command);
        }
        if ($this->modem_init != "") {
            $setRequest->setargument('modem-init', $this->modem_init);
        }
        if ($this->null_modem != "") {
            $setRequest->setargument('null-modem', (($this->null_modem == true) ? "true" : "false"));
        }
        if ($this->dial_on_demand != "") {
            $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        }
        if ($this->add_default_route != "") {
            $setRequest->setargument('add-default-route', (($this->add_default_route == true) ? "true" : "false"));
        }
        if ($this->default_route_distance != "") {
            $setRequest->setargument('default-route-distance', $this->default_route_distance);
        }
        if ($this->use_peer_dns != "") {
            $setRequest->setargument('use-peer-dns', (($this->use_peer_dns == true) ? "true" : "false"));
        }
        if ($this->keepalive_timeout != "") {
            $setRequest->setargument('keepalive-timeout', $this->keepalive_timeout);
        }
        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/ppp-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
