<?php

namespace app\models\mikrotik\wireless;

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

    public $enabled;
    public $interfaces;
    public $certificate;
    public $lock_to_caps_man;
    public $discovery_interfaces;
    public $caps_man_addresses;
    public $caps_man_names;
    public $caps_man_certificate_common_names;
    public $bridge;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['certificate', 'string'],
            ['lock_to_caps_man', 'string'],
            ['caps_man_addresses', 'string'],
            ['caps_man_names', 'string'],
            ['caps_man_certificate_common_names', 'string'],
            ['bridge', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/wireless/cap/print');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->interfaces = $response->getProperty('interfaces');
        $this->certificate = $response->getProperty('certificate');
        $this->lock_to_caps_man = (($response->getProperty('lock-to-caps-man') == 'true') ? true : false);
        $this->discovery_interfaces = $response->getProperty('discovery-interfaces');
        $this->caps_man_addresses = $response->getProperty('caps-man-addresses');
        $this->caps_man_names = $response->getProperty('caps-man-names');
        $this->caps_man_certificate_common_names = $response->getProperty('caps-man-certificate-common-names');
        $this->bridge = $response->getProperty('bridge');
        return;
    }

    public function update() {
        $c = new Cap();
        $c->get();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/wireless/cap/set');

        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->interfaces != $c->interfaces) {
            $setRequest->setargument('interfaces', $this->interfaces);
        }
        if ($this->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        $setRequest->setargument('lock-to-caps-man', (($this->lock_to_caps_man == true) ? "true" : "false"));
        if ($this->discovery_interfaces != $c->discovery_interfaces) {
            $setRequest->setargument('discovery-interfaces', $this->discovery_interfaces);
        }
        if ($this->caps_man_addresses != $c->caps_man_addresses) {
            $setRequest->setargument('caps-man-addresses', $this->caps_man_addresses);
        }
        if ($this->caps_man_names != $c->caps_man_names) {
            $setRequest->setargument('caps-man-names', $this->caps_man_names);
        }
        if ($this->caps_man_certificate_common_names != $c->caps_man_certificate_common_names) {
            $setRequest->setargument('caps-man-certificate-common-names', $this->caps_man_certificate_common_names);
        }
        if ($this->bridge != $c->bridge) {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/wireless/cap/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 getKeys() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/certificate/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {

                if ($response->getProperty('private-key') == 'true') {
                    $return[$response->getProperty('name')] = $response->getProperty('name');
                }
            }
        }
        return $return;
    }

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

}
