<?php

namespace app\controllers\mikrotik\bridge;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\bridge\Filter;
use app\models\mikrotik\bridge\Port;
use app\models\mikrotik\bridge\Bridge;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\Interfaces;
use app\models\mikrotik\ip\firewall\Mangle;

class FilterController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeCreateFilter'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeDeleteFilter'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeDisableFilter'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeEnableFilter'],
                    ], [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeUpdateFilter'],
                    ], [
                        'actions' => ['reset'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeResetFilter'],
                    ],
                    [
                        '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 Filter();
        $bridge = $model->getAll();
        $provide['bridge'] = new ArrayDataProvider([
            'allModels' => $bridge,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'l2mtu',
                    'mac_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $port = new Port();
        $ports = $port->getAll();
        $provide['ports'] = new ArrayDataProvider([
            'allModels' => $ports,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'bridge',
                    'priority',
                    'path_cost',
                    'horizon',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('/mikrotik/bridge/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Filter();
        $inter = new Interfaces();
        $et = $inter->getNames();
        $eth[''] = '';
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        $bridge = new Bridge();
        $br = $bridge->getNames();
        $br[''] = '';
        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/bridge/bridge/index'));
            }
        } else {
            $model->get($id);
        }
        $mangle = new Mangle();
        $packetMark = $mangle->getPacketMark();
        return $this->render('/mikrotik/bridge/filter', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'interfaces' => $eth,
                    'bridges' => array_reverse($br),
                    'packetmark' => $packetMark,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Filter();
        $inter = new Interfaces();
        $et = $inter->getNames();
        $eth[''] = '';
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        $bridge = new Bridge();
        $br = $bridge->getNames();
        $br[''] = '';
        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/bridge/bridge/index'));
            }
        }
        $mangle = new Mangle();
        $packetMark = $mangle->getPacketMark();
        return $this->render('/mikrotik/bridge/filter', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'interfaces' => $eth,
                    'bridges' => array_reverse($br),
                    'packetmark' => $packetMark,
        ]);
    }

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

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

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

    public function actionReset() {
        $model = new Filter();
        $model->reset();
        return $this->redirect(Url::toRoute('mikrotik/bridge/bridge/index'));
    }

}
