<?php

namespace app\models\mikrotik\interfaces;

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

    public $type = 'Bonding';
    public $id;
    public $name;
    public $mtu = '1500';
    public $mac_address;
    public $arp;
    public $slaves;
    public $mode = 'balance-rr';
    public $primary;
    public $link_monitoring = 'mii';
    public $arp_interval;
    public $arp_ip_targets;
    public $mii_interval = '100';
    public $down_delay = '0';
    public $up_delay = '0';
    public $lacp_rate = '30secs';
    public $transmit_hash_policy;
    public $min_links;
    public $running;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['mtu', 'string'],
            ['mac_address', 'string'],
            ['arp', 'string'],
            ['mode', 'string'],
            ['primary', 'string'],
            ['link_monitoring', 'string'],
            ['arp_interval', 'string'],
            ['arp_ip_targets', 'string'],
            ['mii_interval', 'string'],
            ['down_delay', 'string'],
            ['up_delay', 'string'],
            ['lacp_rate', 'string'],
            ['transmit_hash_policy', 'string'],
            ['min_links', 'string'],
            ['running', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/interface/bonding/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Bonding();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->mtu = $response->getProperty('mtu');
                $c->mac_address = $response->getProperty('mac-address');
                $c->arp = $response->getProperty('arp');
                $c->slaves = $response->getProperty('slaves');
                $c->mode = $response->getProperty('mode');
                $c->primary = $response->getProperty('primary');
                $c->link_monitoring = $response->getProperty('link-monitoring');
                $c->arp_interval = $response->getProperty('arp-interval');
                $c->arp_ip_targets = $response->getProperty('arp-ip-targets');
                $c->mii_interval = $response->getProperty('mii-interval');
                $c->down_delay = $response->getProperty('down-delay');
                $c->up_delay = $response->getProperty('up-delay');
                $c->lacp_rate = $response->getProperty('lacp-rate');
                $c->transmit_hash_policy = $response->getProperty('transmit-hash-policy');
                $c->min_links = $response->getProperty('min-links');
                $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/bonding/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->mac_address = $response->getProperty('mac-address');
                $this->arp = $response->getProperty('arp');
                $this->slaves = $response->getProperty('slaves');
                $this->mode = $response->getProperty('mode');
                $this->primary = $response->getProperty('primary');
                $this->link_monitoring = $response->getProperty('link-monitoring');
                $this->arp_interval = $response->getProperty('arp-interval');
                $this->arp_ip_targets = $response->getProperty('arp-ip-targets');
                $this->mii_interval = $response->getProperty('mii-interval');
                $this->down_delay = $response->getProperty('down-delay');
                $this->up_delay = $response->getProperty('up-delay');
                $this->lacp_rate = $response->getProperty('lacp-rate');
                $this->transmit_hash_policy = $response->getProperty('transmit-hash-policy');
                $this->min_links = $response->getProperty('min-links');
                $this->running = (($response->getProperty('running') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
            }
        }
        return;
    }

    public function update($id) {
        $c = new Bonding();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/interface/bonding/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->mac_address != $c->mac_address) {
            $setRequest->setargument('mac-address', $this->mac_address);
        }
        if ($this->arp != $c->arp) {
            $setRequest->setargument('arp', $this->arp);
        }
        if ($this->slaves != $c->slaves) {
            $setRequest->setargument('slaves', $this->slaves);
        }
        if ($this->mode != $c->mode) {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->primary != $c->primary) {
            $setRequest->setargument('primary', $this->primary);
        }
        if ($this->link_monitoring != $c->link_monitoring) {
            $setRequest->setargument('link-monitoring', $this->link_monitoring);
        }
        if ($this->arp_interval != $c->arp_interval) {
            $setRequest->setargument('arp-interval', $this->arp_interval);
        }
        if ($this->arp_ip_targets != $c->arp_ip_targets) {
            $setRequest->setargument('arp-ip-targets', $this->arp_ip_targets);
        }
        if ($this->mii_interval != $c->mii_interval) {
            $setRequest->setargument('mii-interval', $this->mii_interval);
        }
        if ($this->down_delay != $c->down_delay) {
            $setRequest->setargument('down-delay', $this->down_delay);
        }
        if ($this->up_delay != $c->up_delay) {
            $setRequest->setargument('up-delay', $this->up_delay);
        }
        if ($this->lacp_rate != $c->lacp_rate) {
            $setRequest->setargument('lacp-rate', $this->lacp_rate);
        }
        if ($this->transmit_hash_policy != $c->transmit_hash_policy) {
            $setRequest->setargument('transmit-hash-policy', $this->transmit_hash_policy);
        }
        if ($this->min_links != $c->min_links) {
            $setRequest->setargument('min-links', $this->min_links);
        }
        $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('/interface/bonding/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

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

        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->slaves != "") {
            $setRequest->setargument('slaves', $this->slaves);
        }
        if ($this->mode != "") {
            $setRequest->setargument('mode', $this->mode);
        }
        if ($this->primary != "") {
            $setRequest->setargument('primary', $this->primary);
        }
        if ($this->link_monitoring != "") {
            $setRequest->setargument('link-monitoring', $this->link_monitoring);
        }

        if ($this->mii_interval != "") {
            $setRequest->setargument('mii-interval', $this->mii_interval);
        }
        if ($this->down_delay != "") {
            $setRequest->setargument('down-delay', $this->down_delay);
        }
        if ($this->up_delay != "") {
            $setRequest->setargument('up-delay', $this->up_delay);
        }
        if ($this->lacp_rate != "") {
            $setRequest->setargument('lacp-rate', $this->lacp_rate);
        }
        if ($this->transmit_hash_policy != "") {
            $setRequest->setargument('transmit-hash-policy', $this->transmit_hash_policy);
        }
        $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/bonding');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
