module Admin::NodesHelper
  def node_structure_for_select(parent, node)
    result = []

    result << [ parent.title, parent.id ] if parent.root?

    parent.children.ordered.each do |child|
      result << [ "#{"-" * (child.depth)} #{child.title}".html_safe, child.id, { disabled: (child == node or child.ancestor_ids.include?(node.id)) } ]
      result = result + node_structure_for_select(child, node)
    end

    result
  end

  def spacer_node(node)
    return if node.root?

    tag.div class: "spacer" do
      node.depth.times do
        concat tag.div nil
      end
    end
  end


  def toggle_node(node)
    return tag.div nil, class: "child" if node.document?
    link_to url_for(controller: "nodes", action: "children", id: node.id),
            class: "child parent",
            data: {
              turbo_stream: true,
              controller: "nodes",
              action: "click->nodes#toggle_children"
            } do
      concat tag.span("expand_more")
      concat tag.span("keyboard_arrow_right")
    end
  end


  def tree_title(node)
    # result = [link_to(ENV["PROJECT_NAME"],
    #                   url_for(url_base_options.merge(id: nil)),
    #                   class: "list-title-link#{" has-popup-menu" if node.blank?}",
    #                   data: {
    #                     icon: "museum",
    #                     action: node.blank? ? "click->popup#toggle" : "click->nodes#set_current",
    #                     turbo_stream: node.blank? ? nil : true,
    #                   })]
    result = []

    node&.path&.each do |n|
      result << tree_node_title_link(n, node)
    end

    result[-1] = "#{tag.div "#{result[-1]}#{node_popup_menu(node)}".html_safe, data: { controller: "popup" } }"

    result.join(tag.span(">")).html_safe
  end


  def node_popup_menu(node = nil)
    tag.div class: "popup-menu" do
      tag.ul do
        concat tag.li(link_to t("ui.edit"), edit_admin_node_path(node), data: {icon: "edit", turbo_action: "advance"}) if node
        Node.categories.each do |node_category|
            concat tag.li button_to(t(node_category, scope: "nodes.new_categories"),
                            url_for(controller: "nodes", action: "create"),
                            params: { node: { parent_id: node&.id } },
                            data: {
                              action: "click->popup#close_open",
                              icon: t(node_category, scope: "nodes.icons")
                            })
        end
        # concat tag.li button_to(t("ui.reindex"),
        #               url_for(controller: "nodes", action: "reindex", id: node.id),
        #               method: :patch,
        #               data: {
        #                 action: "click->popup#close_open",
        #                 icon: "refresh"
        #               }) if node
      end
    end
  end


  def node_link_title(node)
    # return node.title if !node.document? or node.attachments.blank? or !node.attachments.first.asset.representable?

    # asset = node.attachments.first.asset
    # node_icon = image_tag(rails_storage_proxy_path(asset.representation(resize_to_limit: [48,72], format: :jpg))) if asset.representable?
    node_icon = nil
    node_title = tag.span(node.title)

    (node_icon || "").concat(node_title).html_safe
  end


  def node_flags(node)
    return if node.copyright_with_inheritance.blank? and node.tags_with_inheritance.blank?
    tag.div class: "node__flags" do
      concat tag.span("copyright") if node.copyright_with_inheritance.present?
      concat tag.span("sell") if node.tags_with_inheritance.any?
    end
  end


  def drawer_node_link(node)
    link_to tag.span(node.title),
            url_for(controller: "nodes", action: node.document? ? "edit" : "tree", id: node.id),
            title: node.title,
            class: "node-title",
            id: dom_id(node, "drawer-link"),
            data: {
              nodes_id_param: node.id,
              turbo_stream: true,
              action: "click->nodes#set_current",
              icon: t("nodes.icons.#{node.category}")
            }
  end


  def tree_node_title_link(node, current_node = nil)
    current_node ||= node
    url_base_options = { controller: "nodes", action: "tree" }

    link_to(node.title,
            url_for(url_base_options.merge(id: node.id)),
            class: current_node == node ? "list-title-link has-popup-menu" : "list-title-link",
            id: dom_id(node, "list-title-link"),
            data: {
              action: current_node == node ? "click->popup#toggle" : "click->nodes#set_current",
              turbo_stream: current_node != node ? true : nil,
              nodes_id_param: node.id,
              icon: t("nodes.icons.#{node.category}")
            })
  end
end
