require 'watir-webdriver' # get watir gem from watir.com
                          # get chromedriver.exe from google and put it on Path.

class K5

  attr_accessor :name, :password, :sleep_value, :b, :page

  def initialize
    @name = 'trane'
    @password = 'boogit'
    @sleep_value = 3 # seconds to wait for page to load
    @page = 1
  end

  def login
    Watir::default_timeout = 700
    @b = Watir::Browser.new :chrome # assumes chromedriver.exe is on the path.
    # New login method, since the login fields were deleted
    @b.goto 'http://kuro5hin.org?uname='+ @name.gsub(' ','%20') + '&pass=' + @password
    #@b.goto 'kuro5hin.org'
    sleep @sleep_value # Give page time to load, and don't stress the site.
    #@b.text_field(:name => 'uname').when_present.set @name
    #@b.text_field(:name => 'pass').when_present.set @password
    #@b.button(:name => 'login').when_present.click
    sleep @sleep_value # Give new page time to load
    @b.title
  end

  def review_hidden_comments(n = 29)
    l = @b.link :text => 'Review Hidden Comments'
    puts "Can't find 'Review Hidden Comments' link." unless l.exists?
    l.click
    Watir::Wait.until {@b.body.exists?}
    sleep @sleep_value # Give page time to load.
    puts @b.title

    # move to search box
    Watir::Wait.until {@b.text_field(:name => 'string').exists?}
    searchbox = @b.text_field :name => 'string'
    searchbox.when_present.set ' '
    puts "Setting search box to [blank]..."
    # press tab 7 times to get to the first hidden comment, then click the link
    puts "Sending 7 tabs and enter..."
    @b.send_keys "\t\t\t\t\t\t\t\n"
    sleep @sleep_value
    puts "Waiting for select list..." 
    # select the ratings box 
    # the name has different numbers at the end
    @b.select_list(:name => /rating_/).wait_until_present
    # TODO: put this in the identical (1..n) loop that follows.
    s = @b.select_list :name => 'rating_0'
    (1..1000).each do |i|
      s = @b.select_list :name => 'rating_' + i.to_s
      if s.exists? 
        puts "rating_" + i.to_s + " found."
        break
      end
    end

    s.select 'Encourage (3)' 
    puts "comment 1 upvoted"

    puts "Going back..."
    @b.back
    puts "Back."

    # Now loop through the rest of the hidden comments
    (1..n).each do |j|

      Watir::Wait.until {@b.text_field(:name => 'string').exists?}
      searchbox = @b.text_field :name => 'string'
      searchbox.set ''
    
      tabstring = "\t\t\t\t\t\t\t" 
      (1..j).each do |k| 
        tabstring = tabstring + "\t\t\t\t"
      end
      puts "Setting tabstring..."
      puts "About to send enter..."
      @b.send_keys tabstring + "\n"
      puts "Enter sent"
      sleep @sleep_value
      
      puts "Waiting for select list..." 
      @b.select_list(:name => /rating_/).wait_until_present
      s = @b.select_list :name => 'rating_0'
      (1..1000).each do |i|
        s = @b.select_list :name => 'rating_' + i.to_s
        if s.exists? 
          puts "rating_" + i.to_s + " found."
          break
        end
      end

      s.select 'Encourage (3)'
      puts "Hidden comment ##{j+1} upvoted."

      puts "Going back..."
      @b.back      
      puts "Back."
    end

  end

  def grab_hidden_comments_text n=29
  #def grab_hidden_comments_text n=2 # for testing
    comments = Array.new
    l = @b.link :text => 'Review Hidden Comments'
    puts "Can't find 'Review Hidden Comments' link." unless l.exists?
    l.click
    Watir::Wait.until {@b.body.exists?}
    sleep @sleep_value # Give page time to load.
    puts @b.title

    # Go the page specified.
    puts "@page==#{@page}."
    (2..@page).each do |p|
      @b.button(:name => "next").when_present.click
    end

    (1..n).each do |i|
      # move to search box
      Watir::Wait.until {@b.text_field(:name => 'string').exists?}
      searchbox = @b.text_field :name => 'string'
      searchbox.when_present.set ' '


      # press tab 7 times to get to the first hidden comment, then click the link.
      # press tab 8 times if on page 2 or higher
      # press the tab 4 more times for each additional comment in the list
      tabstring = "\t\t\t\t\t\t\t" 
      (1..i).each do |k| 
        tabstring = tabstring + "\t\t\t\t"
      end
      if i == 1 then tabstring = "\t\t\t\t\t\t\t" end
      # pages of hidden comments 2 or higher have an extra button...
      if @page > 1 then tabstring = tabstring + "\t" end
      @b.send_keys tabstring + "\n"

      sleep @sleep_value
   
      # Grab the comment text.
      @b.div(:class=>'dynexpanded').wait_until_present
      comment = @b.div(:class => 'dynexpanded').text
      comment.sub!(/^\[new\] /m, '')
      comment.sub!(/\n\[.*\]$/m, '')
      #puts "raw comment #{i}: #{comment}"
      comment_links = @b.div(:class => 'dynexpanded').links
      comment_links.each { |l|
        if l.text.to_s.strip == '' then next end
        if l.href =~ /^javascript/ then next end
        #puts "l.text==#{l.text}."
        #if l.text =~ /^\#\d+$/ then next end
        #puts "l.text=#{l.text}."
        #comment.sub!(/#{l.text}((?!<\/a>).)*$/, "<a href=\"#{l.href}\">#{l.text}</a>)") # truncates the second occurrence of "#_" in the subject line 
        comment.sub!(/#{l.text}(?!">)/, "<a href=\"#{l.href}\">#{l.text}</a>") 
      }

      # Collect comments
      # TODO put delineator between comments.
      comments.push comment
      #puts "comment #{i}: #{comment}"

      @b.back
    end
    # Post to diary
    #puts comments.inspect
    post_to_diary comments
  end

  def post_to_diary comments=Array.new
    n = comments.size
    #puts "\n\n\n\n\n\n\n\n\n\nIn post_to_diary. n==#{n}."
    if n == 0 then return "No comments to post!" end
    @b.link(:text => "New Diary Entry").when_present.click 
    @b.text_field(:name => 'title').when_present.set "#{n} Hidden Comments Today!"
    @b.text_field(:name => 'tags').when_present.set "hidden comments"
    @b.textarea(:name => 'introtext').when_present.set comments[0] 
    if n == 1 then return "Diary posted with #{n} comment." end
    @b.textarea(:name => 'bodytext').when_present.set comments[1..-1] 
    @b.button(:name => 'preview').click
    sleep @sleep_value
    #@b.button(:name => 'save').when_present.click
    return "Diary with #{n} comments posted." 
  end

  
end

if $0 == __FILE__
  k5 = K5.new
  k5.login
  k5.review_hidden_comments
  k5.b.logout
end
