<?php

namespace app\controllers\mikrotik\routing\ospf;

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\routing\ospf\Arearouters;
use app\models\mikrotik\routing\ospf\Areas;
use app\models\mikrotik\routing\ospf\Borderrouters;
use app\models\mikrotik\routing\ospf\Instances;
use app\models\mikrotik\routing\ospf\Interfaces;
use app\models\mikrotik\routing\ospf\Links;
use app\models\mikrotik\routing\ospf\Lsa;
use app\models\mikrotik\routing\ospf\Nbma;
use app\models\mikrotik\routing\ospf\Neighbors;
use app\models\mikrotik\routing\ospf\Networks;
use app\models\mikrotik\routing\ospf\Ranges;
use app\models\mikrotik\routing\ospf\Routes;
use app\models\mikrotik\routing\ospf\Sham;

//use app\models\mikrotik\Interfaces;

class OspfController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                  [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewOSPF'],
                    ],
                    [
                        '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();
//        $n = new Nbma();
//        return var_dump($n->getAll());
        $arearouters = new Arearouters();
        $provide['arearouters'] = new ArrayDataProvider([
            'allModels' => $arearouters->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'area',
                    'router_id',
                    'gateway',
                    'interface',
                    'cost',
                    'state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $areas = new Areas();
        $provide['areas'] = new ArrayDataProvider([
            'allModels' => $areas->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'instance',
                    'area_id',
                    'type',
                    'default_cost',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $borderrouters = new Borderrouters();
        $provide['borderrouters'] = new ArrayDataProvider([
            'allModels' => $borderrouters->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'router_id',
                    'gateway',
                    'interface',
                    'cost',
                    'state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $instances = new Instances();
        $provide['instances'] = new ArrayDataProvider([
            'allModels' => $instances->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'router_id',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $interfaces = new Interfaces();
        $provide['interfaces'] = new ArrayDataProvider([
            'allModels' => $interfaces->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'cost',
                    'priority',
                    'authentication',
                    'network_type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $links = new Links();
        $provide['links'] = new ArrayDataProvider([
            'allModels' => $links->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'neighbor_id',
                    'transit_area',
                    'authentication',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $lsa = new Lsa();
        $provide['lsa'] = new ArrayDataProvider([
            'allModels' => $lsa->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'area',
                    'type',
                    'id',
                    'originator',
                    'sequence_number',
                    'age',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $nbma = new Nbma();
        $provide['nbma'] = new ArrayDataProvider([
            'allModels' => $nbma->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'address',
                    'poll_interval',
                    'priority',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $neighbors = new Neighbors();
        $provide['neighbors'] = new ArrayDataProvider([
            'allModels' => $neighbors->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'router_id',
                    'address',
                    'state_changes',
                    'state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $networks = new Networks();
        $provide['networks'] = new ArrayDataProvider([
            'allModels' => $networks->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'network',
                    'area',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $ranges = new Ranges();
        $provide['ranges'] = new ArrayDataProvider([
            'allModels' => $ranges->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'area',
                    'range',
                    'cost',
                    'advertise',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $routes = new Routes();
        $provide['routes'] = new ArrayDataProvider([
            'allModels' => $routes->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'area',
                    'dst_address',
                    'gateway',
                    'interface',
                    'cost',
                    'state',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $sham = new Sham();
        $provide['sham'] = new ArrayDataProvider([
            'allModels' => $sham->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'src_address',
                    'dst_address',
                    'cost',
                    'area',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

}
