<?php

namespace app\controllers\mikrotik\routing\bfd;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\bridge\Bridge;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\routing\bfd\Bfd;
use app\models\mikrotik\routing\bfd\Neighbor;
use app\models\mikrotik\Interfaces;

class BfdController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewBFD'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreateBFD'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDeleteBFD'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDisableBFD'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingEnableBFD'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingUpdateBFD'],
                    ],
                    [
                        '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();
        $bdf = new Bfd();
        $bdfs = $bdf->getAll();


        $provide['interfaces'] = new ArrayDataProvider([
            'allModels' => $bdfs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'interval',
                    'min_rx',
                    'multiplier',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $neighbor = new Neighbor();
        $neingbors = $neighbor->getAll();

        $provide['neighbors'] = new ArrayDataProvider([
            'allModels' => $neingbors,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'address',
                    'protocols',
                    'state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Bfd();
        $bridge = new Interfaces();
        $brn = $bridge->names();
        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/routing/bfd/bfd/index'));
            }
        } else {
            $model->get($id);
        }

        return $this->render('/mikrotik/routing/bfd/bfd', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'interface' => $brn,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Bfd();
        $bridge = new Interfaces();
        $brn = $bridge->names();
        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/routing/bfd/bfd/index'));
            }
        }

        return $this->render('/mikrotik/routing/bfd/bfd', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'interface' => $brn,
        ]);
    }

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

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

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

}
