<?php

namespace app\models\mikrotik\ip\addresses;

use yii\base\Model;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Addresses extends Model {

    public $id;
    public $address;
    public $network;
    public $interface;
    public $actual_interface;
    public $invalid;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['address', 'required'],
            ['address', 'ip', 'subnet' => null],
            ['network', 'ip'],
            ['interface', 'string'],
            ['dynamic', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/address/getall');

        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $c = new Addresses();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->network = $response->getProperty('network');
                $c->interface = $response->getProperty('interface');
                $c->actual_interface = $response->getProperty('actual_interface');
                $c->invalid = $response->getProperty('invalid');
                $c->dynamic = $response->getProperty('dynamic');
                $c->disabled = $response->getProperty('disabled');
                if (($response->getProperty('dynamic') == 'true')) {
                    $c->dynamic = 'D';
                } else {
                    $c->dynamic = '';
                }
                if (($response->getProperty('disabled') == 'true')) {
                    $c->dynamic = 'X';
                }

                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function getAddress($id) {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/address/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->address = $response->getProperty('address');
                $this->network = $response->getProperty('network');
                $this->interface = $response->getProperty('interface');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/address/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setArgument('address', $this->address);
        $setRequest->setArgument('network', $this->network);
        $setRequest->setArgument('interface', $this->interface);
        $setRequest->setArgument('$disabled', $this->disabled);

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

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/address/add');
        $setRequest->setArgument('address', $this->address);
        if (isset($this->network) && ($this->network != null)) {
            $setRequest->setArgument('network', $this->network);
        }
        $setRequest->setArgument('interface', $this->interface);
        $connect->getConnection()->sendSync($setRequest);
    }

    public function delete($id) {
        $connect = MikrotikConnect::Instance();

        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip address');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

    public function getAllTest() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/address/getall');

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

        return $responses;
    }

}
