<?php

namespace app\controllers\mikrotik\tools;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\tools\Ping;
use app\models\mikrotik\tools\Traceroute;

class ToolsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['trace'],
                        'allow' => true,
                        'roles' => ['mikrotik_toolsTrace'],
                    ],
                    [
                        'actions' => ['ping'],
                        'allow' => true,
                        'roles' => ['mikrotik_toolsPing'],
                    ],
                    [
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionPing() {
        $model = new Ping();
        $menu = new MikrotikSideMenu();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->ping();

            $resp = "Seq\t\t\tHost\t\t\tTime\t\t\tReply Size\t\t\tTTL\t\t\tPacket Loss\t\t\t\n";

            foreach ($c as $val) {
                $z = 0;
                foreach ($val as $x => $y) {
                    if ($z == 1 and $x != null) {
                        $resp.=($y) . "\t";
                    } else if ($x != null) {
                        $resp.=($y) . "\t\t\t";
                    } else {
                        $resp.="\t\t\t";
                    }
                    $z++;
                }
                $resp.="\n";
            }
            $model->response = $resp;
        }
        return $this->render('/mikrotik/tools/ping', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $model->names(),
        ]);
    }

    public
            function actionTrace() {
        $model = new Traceroute();
        $menu = new MikrotikSideMenu();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->trace();

            $resp = "Hop\t\t\tLoss\t\t\tTime\t\t\tAddress\t\t\tStatus\t\t\t\n";
            foreach ($c as $val) {
                foreach ($val as $x => $y) {
                    if ($x == null) {
                        $resp.="\t\t\t";
                    }
                    $resp.=($y) . "\t\t\t";
                }
                $resp.="\n";
            }

            $model->response = $resp;
        }
        return $this->render('/mikrotik/tools/traceroute', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $model->names(),
        ]);
    }

}
