<?php

namespace app\models\mikrotik\ip\routes;

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

    public $id;
    public $src_address;
    public $dst_address;
    public $routing_mark;
    public $interface;
    public $action;
    public $table;
    public $inactive;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['src_address', 'ip', 'subnet' => null],
            ['dst_address', 'ip', 'subnet' => null],
            ['routing_mark', 'string'],
            ['interface', 'string'],
            ['action', 'string'],
            ['table', 'string'],
            ['inactive', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/rule/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Rules();
                $c->id = $response->getProperty('.id');
                $c->src_address = $response->getProperty('src-address');
                $c->dst_address = $response->getProperty('dst-address');
                $c->routing_mark = $response->getProperty('routing-mark');
                $c->interface = $response->getProperty('interface');
                $c->action = $response->getProperty('action');
                $c->table = $response->getProperty('table');
                $c->inactive = $response->getProperty('inactive');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/route/rule/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->src_address = $response->getProperty('src-address');
                $this->dst_address = $response->getProperty('dst-address');
                $this->routing_mark = $response->getProperty('routing-mark');
                $this->interface = $response->getProperty('interface');
                $this->action = $response->getProperty('action');
                $this->table = $response->getProperty('table');
                $this->inactive = (($response->getProperty('inactive') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/route/rule/set');
        $setRequest->setargument('.id', $id);

        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->table != "") {
            $setRequest->setargument('table', $this->table);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
    }

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

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

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

        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->routing_mark != "") {
            $setRequest->setargument('routing-mark', $this->routing_mark);
        }
        if ($this->interface != "") {
            $setRequest->setargument('interface', $this->interface);
        }
        if ($this->action != "") {
            $setRequest->setargument('action', $this->action);
        }
        if ($this->table != "") {
            $setRequest->setargument('table', $this->table);
        }
        if ($this->inactive != "") {
            $setRequest->setargument('inactive', (($this->inactive == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

}
