<% ux_toolkit_admin_controllers = %w[
  admin
  settings
  ux_toolkit_checklist_templates
  ux_toolkit_ticket_templates
  ux_toolkit_text_snippets
  ux_toolkit_tracker_checklist_templates
  ux_toolkit_recurring_issue_rules
  ux_toolkit_team_workload
  ux_toolkit_project_health
  ux_toolkit_attachment_storage
  ux_toolkit_log_files
] %>
<% show_ux_toolkit_admin_link = User.current.admin? && ux_toolkit_admin_controllers.include?(controller.controller_path) %>

<% if show_ux_toolkit_admin_link %>
  <h3><%= l(:label_redmine_ux_toolkit) %></h3>
  <ul class="ux-toolkit-admin-sidebar-links">
    <li>
      <%= link_to l(:label_ux_toolkit_settings),
                  plugin_settings_path(:redmine_ux_toolkit),
                  class: 'icon icon-settings' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_checklist_templates),
                  ux_toolkit_checklist_templates_path,
                  class: 'icon icon-list' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_ticket_templates),
                  ux_toolkit_ticket_templates_path,
                  class: 'icon icon-list' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_text_snippets),
                  ux_toolkit_text_snippets_path,
                  class: 'icon icon-list' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_tracker_checklist_defaults),
                  ux_toolkit_tracker_checklist_templates_path,
                  class: 'icon icon-list' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_recurring_issue_rules),
                  ux_toolkit_recurring_issue_rules_path,
                  class: 'icon icon-issue' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_team_workload),
                  ux_toolkit_team_workload_path,
                  class: 'icon icon-user' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_project_health),
                  ux_toolkit_project_health_path,
                  class: 'icon icon-projects' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_attachment_storage),
                  ux_toolkit_attachment_storage_path,
                  class: 'icon icon-attachment' %>
    </li>
    <li>
      <%= link_to l(:label_ux_toolkit_log_files),
                  ux_toolkit_log_files_path,
                  class: 'icon icon-document' %>
    </li>
  </ul>
<% end %>

<% if User.current.logged? %>
  <% ux_toolkit_settings = Setting.plugin_redmine_ux_toolkit || {} %>
  <% show_recent_issues_sidebar = ux_toolkit_settings.fetch('show_recent_issues_in_sidebar', '1').to_s == '1' %>
  <% show_recent_projects_sidebar = ux_toolkit_settings.fetch('show_recent_projects_in_sidebar', '1').to_s == '1' %>

  <% if show_recent_issues_sidebar %>
    <% recent_issues = UxToolkitRecentIssue.visible_for(User.current).limit(UxToolkitRecentIssue.configured_limit) %>

    <div class="ux-toolkit-sidebar-recent-block ux-toolkit-sidebar-recent-issues"
         data-ux-toolkit-recent-block
         data-ux-toolkit-storage-key="redmine_ux_toolkit_recent_issues_sidebar_open">
      <h3>
        <button type="button"
                class="ux-toolkit-sidebar-toggle"
                data-ux-toolkit-recent-toggle
                aria-expanded="false"
                title="<%= l(:label_ux_toolkit_expand_recent_issues) %>">
          <span class="ux-toolkit-toggle-icon ux-toolkit-toggle-icon-open" aria-hidden="true">
            +
          </span>
          <span class="ux-toolkit-toggle-icon ux-toolkit-toggle-icon-close" aria-hidden="true" hidden>
            -
          </span>
          <span><%= l(:label_ux_toolkit_recently_visited_issues) %></span>
        </button>
      </h3>

      <div data-ux-toolkit-recent-content hidden>
        <% if recent_issues.any? %>
          <ul>
            <% recent_issues.each do |recent_issue| %>
              <% issue = recent_issue.issue %>
              <% next unless issue %>
              <li>
                <%= link_to_issue(issue) %>
                <br>
                <small><%= h(issue.project) %></small>
              </li>
            <% end %>
          </ul>
          <p><%= link_to l(:label_ux_toolkit_recent_issues), ux_toolkit_my_recent_issues_path %></p>
        <% else %>
          <p class="nodata"><%= l(:text_ux_toolkit_no_recent_issues) %></p>
        <% end %>
      </div>
    </div>
  <% end %>

  <% if show_recent_projects_sidebar %>
    <% recent_projects = UxToolkitRecentProject.visible_for(User.current).limit(UxToolkitRecentProject.configured_limit) %>

    <div class="ux-toolkit-sidebar-recent-block ux-toolkit-sidebar-recent-projects"
         data-ux-toolkit-recent-block
         data-ux-toolkit-storage-key="redmine_ux_toolkit_recent_projects_sidebar_open">
      <h3>
        <button type="button"
                class="ux-toolkit-sidebar-toggle"
                data-ux-toolkit-recent-toggle
                aria-expanded="false"
                title="<%= l(:label_ux_toolkit_expand_recent_projects) %>">
          <span class="ux-toolkit-toggle-icon ux-toolkit-toggle-icon-open" aria-hidden="true">
            +
          </span>
          <span class="ux-toolkit-toggle-icon ux-toolkit-toggle-icon-close" aria-hidden="true" hidden>
            -
          </span>
          <span><%= l(:label_ux_toolkit_recently_visited_projects) %></span>
        </button>
      </h3>

      <div data-ux-toolkit-recent-content hidden>
        <% if recent_projects.any? %>
          <ul>
            <% recent_projects.each do |recent_project| %>
              <% project = recent_project.project %>
              <% next unless project %>
              <li>
                <%= link_to_project(project) %>
                <br>
                <small><%= h(project.identifier) %></small>
              </li>
            <% end %>
          </ul>
          <p><%= link_to l(:label_ux_toolkit_recent_projects), ux_toolkit_my_recent_projects_path %></p>
        <% else %>
          <p class="nodata"><%= l(:text_ux_toolkit_no_recent_projects) %></p>
        <% end %>
      </div>
    </div>
  <% end %>

  <% if show_recent_issues_sidebar || show_recent_projects_sidebar %>
    <style>
    .ux-toolkit-sidebar-recent-block .ux-toolkit-sidebar-toggle {
      align-items: center;
      background: transparent;
      border: 0;
      color: inherit;
      cursor: pointer;
      display: inline-flex;
      font: inherit;
      gap: 4px;
      padding: 0;
      text-align: left;
    }

    .ux-toolkit-sidebar-recent-block .ux-toolkit-toggle-icon {
      align-items: center;
      border: 1px solid #999;
      border-radius: 50%;
      box-sizing: border-box;
      display: inline-flex;
      height: 14px;
      justify-content: center;
      line-height: 12px;
      width: 14px;
    }

    .ux-toolkit-sidebar-recent-block .ux-toolkit-toggle-icon[hidden] {
      display: none;
    }
    </style>

    <%= javascript_tag do %>
      document.addEventListener('DOMContentLoaded', function() {
        var wrappers = document.querySelectorAll('[data-ux-toolkit-recent-block]');

        wrappers.forEach(function(wrapper) {
          var toggle = wrapper.querySelector('[data-ux-toolkit-recent-toggle]');
          var content = wrapper.querySelector('[data-ux-toolkit-recent-content]');
          var openIcon = wrapper.querySelector('.ux-toolkit-toggle-icon-open');
          var closeIcon = wrapper.querySelector('.ux-toolkit-toggle-icon-close');
          var storageKey = wrapper.getAttribute('data-ux-toolkit-storage-key');
          var expandTitle = wrapper.classList.contains('ux-toolkit-sidebar-recent-projects') ?
            '<%= j l(:label_ux_toolkit_expand_recent_projects) %>' :
            '<%= j l(:label_ux_toolkit_expand_recent_issues) %>';
          var collapseTitle = wrapper.classList.contains('ux-toolkit-sidebar-recent-projects') ?
            '<%= j l(:label_ux_toolkit_collapse_recent_projects) %>' :
            '<%= j l(:label_ux_toolkit_collapse_recent_issues) %>';

          function setOpen(isOpen) {
            content.hidden = !isOpen;
            openIcon.hidden = isOpen;
            closeIcon.hidden = !isOpen;
            toggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
            toggle.setAttribute('title', isOpen ? collapseTitle : expandTitle);
            window.localStorage.setItem(storageKey, isOpen ? '1' : '0');
          }

          setOpen(window.localStorage.getItem(storageKey) === '1');
          toggle.addEventListener('click', function() {
            setOpen(toggle.getAttribute('aria-expanded') !== 'true');
          });
        });
      });
    <% end %>
  <% end %>
<% end %>
