class Redwood::ThreadIndexMode
  register_keymap do |k|
    k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
    k.add_multi "Load all threads (! to confirm) :", '!' do |kk|
      kk.add :load_all_threads, "Load all threads (may list a _lot_ of threads)", '!'
    end
    k.add :cancel_search, "Cancel current search", :ctrl_g
    k.add :reload, "Refresh view", '@'
    k.add :toggle_archived, "Toggle archived status", 'a'
    k.add :toggle_starred, "Star or unstar all messages in thread", '*'
    k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
    k.add :edit_labels, "Edit or add labels for a thread", 'l'
    k.add :edit_message, "Edit message (drafts only)", 'e'
    k.add :toggle_spam, "Mark/unmark thread as spam", 'S'
    k.add :toggle_deleted, "Delete/undelete thread", 'd'
    k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
    k.add :flush_index, "Flush all changes now", '$'
    k.add :jump_to_next_new, "Jump to next new thread", :tab
    k.add :reply, "Reply to latest message in a thread", 'r'
    k.add :reply_all, "Reply to all participants of the latest message in a thread", 'G'
    k.add :forward, "Forward latest message in a thread", 'f'
    k.add :toggle_tagged, "Tag/untag selected thread", 't'
    k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
    k.add :tag_matching, "Tag matching threads", 'g'
    k.add :apply_to_tagged, "Apply next command to all tagged threads", '+', '='
    k.add :join_threads, "Force tagged threads to be joined into the same thread", '#'
    k.add :undo, "Undo the previous action", 'u'
    k.add :thread_as_not_spam, "Mark this thread as not spam", 'H'
  end

  def actually_thread_as_not_spam t
    if t.has_label? :unsure
      t.remove_label :unsure
      t.each { |m, *o|
        if m
          IO.popen("bogofilter -n", "w") {|f|
            f.puts m.raw_message
          }
        end
      }
      lambda do
        t.add_label :unsure
        t.each { |m, *o|
          if m
            IO.popen("bogofilter -N", "w") {|f|
              f.puts m.raw_message
            }
          end
        }
      end
    elsif t.has_label? :spam
      t.remove_label :spam
      t.each { |m, *o|
        if m
          IO.popen("bogofilter -Sn", "w") {|f|
            f.puts m.raw_message
          }
        end
      }
      lambda do
        t.add_label :unsure
        t.each { |m, *o|
          if m
            IO.popen("bogofilter -Ns", "w") {|f|
              f.puts m.raw_message
            }
          end
        }
      end
    else
      lambda do
      end
    end
  end

  def thread_as_not_spam
    t = cursor_thread or return
    undo = actually_thread_as_not_spam t
    UndoManager.register "marking thread as not spam", undo, lambda { Index.save_thread t }
    update_text_for_line curpos
    cursor_down
    Index.save_thread t
  end
end

class Redwood::InboxMode
  def update
    old_cursor_thread = cursor_thread
    @mutex.synchronize do
      ## let's see you do THIS in python
      @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }
      @size_widgets = @threads.map { |t| size_widget_for_thread t }
      @size_widget_width = @size_widgets.max_of { |w| w.display_length }
      @date_widgets = @threads.map { |t| date_widget_for_thread t }
      @date_widget_width = @date_widgets.max_of { |w| w.display_length }
    end
    set_cursor_pos @threads.index(old_cursor_thread)||curpos

    regen_text
  end
end

