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

    public $enabled;
    public $certificate;
    public $ca_certificate;
    public $require_peer_certificate;
    public $generated_certificate;
    public $generated_ca_certificate;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['certificate', 'string'],
            ['ca_certificate', 'string'],
            ['require_peer_certificate', 'string'],
            ['generated_certificate', 'string'],
            ['generated_ca_certificate', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/manager/print');

        $response = $connect->getConnection()->sendSync($getRequest);

        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->certificate = $response->getProperty('certificate');
        $this->ca_certificate = $response->getProperty('ca-certificate');
        $this->require_peer_certificate = (($response->getProperty('require-peer-certificate') == 'true') ? true : false);
        $this->generated_certificate = $response->getProperty('generated-certificate');
        $this->generated_ca_certificate = $response->getProperty('generated-ca-certificate');

        return;
    }

    public function update() {
        $c = new Manager();
        $c->get();
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/manager/set');

        $setRequest->setargument('enabled', (($this->enabled == true) ? "true" : "false"));
        if ($this->certificate != $c->certificate) {
            $setRequest->setargument('certificate', $this->certificate);
        }
        if ($this->ca_certificate != $c->ca_certificate) {
            $setRequest->setargument('ca-certificate', $this->ca_certificate);
        }
        $setRequest->setargument('require-peer-certificate', (($this->require_peer_certificate == true) ? "true" : "false"));

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/manager/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;
    }

}
