<?php

namespace app\models\mikrotik\ip\pool;

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

    public $id;
    public $name;
    public $ranges;
    public $next_pool;
    public $ranges2 = null;

    public function rules() {
        return [
            ['name', 'required'],
            ['name', 'string', 'min' => 1, 'max' => 255],
            ['ranges', 'required'],
            ['ranges', 'ip', 'subnet' => null],
            ['ranges2', 'ip'],
            ['next_pool', 'string'],
        ];
    }

    public function createPool() {
        $connect = MikrotikConnect::Instance();

        $setRequest = new RouterOS\Request('/ip/pool/add');
        $setRequest->setArgument('name', $this->name);
        if ($this->ranges2 != null) {
            $rng = $this->ranges . "-" . $this->ranges2;
            $setRequest->setArgument('ranges', $rng);
        } else {
            $setRequest->setArgument('ranges', $this->ranges);
        }
        $setRequest->setArgument('next-pool', $this->next_pool);
        $connect->getConnection()->sendSync($setRequest);

        return;
    }

    public function getPool($id) {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/pool/getall');
        $getRequest->setQuery(RouterOS\Query::where('name', $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');
                $rn = $response->getProperty('ranges');
                if (strpos($rn, '/') !== false) {
                    $this->ranges = $rn;
                } else if (strpos($rn, '-') !== false) {
                    $rng = explode("-", $response->getProperty('ranges'));
                    $this->ranges = $rng[0];
                    $this->ranges2 = $rng[1];
                } else {
                    $this->ranges = $rn;
                }
                //$this->ranges = $response->getProperty('ranges');
                $this->next_pool = $response->getProperty('next-pool');
            }
        }
        return;
    }

    public function setPool($id) {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/pool/set');
        $setRequest->setArgument('.id', $id);
        $setRequest->setArgument('name', $this->name);
        if ($this->ranges2 != null) {
            $rng = $this->ranges . "-" . $this->ranges2;
            $setRequest->setArgument('ranges', $rng);
        } else {
            $setRequest->setArgument('ranges', $this->ranges);
        }
        $setRequest->setArgument('next-pool', $this->next_pool);
        //$setRequest->setQuery(RouterOS\Query::where('name', $id));
        $ret = $connect->getConnection()->sendSync($setRequest);
        return $ret;
    }

    public function deletePool($name) {
        $connect = MikrotikConnect::Instance();
        $util = new RouterOS\Util($connect->getConnection());
        $util->setMenu('/ip pool');
        $util->remove(RouterOS\Query::where('name', $name));
        return;
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/pool/getall');

        $responses = $connect->getConnection()->sendSync($getRequest);

        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $ret['.id'] = $response->getProperty('.id');
                $ret['name'] = $response->getProperty('name');
                $ret['ranges'] = $response->getProperty('ranges');
                $ret['next_pool'] = $response->getProperty('next-pool');
                $return[] = $ret;
            }
        }
        return $return;
    }

    public function getAllTest() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/pool/getall');

        $responses = $connect->getConnection()->sendSync($getRequest);

        return $responses;
    }

    public function getPoolNames($pool = null) {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/pool/getall');

        $responses = $connect->getConnection()->sendSync($getRequest);
        $return[0] = ('none');
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $return[] = $response->getProperty('name');
            }
        }
        if (isset($pool)) {
            return $this->next_pool = $return[$pool];
        }
        return $return;
    }
    
    public function getNames() {
        $connect = MikrotikConnect::Instance();

        $getRequest = new RouterOS\Request('/ip/pool/getall');
        $return = array();
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {

            if ($response->getProperty('.id') != null) {
                $return[$response->getProperty('name')] = $response->getProperty('name');
            }
        }
        
        return $return;
    }

}
