<?php

namespace app\controllers\mikrotik\ip\routes;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\firewall\Mangle;
use app\models\mikrotik\ip\routes\Routes;
use app\models\mikrotik\ip\routes\Nexthops;
use app\models\mikrotik\ip\routes\Rules;
use app\models\mikrotik\ip\routes\Vrf;
use app\models\mikrotik\Interfaces;

class RoutesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewRoutesRoutes'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateRoutesRoutes'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteRoutesRoutes'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableRoutesRoutes'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableRoutesRoutes'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateRoutesRoutes'],
                    ],
                    [
                        '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 actionIndex() {

        $menu = new MikrotikSideMenu();
        $model = new Routes();
        $next = new Nexthops();
        $rules = new Rules();
        $vrf = new Vrf();
        $provr = $rules->getAll();
        $prov = $next->getAll();
        $providervr = $vrf->getAll();
        $provide['vrf'] = new ArrayDataProvider([
            'allModels' => $providervr,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'routing_mark',
                    'interfaces',
                    'route_distinguisher',
                    'import_route_targets',
                    'export_route_targets',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provider = $model->getAll();
        $provide['rules'] = new ArrayDataProvider([
            'allModels' => $provr,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'src_address',
                    'dst_address',
                    'routing_mark',
                    'interface',
                    'action',
                    'table',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['routes'] = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'distance',
                    'dst_address',
                    'gateway_status',
                    'pref_src',
                    'routing_mark',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['next'] = new ArrayDataProvider([
            'allModels' => $prov,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                    'gw_state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/ip/routes/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Routes();
        $model->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Routes();
        $interfaces = new Interfaces();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
            }
        }
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $mangle = new Mangle();
        $packetMark['routing'] = $mangle->getRoutingMark();
        return $this->render('/mikrotik/ip/routes/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'create',
                    'packetMark' => $packetMark,
        ]);
    }

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Routes();
        $id = Yii::$app->request->get('id');
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
            }
        } else {
            $model->get($id);
        }
        $interfaces = new Interfaces();
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $mangle = new Mangle();
        $packetMark['routing'] = $mangle->getRoutingMark();

        return $this->render('/mikrotik/ip/routes/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'update',
                    'packetMark' => $packetMark,
        ]);
    }

    public function actionEnable() {
        $id = Yii::$app->request->get('id');
        $model = new Routes();
        $model->enable($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
    }

    public function actionDisable() {
        $id = Yii::$app->request->get('id');
        $model = new Routes();
        $model->disable($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
    }

}
