<?php

namespace app\models\mikrotik\ip\arp;

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 Arp extends Model {

    public $id;
    public $address;
    public $mac_address = '00:00:00:00:00:00';
    public $interface;
    public $published;
    public $invalid;
    public $DHCP;
    public $dynamic;
    public $disabled;

    public function rules() {
        return [
            ['address', 'required'],
            ['address', 'ip'],
            ['mac_address', 'mac'],
            ['interface', 'string'],
            ['dynamic', 'string'],
            ['published', 'string'],
        ];
    }

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

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

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

            if ($response->getProperty('.id') != null) {
                $c = new Arp();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->mac_address = $response->getProperty('mac-address');
                $c->interface = $response->getProperty('interface');
                $c->published = $response->getProperty('published');
                $c->invalid = $response->getProperty('invalid');
                $c->DHCP = $response->getProperty('DHCP');
                $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 getARP($id) {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/arp/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->mac_address = $response->getProperty('mac-address');
                $this->interface = $response->getProperty('interface');
                if ($response->getProperty('published') == 'true') {
                    $this->published = true;
                } else {
                    $this->published = false;
                }
                $this->invalid = $response->getProperty('invalid');
                $this->DHCP = $response->getProperty('DHCP');
                $this->dynamic = $response->getProperty('dynamic');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

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

        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/arp/add');
        $setRequest->setArgument('address', $this->address);
        $setRequest->setArgument('mac-address', $this->mac_address);
        $setRequest->setArgument('interface', $this->interface);
        $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('/ip arp');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

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

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

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

        return $responses;
    }

}
