<?php

namespace app\controllers\mikrotik\mpls\mpls;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\mesh\Mesh;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\interfaces\Ethernet;
use app\models\mikrotik\mpls\mpls\Ldpinterface;
use app\models\mikrotik\mpls\mpls\Ldpneighbor;
use app\models\mikrotik\mpls\mpls\Ldpfilter;
use app\models\mikrotik\mpls\mpls\Advertise;
use app\models\mikrotik\mpls\mpls\Forwarding;
use app\models\mikrotik\mpls\mpls\Mplsinterface;
use app\models\mikrotik\mpls\mpls\Localbinding;
use app\models\mikrotik\mpls\mpls\Remotebinding;

class MplsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_mplsViewMpls'],
                    ],
                    [
                        '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();
        $ldpint = new Ldpinterface();
        $ldp = $ldpint->getAll();
        $provide['ldpint'] = new ArrayDataProvider([
            'allModels' => $ldp,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'hello_interval',
                    'hold_time',
                    'transport_address',
                    'accept_dynamic_neighbors'
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $neighbor = new Ldpneighbor();
        $neighbors = $neighbor->getAll();
        $provide['ldpneighbor'] = new ArrayDataProvider([
            'allModels' => $neighbors,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'transport',
                    'send_targeted',
                    'peer',
                    'local_transport',
                    'addresses'
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $filter = new Ldpfilter();
        $filters = $filter->getAll();
        $provide['ldpfilter'] = new ArrayDataProvider([
            'allModels' => $filters,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'prefix',
                    'neighbor',
                    'accept',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $advertise = new Advertise();
        $advertises = $advertise->getAll();
        $provide['advertise'] = new ArrayDataProvider([
            'allModels' => $advertises,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'prefix',
                    'neighbor',
                    'advertise',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $forwarding = new Forwarding();
        $forwardings = $forwarding->getAll();
        $provide['forwarding'] = new ArrayDataProvider([
            'allModels' => $forwardings,
            'key' => 'in_label',
            'sort' => [
                'attributes' => [
                    'in_label',
                    'out_labels',
                    'destination',
                    'interface',
                    'nexthop',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $interface = new Mplsinterface();
        $intrerfaces = $interface->getAll();
        $provide['interface'] = new ArrayDataProvider([
            'allModels' => $intrerfaces,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'mpls-mtu',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $local = new Localbinding();
        $locals = $local->getAll();
        $provide['local'] = new ArrayDataProvider([
            'allModels' => $locals,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'dst_address',
                    'label',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $remote = new Remotebinding();
        $remotes = $remote->getAll();
        $provide['remote'] = new ArrayDataProvider([
            'allModels' => $remotes,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'dst_address',
                    'label',
                    'nexthop'
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

}
