<?php

namespace app\models\mikrotik\ip\accounting;

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

    public $enabled;
    public $account_local_traffic;
    public $threshold = 256;
    public $accessible_via_web;
    public $address;

    public function rules() {
        return [
            ['enabled', 'string'],
            ['account_local_traffic', 'string'],
            ['threshold', 'string', 'min' => 1, 'max' => 256],
            ['address', 'required'],
            ['address', 'ip', 'subnet' => true],
            ['accessible_via_web', 'string'],
        ];
    }

    public function scenarios() {
        $scenarios = parent::scenarios();
        $scenarios['update'] = ['enabled', 'account_local_traffic', 'threshold'];
        $scenarios['actionWebaccess'] = ['accessible_via_web', 'address'];
        //$scenarios['view'] = [];
        return $scenarios;
    }

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

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

        $response = $connect->getConnection()->sendSync($getRequest);
        $this->enabled = (($response->getProperty('enabled') == 'true') ? true : false);
        $this->account_local_traffic = (($response->getProperty('account-local-traffic') == 'true') ? true : false);
        $this->threshold = $response->getProperty('threshold');
        //$return[] = $this;

        return;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/accounting/set');
        $setRequest->setArgument('enabled', (($this->enabled) ? 'true' : 'false'));
        $setRequest->setArgument('account-local-traffic', (($this->account_local_traffic) ? 'true' : 'false'));
        $setRequest->setArgument('threshold', $this->threshold);

        $connect->getConnection()->sendSync($setRequest);
    }

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

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

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

        return $responses;
    }

    public function getAllWeb() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/accounting/web-access/print');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->accessible_via_web = (($response->getProperty('accessible-via-web') == 'true') ? true : false);
        $this->address = (($response->getProperty('address')));
        //$return[] = $this;

        return;
    }

    public function updateWeb() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/accounting/web-access/set');
        $setRequest->setArgument('address', $this->address);
        $setRequest->setArgument('accessible-via-web', (($this->accessible_via_web) ? 'true' : 'false'));


        $connect->getConnection()->sendSync($setRequest);
        return;
    }

}
