<?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 Sham extends Model {

    public $id;
    public $src_address;
    public $dst_address;
    public $cost;
    public $area;
    public $disabled;

    public function rules() {
        return [
            ['id', 'string'],
            ['src_address', 'string'],
            ['dst_address', 'string'],
            ['cost', 'string'],
            ['area', 'string'],
            ['disabled', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/sham-link/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Sham();
                $c->id = $response->getProperty('.id');
                $c->src_address = $response->getProperty('src-address');
                $c->dst_address = $response->getProperty('dst-address');
                $c->cost = $response->getProperty('cost');
                $c->area = $response->getProperty('area');
                $c->disabled = $response->getProperty('disabled');
                $return[] = $c;
            }
        }
        return $return;
    }

    public function get($id) {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/ospf/sham-link/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->src_address = $response->getProperty('src-address');
                $this->dst_address = $response->getProperty('dst-address');
                $this->cost = $response->getProperty('cost');
                $this->area = $response->getProperty('area');
                $this->disabled = $response->getProperty('disabled');
            }
        }
        return;
    }

    public function update($id) {
        $c = new Sham();
        $c->get($id);
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/sham-link/set');
        $setRequest->setargument('.id', $id);
        if ($this->src_address != $c->src_address) {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->dst_address != $c->dst_address) {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->cost != $c->cost) {
            $setRequest->setargument('cost', $this->cost);
        }
        if ($this->area != $c->area) {
            $setRequest->setargument('area', $this->area);
        }
        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/routing/ospf/sham-link/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/sham-link/disable');
        $setRequest->setArgument('.id', $id);
        $connect->getConnection()->sendSync($setRequest);
        return;
    }

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

    public function create() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/routing/ospf/sham-link/add');
        if ($this->src_address != "") {
            $setRequest->setargument('src-address', $this->src_address);
        }
        if ($this->dst_address != "") {
            $setRequest->setargument('dst-address', $this->dst_address);
        }
        if ($this->cost != "") {
            $setRequest->setargument('cost', $this->cost);
        }
        if ($this->area != "") {
            $setRequest->setargument('area', $this->area);
        }
        $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/sham-link');
        $util->remove(RouterOS\Query::where('.id', $id));
        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;
    }

}
