<?php

namespace app\controllers\mikrotik\ip\firewall;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
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\firewall\Filter;
use app\models\mikrotik\ip\firewall\Addresslists;
use app\models\mikrotik\ip\firewall\Layer7protocols;
use app\models\mikrotik\ip\firewall\Mangle;
use app\models\mikrotik\Interfaces;

class FilterController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateFirewallFilter'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteFirewallFilter'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableFirewallFilter'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableFirewallFilter'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateFirewallFilter'],
                    ],
                    [
                        '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();
        $model = new Filter();

        $model->getAll();

//        $model = new Accounting(['scenario' => 'update']);
//        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//
//            $model->update();
//
//            return $this->redirect(Url::toRoute('mikrotik/ip/accounting/index'));
//        }
//        $model->getAll();

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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $model = new Filter();
        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/firewall/firewall/index'));
            }
        } else {
            $model->get($id);
        }
        $menu = new MikrotikSideMenu();

        $adrList = new Addresslists();
        $interfaces = new Interfaces();
        $protocols = new Layer7protocols();
        $prot = $protocols->names();
        $mangle = new Mangle();
        $packetMark['packet'] = $mangle->getPacketMark();
        $packetMark['connection'] = $mangle->getConnectionMark();
        $packetMark['routing'] = $mangle->getRoutingMark();
        $protocolNames[''] = '';
        foreach ($prot as $value) {
            $protocolNames[$value] = $value;
        }
        $intNames[''] = '';
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $addr = $adrList->names();
        $addrlists[''] = '';
        foreach ($addr as $value) {
            $addrlists[$value] = $value;
        }

        $chains = $model->getChains();


        return $this->render('/mikrotik/ip/firewall/filter/index', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'interNames' => $intNames,
                    'addrlists' => $addrlists,
                    'protocolNames' => $protocolNames,
                    'action' => 'update',
                    'chains' => $chains,
                    'packetMark' => $packetMark,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Filter();
        $adrList = new Addresslists();
        $interfaces = new Interfaces();
        $protocols = new Layer7protocols();
        $prot = $protocols->names();
        $mangle = new Mangle();
        $packetMark['packet'] = $mangle->getPacketMark();
        $packetMark['connection'] = $mangle->getConnectionMark();
        $packetMark['routing'] = $mangle->getRoutingMark();
        $protocolNames[''] = '';
        foreach ($prot as $value) {
            $protocolNames[$value] = $value;
        }
        $intNames[''] = '';
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $addr = $adrList->names();
        $addrlists[''] = '';
        foreach ($addr as $value) {
            $addrlists[$value] = $value;
        }

        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/firewall/firewall/index'));
            }
        }
        $chains = $model->getChains();


        return $this->render('/mikrotik/ip/firewall/filter/index', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'interNames' => $intNames,
                    'addrlists' => $addrlists,
                    'protocolNames' => $protocolNames,
                    'action' => 'create',
                    'chains' => $chains,
                    'packetMark' => $packetMark,
        ]);
    }

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

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

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

}
