<?php

namespace app\models\mikrotik\ip\dns;

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

    public $id;
    public $name;
    public $address;
    public $ttl = '1d';
    public $dynamic;
    public $regexp;
    public $disabled;

    public function rules() {
        return [

            ['id', 'string'],
            ['name', 'string'],
            ['address', 'required'],
            ['address', 'ip'],
            ['ttl', 'required'],
            ['ttl', 'string'],
            ['dynamic', 'string'],
            ['regexp', 'string'],
            ['disabled', 'string'],
        ];
    }

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

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dns/static/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->name = $response->getProperty('name');
                $this->address = $response->getProperty('address');
                $this->ttl = $response->getProperty('ttl');
                $this->dynamic = (($response->getProperty('dynamic') == 'true') ? true : false);
                $this->regexp = (($response->getProperty('regexp') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dns/static/set');
        $setRequest->setargument('.id', $id);
        if ($this->id != "") {
            $setRequest->setargument('.id', $id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->ttl != "") {
            $setRequest->setargument('ttl', $this->ttl);
        }

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

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dns/static/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->address != "") {
            $setRequest->setargument('address', $this->address);
        }
        if ($this->ttl != "") {
            $setRequest->setargument('ttl', $this->ttl);
        }
        if ($this->dynamic != "") {
            $setRequest->setargument('dynamic', (($this->dynamic == true) ? "true" : "false"));
        }
        if ($this->regexp != "") {
            $setRequest->setargument('regexp', (($this->regexp == 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/dns/static');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
