# frozen_string_literal: true

module RedmineUxToolkit
  module Services
    module ModernUi
      MODULE_SETTINGS = {
        'modern_ui_base' => 'ux-modern-base',
        'modern_ui_header' => 'ux-modern-header',
        'modern_ui_navigation' => 'ux-modern-navigation',
        'modern_ui_issue_list' => 'ux-modern-issue-list',
        'modern_ui_issue_detail' => 'ux-modern-issue-detail',
        'modern_ui_forms' => 'ux-modern-forms',
        'modern_ui_filters' => 'ux-modern-filters',
        'modern_ui_project_overview' => 'ux-modern-project-overview',
        'modern_ui_roadmap' => 'ux-modern-roadmap',
        'modern_ui_activity' => 'ux-modern-activity',
        'modern_ui_wiki' => 'ux-modern-wiki',
        'modern_ui_repository' => 'ux-modern-repository',
        'modern_ui_login' => 'ux-modern-login',
        'modern_ui_admin' => 'ux-modern-admin'
      }.freeze

      DEFAULT_MODULE_VALUES = {
        'modern_ui_base' => '1',
        'modern_ui_header' => '1',
        'modern_ui_navigation' => '1',
        'modern_ui_issue_list' => '1',
        'modern_ui_issue_detail' => '1',
        'modern_ui_forms' => '1',
        'modern_ui_filters' => '1',
        'modern_ui_project_overview' => '0',
        'modern_ui_roadmap' => '0',
        'modern_ui_activity' => '0',
        'modern_ui_wiki' => '0',
        'modern_ui_repository' => '0',
        'modern_ui_login' => '1',
        'modern_ui_admin' => '0'
      }.freeze

      SETTING_LABELS = {
        'modern_ui_base' => :label_ux_toolkit_modern_ui_base,
        'modern_ui_header' => :label_ux_toolkit_modern_ui_header,
        'modern_ui_navigation' => :label_ux_toolkit_modern_ui_navigation,
        'modern_ui_issue_list' => :label_ux_toolkit_modern_ui_issue_list,
        'modern_ui_issue_detail' => :label_ux_toolkit_modern_ui_issue_detail,
        'modern_ui_forms' => :label_ux_toolkit_modern_ui_forms,
        'modern_ui_filters' => :label_ux_toolkit_modern_ui_filters,
        'modern_ui_project_overview' => :label_ux_toolkit_modern_ui_project_overview,
        'modern_ui_roadmap' => :label_ux_toolkit_modern_ui_roadmap,
        'modern_ui_activity' => :label_ux_toolkit_modern_ui_activity,
        'modern_ui_wiki' => :label_ux_toolkit_modern_ui_wiki,
        'modern_ui_repository' => :label_ux_toolkit_modern_ui_repository,
        'modern_ui_login' => :label_ux_toolkit_modern_ui_login,
        'modern_ui_admin' => :label_ux_toolkit_modern_ui_admin
      }.freeze

      SETTING_HELP = {
        'modern_ui_issue_list' => :text_ux_toolkit_modern_ui_issue_list_help,
        'modern_ui_issue_detail' => :text_ux_toolkit_modern_ui_issue_detail_help,
        'modern_ui_forms' => :text_ux_toolkit_modern_ui_forms_help,
        'modern_ui_filters' => :text_ux_toolkit_modern_ui_filters_help,
        'modern_ui_project_overview' => :text_ux_toolkit_modern_ui_project_overview_help,
        'modern_ui_roadmap' => :text_ux_toolkit_modern_ui_roadmap_help,
        'modern_ui_activity' => :text_ux_toolkit_modern_ui_activity_help,
        'modern_ui_wiki' => :text_ux_toolkit_modern_ui_wiki_help,
        'modern_ui_repository' => :text_ux_toolkit_modern_ui_repository_help,
        'modern_ui_login' => :text_ux_toolkit_modern_ui_login_help,
        'modern_ui_admin' => :text_ux_toolkit_modern_ui_admin_help
      }.freeze

      SETTING_GROUPS = {
        general: %w[modern_ui_base modern_ui_header modern_ui_navigation],
        issue_pages: %w[modern_ui_issue_list modern_ui_issue_detail modern_ui_forms modern_ui_filters],
        project_pages: %w[modern_ui_project_overview modern_ui_roadmap modern_ui_activity modern_ui_wiki modern_ui_repository],
        system_pages: %w[modern_ui_login modern_ui_admin]
      }.freeze

      COLOR_PRESETS = %w[redmine_modern blue neutral dark_header].freeze
      DENSITIES = %w[comfortable compact].freeze

      module_function

      def enabled?(settings = plugin_settings)
        settings.fetch('modern_ui_enabled', '0').to_s == '1'
      end

      def body_classes(settings = plugin_settings)
        return [] unless enabled?(settings)

        classes = ['ux-modern-ui']
        MODULE_SETTINGS.each do |setting_key, class_name|
          classes << class_name if settings.fetch(setting_key, DEFAULT_MODULE_VALUES[setting_key]).to_s == '1'
        end

        classes << density_class(settings)
        classes << color_class(settings)
        classes << redmine_version_class
        classes.compact.uniq
      end

      def adapter_stylesheet
        'redmine_ux_toolkit/modern_ui_redmine_6'
      end

      def plugin_settings
        Setting.plugin_redmine_ux_toolkit || {}
      end

      def redmine_version_class
        'ux-redmine-6'
      end

      def module_enabled?(settings, setting_key)
        default_value = DEFAULT_MODULE_VALUES.fetch(setting_key, '0')
        settings.fetch(setting_key, default_value).to_s == '1'
      end

      def density_class(settings)
        density = settings.fetch('modern_ui_density', 'comfortable').to_s
        density = 'comfortable' unless DENSITIES.include?(density)
        "ux-density-#{density}"
      end

      def color_class(settings)
        preset = settings.fetch('modern_ui_color_preset', 'redmine_modern').to_s
        preset = 'redmine_modern' unless COLOR_PRESETS.include?(preset)
        "ux-color-#{preset.tr('_', '-')}"
      end
    end
  end
end
