<?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 yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\ipsec\Groups;
use app\models\mikrotik\ip\ipsec\Keys;
use app\models\mikrotik\ip\ipsec\Modeconfig;
use app\models\mikrotik\ip\ipsec\Peers;
use app\models\mikrotik\ip\ipsec\Policies;
use app\models\mikrotik\ip\ipsec\Proposals;
use app\models\mikrotik\ip\ipsec\Users;
use app\models\mikrotik\ip\ipsec\Remote;

class IpsecController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewIPsec'],
                    ],
                    [
                        '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();

        $gr = new Groups();

        $provide['groups'] = new ArrayDataProvider([
            'allModels' => $gr->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $act = new Keys();

        $provide['keys'] = new ArrayDataProvider([
            'allModels' => $act->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'key_size',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $profiles = new Modeconfig();
        $provide['modeconfig'] = new ArrayDataProvider([
            'allModels' => $profiles->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'address_pool',
                    'address_prefix_length',
                    'send_dns',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $secrets = new Peers();
        $provide['peers'] = new ArrayDataProvider([
            'allModels' => $secrets->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                    'port',
                    'proposal_check',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $server = new Policies();
        $provide['policies'] = new ArrayDataProvider([
            'allModels' => $server->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'src_address',
                    'src_port',
                    'dst_address',
                    'dst_address',
                    'protocol',
                    'action',
                    'tunnel',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $interfaces = new Proposals();
        $provide['proposals'] = new ArrayDataProvider([
            'allModels' => $interfaces->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'lifetime',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $remote = new Remote();
        $provide['remote'] = new ArrayDataProvider([
            'allModels' => $remote->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'local_address',
                    'remote_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $users = new Users();
        $provide['users'] = new ArrayDataProvider([
            'allModels' => $users->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

}
