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

    public $id;
    public $radio_mac;
    public $hw_supported_modes;
    public $action;
    public $master_configuration;
    public $slave_configurations;
    public $name_prefix;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['radio_mac', 'string'],
            ['action', 'string'],
            ['master_configuration', 'string'],
            ['name_prefix', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/provisioning/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Provisioning();
                $c->id = $response->getProperty('.id');
                $c->radio_mac = $response->getProperty('radio-mac');
                $c->hw_supported_modes = $response->getProperty('hw-supported-modes');
                $c->action = $response->getProperty('action');
                $c->master_configuration = $response->getProperty('master-configuration');
                $c->slave_configurations = $response->getProperty('slave-configurations');
                $c->name_prefix = $response->getProperty('name-prefix');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/provisioning/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->radio_mac = $response->getProperty('radio-mac');
                $this->hw_supported_modes = $response->getProperty('hw-supported-modes');
                $this->action = $response->getProperty('action');
                $this->master_configuration = $response->getProperty('master-configuration');
                $this->slave_configurations = $response->getProperty('slave-configurations');
                $this->name_prefix = $response->getProperty('name-prefix');
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Provisioning();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/provisioning/set');
        $setRequest->setargument('.id', $id);
        if ($this->radio_mac != $c->radio_mac) {
            $setRequest->setargument('radio-mac', $this->radio_mac);
        }
        if ($this->hw_supported_modes != $c->hw_supported_modes) {
            $setRequest->setargument('hw-supported-modes', $this->hw_supported_modes);
        }
        if ($this->action != $c->action) {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->master_configuration != $c->master_configuration) {
            $setRequest->setargument('master-configuration', $this->master_configuration);
        }
        if ($this->slave_configurations != $c->slave_configurations) {
            $setRequest->setargument('slave-configurations', $this->slave_configurations);
        }
        if ($this->name_prefix != $c->name_prefix) {
            $setRequest->setargument('name-prefix', $this->name_prefix);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/provisioning/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/provisioning/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/provisioning/add');
        if ($this->radio_mac != "") {
            $setRequest->setargument('radio-mac', $this->radio_mac);
        }
        if ($this->hw_supported_modes != "") {
            $setRequest->setargument('hw-supported-modes', $this->hw_supported_modes);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->master_configuration != "") {
            $setRequest->setargument('master-configuration', $this->master_configuration);
        }
        if ($this->slave_configurations != "") {
            $setRequest->setargument('slave-configurations', $this->slave_configurations);
        }
        if ($this->name_prefix != "") {
            $setRequest->setargument('name-prefix', $this->name_prefix);
        }
        $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/provisioning');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

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

}
