<?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\Bridge;
use app\models\mikrotik\bridge\Port;
use app\models\mikrotik\bridge\Filter;
use app\models\mikrotik\bridge\Nat;
use app\models\mikrotik\bridge\Hosts;
use yii\data\ArrayDataProvider;

class BridgeController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ViewBridge'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_CreateBridge'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_DeleteBridge'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_DisableBridge'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_EnableBridge'],
                    ], [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_UpdateBridge'],
                    ],
                    [
                        '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 Bridge();
        $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,
            ],
        ]);
        $filter = new Filter();
        $filters = $filter->getAll();
        $provide['filters'] = new ArrayDataProvider([
            'allModels' => $filters,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'chain',
                    'in_interface',
                    'out_interface',
                    'src_mac_address',
                    'dst_mac_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $nat = new Nat();
        $nats = $nat->getAll();
        $provide['nat'] = new ArrayDataProvider([
            'allModels' => $nats,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'chain',
                    'in_interface',
                    'out_interface',
                    'src_mac_address',
                    'dst_mac_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $host = new Hosts();
        $hosts = $host->getAll();
        $provide['hosts'] = new ArrayDataProvider([
            'allModels' => $hosts,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'mac_address',
                    'on_interface',
                    'age',
                    'bridge',
                ],
            ],
            '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 Bridge();

        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);
        }
        return $this->render('/mikrotik/bridge/bridge', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Bridge();

        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'));
            }
        }
        return $this->render('/mikrotik/bridge/bridge', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

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

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

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

}
