<?php

namespace app\controllers\mikrotik\ip\arp;

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\arp\Arp;
use app\models\mikrotik\Interfaces;

class ArpController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewArp'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateArp'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteArp'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableArp'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableArp'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateArp'],
                    ],
                    [
                        '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 Arp();
        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                    'mac_address',
                    'interface',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Arp();
        $inter = new Interfaces();
        $items = $inter->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/arp/arp/index'));
            }
        }

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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Arp();
        $inter = new Interfaces();
        $items = $inter->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];

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

            $model->getARP($id);
        }
        $visible = false;
        $disabled = false;
        if ($model->dynamic == 'true') {
            $visible = true;
            $disabled = true;
        }
        $flipped = array_flip($items);
        $x = $flipped[$model->interface];

        if ($model->disabled == 'false') {
            $action = 'disable';
        } else {
            $action = 'enable';
        }
        return $this->render('/mikrotik/ip/arp/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $x,
                    'visible' => $visible,
                    'disabled' => $disabled,
                    'action' => $action,
        ]);
    }

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

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

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

}
