# frozen_string_literal: true

module RedmineUxToolkit
  module Services
    module AttachmentPreview
      DEFAULTS = {
        'attachment_preview_enabled' => '1',
        'attachment_preview_images' => '1',
        'attachment_preview_pdfs' => '1',
        'attachment_preview_gallery' => '1',
        'attachment_preview_download' => '1'
      }.freeze

      module_function

      def enabled?(settings = plugin_settings)
        feature_enabled?(settings) &&
          (images_enabled?(settings) || pdfs_enabled?(settings))
      end

      def feature_enabled?(settings = plugin_settings)
        enabled_setting?(settings, 'attachment_preview_enabled')
      end

      def images_enabled?(settings = plugin_settings)
        enabled_setting?(settings, 'attachment_preview_images')
      end

      def pdfs_enabled?(settings = plugin_settings)
        enabled_setting?(settings, 'attachment_preview_pdfs')
      end

      def gallery_enabled?(settings = plugin_settings)
        enabled_setting?(settings, 'attachment_preview_gallery')
      end

      def download_enabled?(settings = plugin_settings)
        enabled_setting?(settings, 'attachment_preview_download')
      end

      def plugin_settings
        Setting.plugin_redmine_ux_toolkit || {}
      end

      def enabled_setting?(settings, key)
        settings.fetch(key, DEFAULTS.fetch(key)).to_s == '1'
      end
      private_class_method :enabled_setting?
    end
  end
end
