<?php

namespace DZ\ImageShield\Option;

use XF;
use XF\Entity\AddOn;
use XF\Entity\ContentTypeField;
use XF\Option\AbstractOption;

class OverlayScope extends AbstractOption
{
    public const ENABLE_FOR_ALL = 'all';

    public static function renderOption(\XF\Entity\Option $option, array $htmlParams): string
    {
        $contentTypeFields = XF::finder('XF:ContentTypeField')
            ->with('AddOn')
            ->where('field_name', 'attachment_handler_class')
            ->fetch();

        $contentTypes = [];
        /** @var ContentTypeField $contentTypeField */
        foreach ($contentTypeFields as $contentTypeField) {
            /** @var AddOn $addOn */
            $addOn = $contentTypeField->AddOn;
            $contentTypePhrase = XF::app()->getContentTypePhraseName($contentTypeField->content_type, false);
            $phrase = strlen($contentTypePhrase) > 0 ? (string) XF::phrase($contentTypePhrase) : '';
            $contentTypes[$contentTypeField->content_type] = sprintf(
                '%s (%s)',
                strlen($phrase) > 0 ? $phrase : $contentTypeField->content_type,
                $addOn->title
            );
        }

        return static::getTemplate(
            'admin:dz_imageshield_option_template_content_types',
            $option,
            $htmlParams,
            [
                'contentTypes' => $contentTypes,
            ]
        );
    }
}
