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

    public $type = 'pppoe-client';
    public $id;
    public $name;
    public $max_mtu;
    public $max_mru;
    public $mrru;
    public $interface;
    public $user;
    public $password;
    public $profile;
    public $keepalive_timeout;
    public $service_name;
    public $ac_name;
    public $add_default_route;
    public $default_route_distance;
    public $dial_on_demand;
    public $use_peer_dns;
    public $allow;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['max_mtu', 'string'],
            ['max_mru', 'string'],
            ['mrru', 'string'],
            ['user', 'string'],
            ['password', 'string'],
            ['profile', 'string'],
            ['keepalive_timeout', 'string'],
            ['service_name', 'string'],
            ['ac_name', 'string'],
            ['add_default_route', 'string'],
            ['default_route_distance', 'string'],
            ['dial_on_demand', 'string'],
            ['use_peer_dns', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/pppoe-client/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Pppoeclient();
                $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->interface = $response->getProperty('interface');
                $c->user = $response->getProperty('user');
                $c->password = $response->getProperty('password');
                $c->profile = $response->getProperty('profile');
                $c->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $c->service_name = $response->getProperty('service-name');
                $c->ac_name = $response->getProperty('ac-name');
                $c->add_default_route = $response->getProperty('add-default-route');
                $c->default_route_distance = $response->getProperty('default-route-distance');
                $c->dial_on_demand = $response->getProperty('dial-on-demand');
                $c->use_peer_dns = $response->getProperty('use-peer-dns');
                $c->allow = $response->getProperty('allow');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        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;
    }

    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 get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/pppoe-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->interface = $response->getProperty('interface');
                $this->user = $response->getProperty('user');
                $this->password = $response->getProperty('password');
                $this->profile = $response->getProperty('profile');
                $this->keepalive_timeout = $response->getProperty('keepalive-timeout');
                $this->service_name = $response->getProperty('service-name');
                $this->ac_name = $response->getProperty('ac-name');
                $this->add_default_route = (($response->getProperty('add-default-route') == 'true') ? true : false);
                $this->default_route_distance = $response->getProperty('default-route-distance');
                $this->dial_on_demand = (($response->getProperty('dial-on-demand') == 'true') ? true : false);
                $this->use_peer_dns = (($response->getProperty('use-peer-dns') == '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 Pppoeclient();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/pppoe-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->interface != $c->interface) {
            $setRequest->setargument('interface', $this->interface);
        }
        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);
        }
        if ($this->service_name != $c->service_name) {
            $setRequest->setargument('service-name', $this->service_name);
        }
        if ($this->ac_name != $c->ac_name) {
            $setRequest->setargument('ac-name', $this->ac_name);
        }
        $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('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        $setRequest->setargument('use-peer-dns', (($this->use_peer_dns == true) ? "true" : "false"));
        if ($this->allow != $c->allow) {
            $setRequest->setargument('allow', $this->allow);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/pppoe-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/pppoe-client/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/pppoe-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->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        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->service_name != "") {
            $setRequest->setargument('service-name', $this->service_name);
        }
        if ($this->ac_name != "") {
            $setRequest->setargument('ac-name', $this->ac_name);
        }
        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->dial_on_demand != "") {
            $setRequest->setargument('dial-on-demand', (($this->dial_on_demand == true) ? "true" : "false"));
        }
        if ($this->use_peer_dns != "") {
            $setRequest->setargument('use-peer-dns', (($this->use_peer_dns == 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/pppoe-client');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
