<?php

namespace app\controllers\mikrotik\ip\ipsec;

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\ip\ipsec\Keys;

class KeysController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateIPsecKeys'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteIPsecKeys'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableIPsecKeys'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableIPsecKeys'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateIPsecKeys'],
                    ],
                    [
                        'actions' => ['export'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipExportIPsecKeys'],
                    ],
                    [
                        'actions' => ['generate'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipGenerateIPsecKeys'],
                    ],
                    [
                        'actions' => ['import'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipImportIPsecKeys'],
                    ],
                    [
                        'actions' => ['reset'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipResetIPsecKeys'],
                    ],
                    [
                        '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() {
        $model = new Keys();
        return var_dump($model->getAll());
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Keys();

        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/ipsec/ipsec/index'));
            }
        } else {
            $model->get($id);
        }

        return $this->render('/mikrotik/ip/ipsec/groups', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

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

        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/ipsec/ipsec/index'));
            }
        }

        return $this->render('/mikrotik/ip/ipsec/groups', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

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

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

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

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

    public function actionGenerate() {
        $menu = new MikrotikSideMenu();
        $model = new Keys();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->generate();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/ipsec/ipsec/index'));
            }
        }

        return $this->render('/mikrotik/ip/ipsec/keysgenerate', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionExport() {
        $menu = new MikrotikSideMenu();
        $model = new Keys();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->export();

            return $this->redirect(Url::toRoute('mikrotik/ip/ipsec/ipsec/index'));
        }

        return $this->render('/mikrotik/ip/ipsec/keysexport', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'keys' => $model->getKeys(),
        ]);
    }

    public function actionImport() {
        $menu = new MikrotikSideMenu();
        $model = new Keys();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->import();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/ipsec/ipsec/index'));
            }
        }

        return $this->render('/mikrotik/ip/ipsec/keysimport', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'files' => $model->getFiles(),
        ]);
    }

}
