<?php
namespace PortalPeople\documentdesigner;

use PortalPeople\documentdesigner\services\OcrService;
use Yii;
use yii\base\Module as BaseModule;
use yii\base\BootstrapInterface;

/**
 * DocumentDesigner Module
 * Standalone, bootstrapped into /NGauge backend.
 */
class Module extends BaseModule implements BootstrapInterface
{
    public $controllerNamespace = 'PortalPeople\\documentdesigner\\controllers';

    /**
     * @var array Document layouts (HTML or URL references)
     */
    public $layouts = [];

    /**
     * @var string PDF engine (chrome-php)
     */
    public $pdfEngine = 'chrome-php';

    private ?OcrService $ocrService = null;

    /**
     * Bootstrap method to register URL rules and module config
     */
    public function bootstrap($app)
    {
        $this->id = 'documentdesigner';
        if (!isset($app->getModules()[$this->id])) {
            $app->setModule($this->id, $this);
        }
        $app->urlManager->addRules([
            'documentdesigner/<action:[\w\-]+>' => 'documentdesigner/config/<action>',
            'documentdesigner/<controller:[\w\-]+>/<action:[\w\-]+>' => 'documentdesigner/<controller>/<action>',
            'v1/ocr/<action:[\w\-]+>' => 'documentdesigner/api/ocr/<action>',
        ], false);
        self::init();
    }

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        Yii::$app->params['plugins']['documentdesigner'] = [
            'layouts' => $this->layouts,
            'pdfEngine' => $this->pdfEngine,
        ];
    }

    public function getOcrService(): OcrService
    {
        if ($this->ocrService === null) {
            $this->ocrService = new OcrService();
        }

        return $this->ocrService;
    }

    /**
     * Get module version
     */
    public function getVersion()
    {
        return '1.0.0';
    }

    /**
     * Get module config
     */
    public function getConfig()
    {
        return [
            'layouts' => $this->layouts,
            'pdfEngine' => $this->pdfEngine,
            'version' => $this->getVersion(),
        ];
    }
}
