<?php

namespace app\models\mikrotik\ip\upnp;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Interfacesupnp extends Model {

    public $id;
    public $interface;
    public $type;
    public $dynamic;
    public $disabled;
    public $forced_external_ip;

    public function rules() {
        return [
            ['id', 'string'],
            ['interface', 'string'],
            ['type', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
            ['forced_external_ip', 'ip'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/upnp/interfaces/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Interfacesupnp();
                $c->id = $response->getProperty('.id');
                $c->interface = $response->getProperty('interface');
                $c->type = $response->getProperty('type');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                $c->forced_external_ip = $response->getProperty('forced-external-ip');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/upnp/interfaces/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $this->id = $id;
                $this->interface = $response->getProperty('interface');
                $this->type = $response->getProperty('type');
                $this->forced_external_ip = $response->getProperty('forced-external-ip');
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/upnp/interfaces/set');
        $setRequest->setargument('.id', $id);
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if (($this->forced_external_ip) != "" and ( $this->type == 'external')) {
            $setRequest->setargument('forced-external-ip', $this->forced_external_ip);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }

        $connect->getConnection()->sendSync($setRequest);
    }

    public function disable($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/upnp/interfaces/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/upnp/interfaces/add');


        if (($this->forced_external_ip) != "" and ( $this->type == 'external')) {
            $setRequest->setargument('forced-external-ip', $this->forced_external_ip);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }

        $connect->getConnection()->sendSync($setRequest);
        return;
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip/upnp/interfaces');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
