# frozen_string_literal: true

module RedmineUxToolkit
  module Patches
    module ProjectsHelperPatch
      class << self
        attr_reader :original_project_settings_tabs

        def capture_original_project_settings_tabs(method)
          @original_project_settings_tabs = method
        end
      end

      def render_project_hierarchy(projects)
        bookmarked_project_ids = User.current.bookmarked_project_ids
        favorite_project_ids = ux_toolkit_favorite_project_ids

        render_project_nested_lists(projects) do |project|
          classes = project.css_classes.split
          classes += %w(icon icon-user my-project) if User.current.member_of?(project)
          classes += %w(icon icon-bookmarked-project) if bookmarked_project_ids.include?(project.id)
          classes += %w(icon icon-fav ux-toolkit-favorite-project-icon) if favorite_project_ids.include?(project.id)

          s = link_to_project(project, {}, class: classes.uniq.join(' '))
          if User.current.member_of?(project)
            s << tag.span(
              ux_toolkit_sprite_icon('user', l(:label_my_projects), icon_only: true),
              class: 'icon-only icon-user my-project'
            )
          end
          if bookmarked_project_ids.include?(project.id)
            s << tag.span(
              ux_toolkit_sprite_icon('bookmarked', l(:label_my_bookmarks), icon_only: true),
              class: 'icon-only icon-bookmarked-project'
            )
          end
          if favorite_project_ids.include?(project.id)
            s << ux_toolkit_favorite_project_icon
          end
          if project.description.present?
            s << content_tag('div', textilizable(project, :short_description, project: project), class: 'wiki description')
          end
          s
        end
      end

      def project_settings_tabs
        if @ux_toolkit_resolving_project_settings_tabs
          original_method = ProjectsHelperPatch.original_project_settings_tabs
          return original_method.bind(self).call if original_method
        end

        @ux_toolkit_resolving_project_settings_tabs = true
        begin
          tabs = super
          return tabs unless @project && User.current.allowed_to?(:edit_project, @project)
          return tabs unless ux_toolkit_project_module_enabled?(@project)

          unless tabs.any? {|tab| tab[:name] == 'redmine_ux_toolkit'}
            # Redmine has no view hook for project settings tabs, so the plugin appends
            # one tab here while leaving the core settings view untouched.
            tabs << {
              name: 'redmine_ux_toolkit',
              action: :edit_project,
              partial: 'projects/settings/redmine_ux_toolkit',
              label: :label_ux_toolkit_project_settings
            }
          end

          tabs
        ensure
          remove_instance_variable(:@ux_toolkit_resolving_project_settings_tabs)
        end
      end

      private

      def ux_toolkit_favorite_project_ids
        return [] unless User.current.logged?

        @ux_toolkit_favorite_project_ids ||=
          UxToolkitFavoriteProject.where(user_id: User.current.id).pluck(:project_id)
      end

      def ux_toolkit_favorite_project_icon
        tag.span(
          ux_toolkit_sprite_icon('fav', l(:label_ux_toolkit_favorite_project), icon_only: true),
          class: 'icon-only icon-fav ux-toolkit-favorite-project-icon',
          title: l(:label_ux_toolkit_favorite_project)
        )
      end

      def ux_toolkit_project_module_enabled?(project)
        if project.respond_to?(:module_enabled?)
          project.module_enabled?(:redmine_ux_toolkit)
        else
          project.enabled_modules.where(name: 'redmine_ux_toolkit').exists?
        end
      end
    end
  end
end
