# Returns a file upload input tag for a given block, along with label and instructions.
#
# @param [Symbol] method The name of the model this form upload is associated with
# @param [Hash] options
# @option options [String] :label (method) If no label is specified, the human readable name for method will be used.
# @option options [String] :hint (blank) Helpful tips for the person entering the field, appears blank if nothing is specified.
class FilePickerInput < SimpleForm::Inputs::Base

  def input(wrapper_options)
    # New blocks will not have their attachments created yet.
    object.ensure_attachment_exists if object.respond_to?(:ensure_attachment_exists)

    # Need to explictly use correct id (i.e. image_block_file) rather than the autogenerated one (i.e. image_block_attachments_attributes_0_attachment_name)
    # Otherwise <label for=""/> won't be right.
    tag = ActionView::Helpers::Tags::Base.new(object_name, attribute_name, template, options)
    tag_id = tag.send(:tag_id)

    html = ""
    if render_section_picker?
      sections = sections_with_full_paths
      sections.each do |s|
        html << template.content_tag(:span, "", {:class => "section_id_map", style: 'display: hidden', :data => {:id => s.id, :path => s.prependable_path}})
      end
    end
    @builder.simple_fields_for :attachments do |a|
      if matching_attachment?(a)
        html << a.hidden_field("attachment_name", value: attribute_name.to_s)
        html << a.file_field(:data, input_html_options.merge('data-purpose' => "cms_file_field", id: tag_id))
        html << a.hint(options.delete(:hint)) if has_hint?
        if render_section_picker?
          html << a.input(:section_id, collection: sections, label_method: :full_path, include_blank: false, label: "Section", wrapper_html: {class: "inline-section-picker"}, input_html: {'data-purpose' => "section_selector"})
        end
        if render_path_input?
          klass = object.new_record? ? "suggest_file_path" : "keep_existing_path"
          html << a.input(:data_file_path, label: "Path", wrapper_html: {class: "inline-path"}, input_html: {class: klass})
        end
      end
      html << a.input(:id, as: :hidden, wrapper: false)
    end
    html.html_safe

  end

  protected

  # @override Find errors on attachments rather than the provide attribute name.
  def errors_on_attribute
    object.errors[:attachment]
  end

  # Because #attachments holds ALL attachments by all names, need to verify we only render one form control for each one.
  def matching_attachment?(a)
    a.object.attachment_name == attribute_name.to_s
  end

  def render_path_input?
    object.respond_to?(:set_attachment_path)
  end

  def render_section_picker?
    object.respond_to?(:set_attachment_section)
  end


  def sections_with_full_paths
    root = Cms::Section.root.first
    root.full_path = root.name
    sections = []
    sections << root
    sections += root.master_section_list
    sections.each { |s| s.full_path = "/" + s.full_path unless s == root }
    sections
  end

end