<?php

namespace app\models\mikrotik\mesh;

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

    public $type = 'Mesh';
    public $id;
    public $name;
    public $mtu;
    public $arp;
    public $mac_address;
    public $auto_mac;
    public $admin_mac;
    public $mesh_portal;
    public $hwmp_default_hoplimit;
    public $hwmp_preq_waiting_time;
    public $hwmp_preq_retries;
    public $hwmp_preq_destination_only;
    public $hwmp_preq_reply_and_forward;
    public $hwmp_prep_lifetime;
    public $hwmp_rann_interval;
    public $hwmp_rann_propagation_delay;
    public $hwmp_rann_lifetime;
    public $reoptimize_paths;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['arp', 'string'],
            ['mac_address', 'string'],
            ['auto_mac', 'string'],
            ['admin_mac', 'string'],
            ['mesh_portal', 'string'],
            ['hwmp_default_hoplimit', 'string'],
            ['hwmp_preq_waiting_time', 'string'],
            ['hwmp_preq_retries', 'string'],
            ['hwmp_preq_destination_only', 'string'],
            ['hwmp_preq_reply_and_forward', 'string'],
            ['hwmp_prep_lifetime', 'string'],
            ['hwmp_rann_interval', 'string'],
            ['hwmp_rann_propagation_delay', 'string'],
            ['hwmp_rann_lifetime', 'string'],
            ['reoptimize_paths', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getNames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/mesh/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $ret = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {

                $ret[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        return $ret;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/mesh/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Mesh();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->arp = $response->getProperty('arp');
                $c->mac_address = $response->getProperty('mac-address');
                $c->auto_mac = $response->getProperty('auto-mac');
                $c->admin_mac = $response->getProperty('admin-mac');
                $c->mesh_portal = $response->getProperty('mesh-portal');
                $c->hwmp_default_hoplimit = $response->getProperty('hwmp-default-hoplimit');
                $c->hwmp_preq_waiting_time = $response->getProperty('hwmp-preq-waiting-time');
                $c->hwmp_preq_retries = $response->getProperty('hwmp-preq-retries');
                $c->hwmp_preq_destination_only = $response->getProperty('hwmp-preq-destination-only');
                $c->hwmp_preq_reply_and_forward = $response->getProperty('hwmp-preq-reply-and-forward');
                $c->hwmp_prep_lifetime = $response->getProperty('hwmp-prep-lifetime');
                $c->hwmp_rann_interval = $response->getProperty('hwmp-rann-interval');
                $c->hwmp_rann_propagation_delay = $response->getProperty('hwmp-rann-propagation-delay');
                $c->hwmp_rann_lifetime = $response->getProperty('hwmp-rann-lifetime');
                $c->reoptimize_paths = $response->getProperty('reoptimize-paths');
                $c->running = $response->getProperty('running');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/mesh/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->mtu = $response->getProperty('mtu');
                $this->arp = $response->getProperty('arp');
                $this->mac_address = $response->getProperty('mac-address');
                $this->auto_mac = (($response->getProperty('auto-mac') == 'true') ? true : false);
                $this->admin_mac = $response->getProperty('admin-mac');
                $this->mesh_portal = (($response->getProperty('mesh-portal') == 'true') ? true : false);
                $this->hwmp_default_hoplimit = $response->getProperty('hwmp-default-hoplimit');
                $this->hwmp_preq_waiting_time = $response->getProperty('hwmp-preq-waiting-time');
                $this->hwmp_preq_retries = $response->getProperty('hwmp-preq-retries');
                $this->hwmp_preq_destination_only = (($response->getProperty('hwmp-preq-destination-only') == 'true') ? true : false);
                $this->hwmp_preq_reply_and_forward = (($response->getProperty('hwmp-preq-reply-and-forward') == 'true') ? true : false);
                $this->hwmp_prep_lifetime = $response->getProperty('hwmp-prep-lifetime');
                $this->hwmp_rann_interval = $response->getProperty('hwmp-rann-interval');
                $this->hwmp_rann_propagation_delay = $response->getProperty('hwmp-rann-propagation-delay');
                $this->hwmp_rann_lifetime = $response->getProperty('hwmp-rann-lifetime');
                $this->reoptimize_paths = (($response->getProperty('reoptimize-paths') == 'true') ? true : false);
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Mesh();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/mesh/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != $c->mtu) {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->arp != $c->arp) {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->mac_address != $c->mac_address) {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        $setRequest->setargument('auto-mac', (($this->auto_mac == true) ? "true" : "false"));
        if ($this->admin_mac != $c->admin_mac) {
            $setRequest->setargument('admin-mac', $this->admin_mac);
        }
        $setRequest->setargument('mesh-portal', (($this->mesh_portal == true) ? "true" : "false"));
        if ($this->hwmp_default_hoplimit != $c->hwmp_default_hoplimit) {
            $setRequest->setargument('hwmp-default-hoplimit', $this->hwmp_default_hoplimit);
        }
        if ($this->hwmp_preq_waiting_time != $c->hwmp_preq_waiting_time) {
            $setRequest->setargument('hwmp-preq-waiting-time', $this->hwmp_preq_waiting_time);
        }
        if ($this->hwmp_preq_retries != $c->hwmp_preq_retries) {
            $setRequest->setargument('hwmp-preq-retries', $this->hwmp_preq_retries);
        }
        $setRequest->setargument('hwmp-preq-destination-only', (($this->hwmp_preq_destination_only == true) ? "true" : "false"));
        $setRequest->setargument('hwmp-preq-reply-and-forward', (($this->hwmp_preq_reply_and_forward == true) ? "true" : "false"));
        if ($this->hwmp_prep_lifetime != $c->hwmp_prep_lifetime) {
            $setRequest->setargument('hwmp-prep-lifetime', $this->hwmp_prep_lifetime);
        }
        if ($this->hwmp_rann_interval != $c->hwmp_rann_interval) {
            $setRequest->setargument('hwmp-rann-interval', $this->hwmp_rann_interval);
        }
        if ($this->hwmp_rann_propagation_delay != $c->hwmp_rann_propagation_delay) {
            $setRequest->setargument('hwmp-rann-propagation-delay', $this->hwmp_rann_propagation_delay);
        }
        if ($this->hwmp_rann_lifetime != $c->hwmp_rann_lifetime) {
            $setRequest->setargument('hwmp-rann-lifetime', $this->hwmp_rann_lifetime);
        }
        $setRequest->setargument('reoptimize-paths', (($this->reoptimize_paths == true) ? "true" : "false"));

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/interface/mesh/unset');
            foreach ($unset as $value) {
                $unsetRequest->setArgument('.id', $id);
                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

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

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/mesh/add');
        if ($this->id != "") {
            $setRequest->setargument('.id', $this->id);
        }
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->mtu != "") {
            $setRequest->setargument('mtu', $this->mtu);
        }
        if ($this->arp != "") {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->mac_address != "") {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->auto_mac != "") {
            $setRequest->setargument('auto-mac', (($this->auto_mac == true) ? "true" : "false"));
        }
        if ($this->admin_mac != "") {
            $setRequest->setargument('admin-mac', $this->admin_mac);
        }
        if ($this->mesh_portal != "") {
            $setRequest->setargument('mesh-portal', (($this->mesh_portal == true) ? "true" : "false"));
        }
        if ($this->hwmp_default_hoplimit != "") {
            $setRequest->setargument('hwmp-default-hoplimit', $this->hwmp_default_hoplimit);
        }
        if ($this->hwmp_preq_waiting_time != "") {
            $setRequest->setargument('hwmp-preq-waiting-time', $this->hwmp_preq_waiting_time);
        }
        if ($this->hwmp_preq_retries != "") {
            $setRequest->setargument('hwmp-preq-retries', $this->hwmp_preq_retries);
        }
        if ($this->hwmp_preq_destination_only != "") {
            $setRequest->setargument('hwmp-preq-destination-only', (($this->hwmp_preq_destination_only == true) ? "true" : "false"));
        }
        if ($this->hwmp_preq_reply_and_forward != "") {
            $setRequest->setargument('hwmp-preq-reply-and-forward', (($this->hwmp_preq_reply_and_forward == true) ? "true" : "false"));
        }
        if ($this->hwmp_prep_lifetime != "") {
            $setRequest->setargument('hwmp-prep-lifetime', $this->hwmp_prep_lifetime);
        }
        if ($this->hwmp_rann_interval != "") {
            $setRequest->setargument('hwmp-rann-interval', $this->hwmp_rann_interval);
        }
        if ($this->hwmp_rann_propagation_delay != "") {
            $setRequest->setargument('hwmp-rann-propagation-delay', $this->hwmp_rann_propagation_delay);
        }
        if ($this->hwmp_rann_lifetime != "") {
            $setRequest->setargument('hwmp-rann-lifetime', $this->hwmp_rann_lifetime);
        }
        if ($this->reoptimize_paths != "") {
            $setRequest->setargument('reoptimize-paths', (($this->reoptimize_paths == true) ? "true" : "false"));
        }
        if ($this->running != "") {
            $setRequest->setargument('running', (($this->running == true) ? "true" : "false"));
        }
        if ($this->disabled != "") {
            $setRequest->setargument('disabled', (($this->disabled == true) ? "true" : "false"));
        }
        $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('/interface/mesh');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
