# frozen_string_literal: true

module RedmineUxToolkit
  module Hooks
    def self.register!
      if Redmine::Hook.respond_to?(:hook_listeners)
        return if Redmine::Hook.hook_listeners(:view_my_account_preferences).any? do |listener|
          listener.is_a?(MyAccountHook)
        end
      else
        return if defined?(@registered) && @registered
      end

      Redmine::Hook.add_listener(MyAccountHook)
      @registered = true
    end

    class MyAccountHook < Redmine::Hook::ViewListener
      render_on :view_my_account_preferences,
                partial: 'redmine_ux_toolkit/hooks/landing_page_preferences'

      render_on :view_layouts_base_html_head,
                partial: 'redmine_ux_toolkit/hooks/head'

      render_on :view_my_account_contextual,
                partial: 'redmine_ux_toolkit/hooks/favorite_links'

      render_on :view_layouts_base_sidebar,
                partial: 'redmine_ux_toolkit/hooks/admin_sidebar_link'

      render_on :view_account_login_top,
                partial: 'redmine_ux_toolkit/hooks/login_notice'

      render_on :view_issues_new_top,
                partial: 'redmine_ux_toolkit/hooks/new_issue_context_notices'

      render_on :view_issues_form_details_top,
                partial: 'redmine_ux_toolkit/hooks/new_issue_notice'

      render_on :view_issues_form_details_top,
                partial: 'redmine_ux_toolkit/hooks/ticket_template_insert'

      render_on :view_issues_form_details_bottom,
                partial: 'redmine_ux_toolkit/hooks/issue_quality_check'

      render_on :view_issues_edit_notes_bottom,
                partial: 'redmine_ux_toolkit/hooks/text_snippet_insert'

      render_on :view_projects_show_left,
                partial: 'redmine_ux_toolkit/hooks/favorite_project'

      render_on :view_issues_index_bottom,
                partial: 'redmine_ux_toolkit/hooks/favorite_query'

      render_on :view_issues_show_details_bottom,
                {partial: 'redmine_ux_toolkit/hooks/assign_to_me'},
                {partial: 'redmine_ux_toolkit/hooks/issue_hint_box'},
                {partial: 'redmine_ux_toolkit/hooks/checklist'}

      render_on :view_issues_sidebar_issues_bottom,
                partial: 'redmine_ux_toolkit/hooks/sla_light'
    end
  end
end
