<?php

namespace app\models\mikrotik\ip\firewall;

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

    public $id;
    public $address;
    public $timeout;
    public $list;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['address', 'ip', 'subnet' => null],
            ['timeout', 'string'],
            ['list', 'string'],
            ['list', 'required'],
            ['address', 'required'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/address-list/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Addresslists();
                $c->id = $response->getProperty('.id');
                $c->address = $response->getProperty('address');
                $c->timeout = $response->getProperty('timeout');
                $c->list = $response->getProperty('list');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/firewall/address-list/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->address = $response->getProperty('address');
                $this->timeout = $response->getProperty('timeout');
                $this->list = $response->getProperty('list');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/address-list/set');
        $setRequest->setargument('.id', $id);
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->timeout != "") {
            $setRequest->setargument('timeout', $this->timeout);
        }
        if ($this->list != "") {
            $setRequest->setargument('list', $this->list);
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', $this->disabled);
        }
        $connect->getConnection()->sendSync($setRequest);
    }

    public function names() {

        $connect = MikrotikConnect::Instance();
        $printRequest = new RouterOS\Request('/ip/firewall/address-list/getall');
        $responses = $connect->getConnection()->sendSync($printRequest);
        $ret = array();
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret[$response->getProperty('list')] = $response->getProperty('list');
            }
        }
        return $ret;
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/firewall/address-list/add');
        if ($this->list != "") {
            $setRequest->setargument('list', $this->list);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->timeout != "") {
            $setRequest->setargument('timeout', $this->timeout);
        }

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

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

}
