<?php

/**
 * @link https://www.humhub.org/
 * @copyright Copyright (c) 2021 HumHub GmbH & Co. KG
 * @license https://www.humhub.com/licences
 */

namespace humhub\modules\collabora\controllers;

use humhub\components\access\ControllerAccess;
use humhub\components\Controller;
use humhub\modules\collabora\Module;
use humhub\modules\collabora\permissions\CanUseCollabora;
use humhub\modules\collabora\services\CollaboraService;
use humhub\modules\file\models\File;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;

/**
 * @property Module $module
 */
class EditorController extends Controller
{
    /**
     * @inheritdoc
     */
    protected function getAccessRules()
    {
        return [
            [ControllerAccess::RULE_PERMISSION => [CanUseCollabora::class]],
        ];
    }

    /**
     * Edit the text file in modal
     *
     * @return string
     */
    public function actionIndex()
    {
        if (!$this->module->getConfiguration()->isConfigured()) {
            throw new ServerErrorHttpException('Collabora Online is not configured!');
        }

        $file = $this->getFile();
        if (!$file->canView()) {
            throw new ForbiddenHttpException('You are not allowed to view this file!');
        }

        $collabora = new CollaboraService();
        return $this->renderAjax('index', [
            'file' => $file,
            'wopiUrl' => $collabora->buildUrl($file, Yii::$app->user->getIdentity()),
        ]);
    }

    protected function getFile(): File
    {
        $guid = Yii::$app->request->get('guid', Yii::$app->request->post('guid'));
        $file = File::findOne(['guid' => $guid]);
        if (empty($file)) {
            throw new NotFoundHttpException(Yii::t('CollaboraModule.base', 'Could not find requested file!'));
        }

        return $file;
    }
}
