<?php

namespace app\controllers\mikrotik\system;

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\system\Certificates;
use app\models\mikrotik\system\Scep;

class CertificatesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    'rules' =>
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemViewCertificates'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemCreateCertificates'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDeleteCertificates'],
                    ],
                    [
                        'actions' => ['export'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemExportCertificates'],
                    ],
                    [
                        'actions' => ['import'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemImportCertificates'],
                    ],
                    [
                        'actions' => ['sign'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemSignCertificates'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateCertificates'],
                    ],
                    [
                        '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();
        $certs = new Certificates();
        $provide['cert'] = new ArrayDataProvider([
            'allModels' => $certs->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'common_name',
                    'key_size',
                    'days_valid',
                    'trusted',
                    'ca',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $scep = new Scep();
        $provide['scep'] = new ArrayDataProvider([
            'allModels' => $scep->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'ca_cert',
                    'next_ca_cert',
                    'path',
                    'days_valid',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Certificates();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Certificates']['key_usage'] != '') {
                $opt = implode(",", $_POST['Certificates']['key_usage']);
                $model->key_usage = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/certificates/index'));
            }
        }

        return $this->render('/mikrotik/system/certificate/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Certificates();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Certificates']['key_usage'] != '') {
                $opt = implode(",", $_POST['Certificates']['key_usage']);
                $model->key_usage = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/certificates/index'));
            }
        }
        $model->get($id);
        $model->key_usage = explode(',', $model->key_usage);
        return $this->render('/mikrotik/system/certificate/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionSign() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Certificates();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $c = $model->sign($id);

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

        $model->get($id);
        return $this->render('/mikrotik/system/certificate/sign', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'sign',
                    'ca' => $model->getCa(),
        ]);
    }

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

    public function actionImport() {
        $menu = new MikrotikSideMenu();
        $model = new Certificates();

        if ($model->load(Yii::$app->request->post())) {
            $c = $model->import();
            return $this->redirect(Url::toRoute('mikrotik/system/certificates/index'));
        }

        return $this->render('/mikrotik/system/certificate/import', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'files' => $model->getFiles(),
        ]);
    }

    public function actionExport() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Certificates();

        if ($model->load(Yii::$app->request->post())) {
            $c = $model->export($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/certificates/index'));
            }
        }
        $model->get($id);
        return $this->render('/mikrotik/system/certificate/export', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
