<?php

namespace app\models\mikrotik\routing\ospf;

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

    public $id;
    public $name;
    public $instance;
    public $type;
    public $area_id = '0.0.0.0';
    public $interfaces;
    public $active_interfaces;
    public $neighbors;
    public $adjacent_neighbors;
    public $invalid;
    public $default;
    public $disabled;
    public $inject_summary_lsas = true;
    public $translator_role;
    public $default_cost = '1';

    public function rules() {
        return [
            ['id', 'string'],
            ['name', 'string'],
            ['instance', 'string'],
            ['type', 'string'],
            ['area_id', 'string'],
            ['interfaces', 'string'],
            ['active_interfaces', 'string'],
            ['neighbors', 'string'],
            ['adjacent_neighbors', 'string'],
            ['invalid', 'string'],
            ['default', 'string'],
            ['disabled', 'string'],
            ['inject_summary_lsas', 'string'],
            ['translator_role', 'string'],
            ['default_cost', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/area/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Areas();
                $c->id = $response->getProperty('.id');
                $c->name = $response->getProperty('name');
                $c->instance = $response->getProperty('instance');
                $c->type = $response->getProperty('type');
                $c->area_id = $response->getProperty('area-id');
                $c->interfaces = $response->getProperty('interfaces');
                $c->active_interfaces = $response->getProperty('active-interfaces');
                $c->neighbors = $response->getProperty('neighbors');
                $c->adjacent_neighbors = $response->getProperty('adjacent-neighbors');
                $c->invalid = $response->getProperty('invalid');
                $c->default = $response->getProperty('default');
                $c->disabled = $response->getProperty('disabled');
                $c->inject_summary_lsas = $response->getProperty('inject-summary-lsas');
                $c->translator_role = $response->getProperty('translator-role');
                $c->default_cost = $response->getProperty('default-cost');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function getAreanames() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/area/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 get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/area/print');
        $getRequest->setQuery(RouterOS\Query::where('.id', $id));
        $getRequest2 = new RouterOS\Request('/routing/ospf/area/print/status');
        $getRequest2->setQuery(RouterOS\Query::where('.id', $id));
        $responses2 = $connect->getConnection()->sendSync($getRequest2);
        $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->instance = $response->getProperty('instance');
                $this->type = $response->getProperty('type');
                $this->area_id = $response->getProperty('area-id');
                $this->interfaces = $responses2->getProperty('interfaces');
                $this->active_interfaces = $responses2->getProperty('active-interfaces');
                $this->neighbors = $responses2->getProperty('neighbors');
                $this->adjacent_neighbors = $responses2->getProperty('adjacent-neighbors');
                $this->invalid = (($response->getProperty('invalid') == 'true') ? true : false);
                $this->default = (($response->getProperty('default') == 'true') ? true : false);
                $this->disabled = (($response->getProperty('disabled') == 'true') ? true : false);
                $this->inject_summary_lsas = (($response->getProperty('inject-summary-lsas') == 'true') ? true : false);
                $this->translator_role = $response->getProperty('translator-role');
                $this->default_cost = $response->getProperty('default-cost');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Areas();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/area/set');
        $setRequest->setargument('.id', $id);
        if ($this->name != $c->name) {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->instance != $c->instance) {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->type != $c->type) {
            $setRequest->setargument('type', $this->type);
        }
        if ($this->area_id != $c->area_id) {
            $setRequest->setargument('area-id', $this->area_id);
        }

        $setRequest->setargument('inject-summary-lsas', (($this->inject_summary_lsas == true) ? "true" : "false"));
        if ($this->translator_role != $c->translator_role) {
            $setRequest->setargument('translator-role', $this->translator_role);
        }
        if ($this->default_cost != $c->default_cost) {
            $setRequest->setargument('default-cost', $this->default_cost);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/ospf/area/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('/routing/ospf/area/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/area/add');
        if ($this->name != "") {
            $setRequest->setargument('name', $this->name);
        }
        if ($this->instance != "") {
            $setRequest->setargument('instance', $this->instance);
        }
        if ($this->type != "") {
            $setRequest->setargument('type', $this->type);
        }
        if ($this->area_id != "") {
            $setRequest->setargument('area-id', $this->area_id);
        }
        if ($this->inject_summary_lsas != "") {
            $setRequest->setargument('inject-summary-lsas', (($this->inject_summary_lsas == true) ? "true" : "false"));
        }
        if ($this->translator_role != "") {
            $setRequest->setargument('translator-role', $this->translator_role);
        }
        if ($this->default_cost != "") {
            $setRequest->setargument('default-cost', $this->default_cost);
        }
        $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('/routing/ospf/area');
        $util->remove(RouterOS\Query::where('.id', $id));
        return;
    }

}
