#!/usr/bin/env ruby
# Take a file as first argument, which is to be the file biblio.csv, containing
# all books read. Then formats this as a table. It sorts the books in
# the order [Ongoing, Completed, Yet to Read], and replaces several values with
# ones more appropriate for viewing pleasure.
# For the final column (index: 11, zero-indexed) we replace the contents with a
# link. So rd/the-progress-of-a-crime becomes
# For column 3 (Part), this is rounded as special logic.
# <a href="https://gabbott.dev/rd/the-progress-of-a-crime>Link</a>

require_relative './biblio-require'



if ARGV[0] == nil then 
  puts "<!-- No argument passed! ma-fmt-tbl Exitting early. -->\n"
  exit
end

# def do_header(h: str): str
# Returns the rows of the table corresponding to the header.
def do_header(h)
  s = "\t<tr class=\"header\">\n"
  values = h.split($delim)

  values.each_with_index do |value, idx|
    next if idx == 0 or idx == 7 or idx == 8 or idx == 9
    s << ("\t\t<th>" + value + "</th>\n")
  end

  s << "\t</tr>\n"
  return s
end

# Make the string look sexy, ooh la la.
def sexify(val)
  # Round integers if they end .00.
  if val.end_with? ".00" then 
    integral = val.to_i
    unless integral == 0
      return integral.to_s
    end
  end

  # Transform strings.
  if $status_transform.has_key? (val)
    return $status_transform[val]
  end
  if $form_transform.has_key? (val)
    return $form_transform[val]
  end
  if $ownership_transform.has_key? (val)
    return $ownership_transform[val]
  end
  if $general_transform.has_key? (val)
    return $general_transform[val]
  end
  return val
end


# The tables, which are set as targets during the look
table=""
table_ongoing=""
table_cmp="" # Just contains completed entries.
table_paused=""
table_drp=""
table_rest=""
table_coll=""

# And the counts, for adding in counts if I want
ctable_ongoing=0
ctable_cmp=0
ctable_paused=0
ctable_drp=0
ctable_rest=0
ctable_coll=0

# And the header, don't forget!
header=""

File.foreach(ARGV[0]).with_index do |line, line_num| 
  next if line == "\n" or line == "\t" or line == "" or line == " " or line == $delim

  # Handle the first line - the header.
  if line_num == 0 then 
    header << do_header(line)
  else
    # We need to figure out whether it is ongoing, completed, or yet to read.
    # To do this, we will check if the corresponding string is in the line.
    if line.include? "completed;" then 
      target = table_cmp 
      ctable_cmp += 1
    elsif line.include? "ongoing;" then 
      target = table_ongoing
      ctable_ongoing += 1
    elsif line.include? "paused;" then 
      target = table_paused
      ctable_paused += 1
    elsif line.include? "dropped;" then 
      target = table_drp
      ctable_drp += 1
    elsif line.include? "collection;" then
      target = table_coll
      ctable_coll += 1
    else
      target = table_rest
      ctable_rest += 1
    end

    target << "\t<tr>\n"
    line.split($delim).each_with_index do |entry, idx|
      # We want to skip: Status, Ownership, Bought, Cost as they just take up
      # space. Here we are skipping by the relevant index.
      next if idx == 0 or idx == 7 or idx == 8 or idx == 9

      entry = entry.delete "\n"
      if idx == 11 and entry != "N/A" then 
        # Review column: transform it to contain a link.
        target << "\t\t<td class=\"biblio-tr\"><a href=\"" \
               << ENV["WEBSITE_URL"] \
               << "/" \
               << entry \
               << "\">Link</a></td>\n" 
      else
        target << "\t\t<td class=\"biblio-tr\">" \
               << sexify(entry) \
               << "</td>\n"
      end
    end
    target << "\t</tr>\n"
  end
end

# Instead of one table, split into <h2>Currently Reading</h2><table></table>, 
# <h2>Completed</h2><table></table> and <h2>Not read or
# reference</h2><table></table>.
table << "<h2>Currently Reading</h2>\n"
table << "<p>There are " << ctable_ongoing.to_s << " entries in this table.</p>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_ongoing
table << "</table>\n\n"

table << "<h2>Completed</h2>\n"
table << "<p>There are " << ctable_cmp.to_s << " entries in this table.</p>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_cmp
table << "</table>\n\n"

table << "<h2>Paused</h2>\n"
table << "<p>There are " << ctable_paused.to_s << " entries in this table.</p>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_paused
table << "</table>\n\n"

table << "<h2>Collections of Works</h2>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_coll
table << "</table>\n\n"

table << "<h2>Dropped</h2>\n"
table << "<p>There are " << ctable_drp.to_s << " entries in this table.</p>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_drp
table << "</table>\n\n"

table << "<h2>Not read or reference</h2>\n"
table << "<p>There are " << ctable_rest.to_s << " entries in this table.</p>\n"
table << "<table class=\"biblio\">\n"
table << header
table << table_rest
table << "</table>\n\n"

# Echo the file. 
puts table