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

    public $id;
    public $name;
    public $frequency;
    public $width;
    public $band;
    public $extension_channel;
    public $tx_power;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['frequency', 'string'],
            ['width', 'string'],
            ['band', 'string'],
            ['extension_channel', 'string'],
            ['tx_power', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/channel/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Channels();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->frequency = $response->getProperty('frequency');
                $c->width = $response->getProperty('width');
                $c->band = $response->getProperty('band');
                $c->extension_channel = $response->getProperty('extension-channel');
                $c->tx_power = $response->getProperty('tx-power');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/channel/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->frequency = $response->getProperty('frequency');
                $this->width = $response->getProperty('width');
                $this->band = $response->getProperty('band');
                $this->extension_channel = $response->getProperty('extension-channel');
                $this->tx_power = $response->getProperty('tx-power');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Channels();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/channel/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->frequency != $c->frequency) {
            if ($this->frequency == '') {
                $unset[] = 'frequency';
            } else {
                $setRequest->setargument('frequency', $this->frequency);
            }
        }
        if ($this->width != $c->width) {
            if ($this->width == '') {
                $unset[] = 'width';
            } else {
                $setRequest->setargument('width', $this->width);
            }
        }
        if ($this->band != $c->band) {
            if ($this->band == '') {
                $unset[] = 'band';
            } else {
                $setRequest->setargument('band', $this->band);
            }
        }
        if ($this->extension_channel != $c->extension_channel) {
            if ($this->extension_channel == '') {
                $unset[] = 'extension-channel';
            } else {
                $setRequest->setargument('extension-channel', $this->extension_channel);
            }
        }
        if ($this->tx_power != $c->tx_power) {
            if ($this->tx_power == '') {
                $unset[] = 'tx-power';
            } else {
                $setRequest->setargument('tx-power', $this->tx_power);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/channel/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/channel/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/channel/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->frequency != "") {
            $setRequest->setargument('frequency', $this->frequency);
        }
        if ($this->width != "") {
            $setRequest->setargument('width', $this->width);
        }
        if ($this->band != "") {
            $setRequest->setargument('band', $this->band);
        }
        if ($this->extension_channel != "") {
            $setRequest->setargument('extension-channel', $this->extension_channel);
        }
        if ($this->tx_power != "") {
            $setRequest->setargument('tx-power', $this->tx_power);
        }
        $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/channel');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
