<?php

namespace app\models\mikrotik\capsman;

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

    public $id;
    public $name;
    public $client_to_client_forwarding;
    public $bridge;
    public $bridge_cost;
    public $bridge_horizon;
    public $local_forwarding;
    public $vlan_mode;
    public $vlan_id;

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['client_to_client_forwarding', 'string'],
            ['bridge', 'string'],
            ['bridge_cost', 'string'],
            ['bridge_horizon', 'string'],
            ['local_forwarding', 'string'],
            ['vlan_mode', 'string'],
            ['vlan_id', 'string'],
        ];
    }

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

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

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/datapath/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Datapaths();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->client_to_client_forwarding = $response->getProperty('client-to-client-forwarding');
                $c->bridge = $response->getProperty('bridge');
                $c->bridge_cost = $response->getProperty('bridge-cost');
                $c->bridge_horizon = $response->getProperty('bridge-horizon');
                $c->local_forwarding = $response->getProperty('local-forwarding');
                $c->vlan_mode = $response->getProperty('vlan-mode');
                $c->vlan_id = $response->getProperty('vlan-id');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/caps-man/datapath/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->client_to_client_forwarding = (($response->getProperty('client-to-client-forwarding') == 'true') ? true : false);
                $this->bridge = $response->getProperty('bridge');
                $this->bridge_cost = $response->getProperty('bridge-cost');
                $this->bridge_horizon = $response->getProperty('bridge-horizon');
                $this->local_forwarding = (($response->getProperty('local-forwarding') == 'true') ? true : false);
                $this->vlan_mode = $response->getProperty('vlan-mode');
                $this->vlan_id = $response->getProperty('vlan-id');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Datapaths();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/datapath/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        $setRequest->setargument('client-to-client-forwarding', (($this->client_to_client_forwarding == true) ? "true" : "false"));
        if ($this->bridge != $c->bridge) {
            if ($this->bridge == '') {
                $unset[] = 'bridge';
            } else {
                $setRequest->setargument('bridge', $this->bridge);
            }
        }
        if ($this->bridge_cost != $c->bridge_cost) {
            if ($this->bridge_cost == '') {
                $unset[] = 'bridge-cost';
            } else {
                $setRequest->setargument('bridge-cost', $this->bridge_cost);
            }
        }
        if ($this->bridge_horizon != $c->bridge_horizon) {
            if ($this->bridge_horizon == '') {
                $unset[] = 'bridge-horizon';
            } else {
                $setRequest->setargument('bridge-horizon', $this->bridge_horizon);
            }
        }
        $setRequest->setargument('local-forwarding', (($this->local_forwarding == true) ? "true" : "false"));
        if ($this->vlan_mode != $c->vlan_mode) {
            if ($this->vlan_mode == '') {
                $unset[] = 'vlan-mode';
            } else {
                $setRequest->setargument('vlan-mode', $this->vlan_mode);
            }
        }
        if ($this->vlan_id != $c->vlan_id) {
            if ($this->vlan_id == '') {
                $unset[] = 'vlan-id';
            } else {
                $setRequest->setargument('vlan-id', $this->vlan_id);
            }
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/caps-man/datapath/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('/caps-man/datapath/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/caps-man/datapath/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->client_to_client_forwarding != "") {
            $setRequest->setargument('client-to-client-forwarding', (($this->client_to_client_forwarding == true) ? "true" : "false"));
        }
        if ($this->bridge != "") {
            $setRequest->setargument('bridge', $this->bridge);
        }
        if ($this->bridge_cost != "") {
            $setRequest->setargument('bridge-cost', $this->bridge_cost);
        }
        if ($this->bridge_horizon != "") {
            $setRequest->setargument('bridge-horizon', $this->bridge_horizon);
        }
        if ($this->local_forwarding != "") {
            $setRequest->setargument('local-forwarding', (($this->local_forwarding == true) ? "true" : "false"));
        }
        if ($this->vlan_mode != "") {
            $setRequest->setargument('vlan-mode', $this->vlan_mode);
        }
        if ($this->vlan_id != "") {
            $setRequest->setargument('vlan-id', $this->vlan_id);
        }
        $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('/caps-man/datapath');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
