<?php

namespace app\controllers\mikrotik\files;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\files\Files;
use yii\web\UploadedFile;

class FilesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_filesUpload'],
                    ],
                       [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_filesView'],
                    ],
                    [
                        'actions' => ['download'],
                        'allow' => true,
                        'roles' => ['mikrotik_filesDownload'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_filesDelete'],
                    ],
                    [
                        '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 Files();
        $provide = new ArrayDataProvider([
            'allModels' => $certs->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'size',
                    'creation_time',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Files();
        $file = $model->get($id);
        if ($file->type == 'directory') {
            Yii::$app->session->setFlash('message', 'You cannot delete a folder!');
            return Yii::$app->getResponse()->redirect(Url::toRoute('mikrotik/files/files/index'));
        }
        $model->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/files/files/index'));
    }

    public function actionCreate() {
        $model = new Files();
        $menu = new MikrotikSideMenu();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $session = Yii::$app->session;
            $server[$session->get('ip')] = ['login' => $session->get('login'), 'pwd' => $session->get('pwd')];
            $c = UploadedFile::getInstances($model, 'file');
            $c = $c[0];
            $model->upload($server, $c);
            //return var_dump($c);
            return Yii::$app->getResponse()->redirect(Url::toRoute('mikrotik/files/files/index'));
        }

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

    public function actionDownload() {
        $model = new Files();
        $session = Yii::$app->session;
        $id = Yii::$app->request->get('id');
        $file = $model->get($id);
        if ($file->type == 'directory') {
            Yii::$app->session->setFlash('message', 'You cannot download a folder!');
            return Yii::$app->getResponse()->redirect(Url::toRoute('mikrotik/files/files/index'));
        }
        $server[$session->get('ip')] = ['login' => $session->get('login'), 'pwd' => $session->get('pwd')];
        $c = $model->download($server, $file);
        header('Content-Description: File Transfer');
        header('Content-length:' . $file->size);
        header('Content-Disposition: attachment; filename=' . $file->name);
        header("Content-transfer-encoding: binary");
        echo $c;
        return;
    }

}
