<?php

namespace app\models\mikrotik\capsman;

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

    public $id;
    public $mac_address;
    public $mac_address_mask;
    public $interface;
    public $signal_range;
    public $action;
    public $ap_tx_limit;
    public $client_tx_limit;
    public $private_passphrase;
    public $radius_accounting;
    public $client_to_client_forwarding;
    public $vlan_mode;
    public $vlan_id;

    public function rules() {
        return [
            ['id', 'string'],
            ['mac_address', 'string'],
            ['mac_address_mask', 'string'],
            ['interface', 'string'],
            ['signal_range', 'string'],
            ['action', 'string'],
            ['ap_tx_limit', 'string'],
            ['client_tx_limit', 'string'],
            ['private_passphrase', 'string'],
            ['radius_accounting', 'string'],
            ['client_to_client_forwarding', 'string'],
            ['vlan_mode', 'string'],
            ['vlan_id', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/access-list/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Accesslist();
                $c->id = $response->getProperty('.id');
                $c->mac_address = $response->getProperty('mac-address');
                $c->mac_address_mask = $response->getProperty('mac-address-mask');
                $c->interface = $response->getProperty('interface');
                $c->signal_range = $response->getProperty('signal-range');
                $c->action = $response->getProperty('action');
                $c->ap_tx_limit = $response->getProperty('ap-tx-limit');
                $c->client_tx_limit = $response->getProperty('client-tx-limit');
                $c->private_passphrase = $response->getProperty('private-passphrase');
                $c->radius_accounting = $response->getProperty('radius-accounting');
                $c->client_to_client_forwarding = $response->getProperty('client-to-client-forwarding');
                $c->vlan_mode = $response->getProperty('vlan-mode');
                $c->vlan_id = $response->getProperty('vlan-id');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/access-list/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->mac_address = $response->getProperty('mac-address');
                $this->mac_address_mask = $response->getProperty('mac-address-mask');
                $this->interface = $response->getProperty('interface');
                $this->signal_range = $response->getProperty('signal-range');
                $this->action = $response->getProperty('action');
                $this->ap_tx_limit = $response->getProperty('ap-tx-limit');
                $this->client_tx_limit = $response->getProperty('client-tx-limit');
                $this->private_passphrase = $response->getProperty('private-passphrase');
                $this->radius_accounting = (($response->getProperty('radius-accounting') == 'true') ? true : false);
                $this->client_to_client_forwarding = (($response->getProperty('client-to-client-forwarding') == 'true') ? true : false);
                $this->vlan_mode = $response->getProperty('vlan-mode');
                $this->vlan_id = $response->getProperty('vlan-id');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Accesslist();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/access-list/set');
        $setRequest->setargument('.id', $id);
        if ($this->mac_address != $c->mac_address) {
            if ($this->mac_address == '') {
                $unset[] = 'mac-address';
            } else {
                $setRequest->setargument('mac-address', $this->mac_address);
            }
        }
        if ($this->mac_address_mask != $c->mac_address_mask) {
            if ($this->mac_address_mask == '') {
                $unset[] = 'mac-address-mask';
            } else {
                $setRequest->setargument('mac-address-mask', $this->mac_address_mask);
            }
        }
        if ($this->interface != $c->interface) {
            if ($this->interface == '') {
                $unset[] = 'interface';
            } else {
                $setRequest->setargument('interface', $this->interface);
            }
        }
        if ($this->signal_range != $c->signal_range) {
            if ($this->signal_range == '') {
                $unset[] = 'signal-range';
            } else {
                $setRequest->setargument('signal-range', $this->signal_range);
            }
        }
        if ($this->action != $c->action) {
            if ($this->action == '') {
                $unset[] = 'action';
            } else {
                $setRequest->setargument('action', $this->action);
            }
        }
        if ($this->ap_tx_limit != $c->ap_tx_limit) {
            if ($this->ap_tx_limit == '') {
                $unset[] = 'ap-tx-limit';
            } else {
                $setRequest->setargument('ap-tx-limit', $this->ap_tx_limit);
            }
        }
        if ($this->client_tx_limit != $c->client_tx_limit) {
            if ($this->client_tx_limit == '') {
                $unset[] = 'client-tx-limit';
            } else {
                $setRequest->setargument('client-tx-limit', $this->client_tx_limit);
            }
        }
        if ($this->private_passphrase != $c->private_passphrase) {
            if ($this->private_passphrase == '') {
                $unset[] = 'private-passphrase';
            } else {
                $setRequest->setargument('private-passphrase', $this->private_passphrase);
            }
        }
        $setRequest->setargument('radius-accounting', (($this->radius_accounting == true) ? "true" : "false"));
        $setRequest->setargument('client-to-client-forwarding', (($this->client_to_client_forwarding == true) ? "true" : "false"));
        if ($this->vlan_mode != $c->vlan_mode) {
            if ($this->vlan_mode == '') {
                $unset[] = 'vlan-mode';
            } else {
                $setRequest->setargument('vlan-mode', $this->vlan_mode);
            }
        }
        if ($this->vlan_id != $c->vlan_id) {
            if ($this->vlan_id == '') {
                $unset[] = 'vlan-id';
            } else {
                $setRequest->setargument('vlan-id', $this->vlan_id);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/access-list/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('/caps-man/access-list/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/access-list/add');
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->mac_address_mask != "") {
            $setRequest->setargument('mac-address-mask', $this->mac_address_mask);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->signal_range != "") {
            $setRequest->setargument('signal-range', $this->signal_range);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->ap_tx_limit != "") {
            $setRequest->setargument('ap-tx-limit', $this->ap_tx_limit);
        }
        if ($this->client_tx_limit != "") {
            $setRequest->setargument('client-tx-limit', $this->client_tx_limit);
        }
        if ($this->private_passphrase != "") {
            $setRequest->setargument('private-passphrase', $this->private_passphrase);
        }
        if ($this->radius_accounting != "") {
            $setRequest->setargument('radius-accounting', (($this->radius_accounting == true) ? "true" : "false"));
        }
        if ($this->client_to_client_forwarding != "") {
            $setRequest->setargument('client-to-client-forwarding', (($this->client_to_client_forwarding == true) ? "true" : "false"));
        }
        if ($this->vlan_mode != "") {
            $setRequest->setargument('vlan-mode', $this->vlan_mode);
        }
        if ($this->vlan_id != "") {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        $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('/caps-man/access-list');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

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

}
