require "prawn"
require "pry"

require_relative "spell"
require_relative "spells"
require_relative "primitive_adds"

puts "Book of Faces PDF generator"
class BookOfFaces
	FONT_SIZE_CHAPTER = 48
	FONT_SIZE_SECTION = 24
	FONT_SIZE_SUBSECTION = 18
	FONT_SIZE_BODY = 10

	attr_accessor :pdf, :number_name, :chapter_name, :chapter_title, :section_name, :spells

	def start_chapter(num, name)
		@number_name = num
		@chapter_name = name

		@chapter_title = "Chapter #{number_name}: #{chapter_name}"
		puts "Starting #{chapter_title}"

		pdf.start_new_page

		pdf.outline.update do
			puts "\tAdding #{@book_of_faces.chapter_title} on page #{page_number}"
			section(@book_of_faces.chapter_title, :destination => page_number)
		end

		puts "\tSetting font size"
		pdf.font_size FONT_SIZE_CHAPTER
		pdf.font "Abess"
		puts "\tPrinting chapter to document"
		pdf.text "Chapter #{number_name}: #{chapter_name}"

		puts "\tRestoring body text"
		pdf.font "Goudy"
		pdf.font_size FONT_SIZE_BODY
	end

	def start_section(section_name)
		puts "Beginning section #{section_name}: "

		@section_name = section_name

		pdf.outline.add_subsection_to(chapter_title) do
			@pdf.outline.section section_name, :destination => @pdf.page_number
		end

		puts "\tSetting font size"
		pdf.font_size FONT_SIZE_SECTION
		pdf.text section_name

		puts "\tRestoring body text"
		pdf.font_size FONT_SIZE_BODY
		pdf.font "Goudy"

	end

	def roll_results(dramatic_failure:, failure:, success:, exceptional_success:)
		pdf.font_size FONT_SIZE_BODY
		pdf.font "Goudy"

		pdf.text "Roll Results", :style => :bold

		pdf.formatted_text [
			{ :text => "Dramatic Failure: ", :styles => [:bold] },
			{ :text => dramatic_failure }
		]

		pdf.formatted_text [
			{ :text => "Failure: ", :styles => [:bold] },
			{ :text => failure }
		]

		pdf.formatted_text [
			{ :text => "Success: ", :styles => [:bold] },
			{ :text => success }
		]

		pdf.formatted_text [
			{ :text => "Exceptional Success: ", :styles => [:bold] },
			{ :text => exceptional_success }
		]
	end

	def generate
		puts "Parsing spells..."
		@spells = Spell.spells

		puts "Creating PDF object..."
		@pdf = Prawn::Document.new(:margin => 0)
		pdf.default_leading 5

		pdf.font_families.update(
			"Goudy"  => {
				:normal => "goudy.ttf",
				:bold   => "goudy-bold.ttf",
				:italic => "goudy-italic.ttf"
			},
			"Abess"  => {
				:normal => "abess.ttf"
			},
			"Lilith" => {
				:normal => "lilith.ttf"
			}
		)

		pdf.outline.instance_variable_set(:@book_of_faces, self)

		puts "Importing cover..."
		pdf.image("cover.png", :width => 612, :height => 792)
		
		puts "Printing inner cover..."
		pdf.start_new_page(:margin => 75)

		pdf.font "Abess"
		pdf.font_size 72
		pdf.text "The Book\nOf Faces", :align => :center
		puts "\tBook Of Faces big text..."

		pdf.font "Goudy"
		pdf.font_size 36
		pdf.text "\nA grimoire of spells, Gifts, Disciplines, Contracts, and other anomalies, with anecdotes and exposition from their in-game authors\n\n", :align => :center
		puts "\tDescription..."

		pdf.font_size 24
		pdf.text "Written by members of the /cofd/ Discord, edited by NEETzsche", :align => :center
		puts "\tMain credit..."
		
		pdf.outline.define do
		end

		start_chapter "One", "Introduction"
		
		start_section "Judging It By Its Cover"
		
		pdf.text "This tome is an Artifact-grimoire with certain special properties. It easily weighs about thirty or so pounds from all of the paper within it. It is leather bound with a very visible face on its cover and no title listed; each time people look away and take another look at it, the face on it changes. Upon opening it, it appears anachronistic and mixed. Most of the pages are merely old paper, but some of them are papyrus scrolls that have been glued into it, while others are cuneiform tablets, and some are even touch screens. Upon inspection of this tome, it's clear it should be much bigger, and much heavier, than it actually is. A quill pen is attached to it by a string. Even though a quill pen normally would only be used with ink-on-paper, it appears to also work as a tool to etch into the cuneiform tablets, as a stylus for the touch screens, and whatever else someone manages to find in this tome. Its contents change upon future viewings, making it feel like an endless sea."
		
		start_section "Exploring Its Contents"

		pdf.text "If one were to open it up and start reading, the content would make no sense. It simply cannot be understood. No amount of knowledge on High Speech, First Tongue, or whatever other language of supernatural origin seems to decipher it. However, he who possesses this book may inscribe their meaningful experiences into it. Mages can Scribe Grimoire (Mage: the Awakening, p. 166) into it, except not only is it Lasting, but they may also place their improvised spells in it as though they were Rotes. They may also write their anecdotes, philosophy and experiences within this book, as long as they are sufficiently meaningful. Other supernatural entities like vampires or werewolves may list in explicit detail their Gifts, Disciplines, Contracts or other traits."

		pdf.text "This action requires an hour of soul-searching, a successful roll on their equivalent Integrity rating, and a Beat. This Beat is consumed by the book; that Beat is gone forever."

		roll_results(
			:dramatic_failure    => "Same as failure, but in addition to this, the Book of Faces is offended at the player's arrogance. It emits a piercing roar that deals one point of Resistant aggravated damage to everyone in the room.",
			:failure             => "The person attempting it gave the Book Of Faces dross. It gave it crap, nonsense. Emo scribblings and cockamamie shitposts. They do not get their Beat back; turns out, their life experience wasn't that useful after all. They may try again without penalty, but must sacrifice another Beat.",
			:success             => "The content the character submitted is of actual value, and it is accepted, incorporated into its pages.\n\nIf the roll succeeds, for the remainder of the scene, the character may at his discretion learn any of the Rotes (as long as they have the Arcanum prerequisites, and even if they were Improvised spells when they were put into the Book of Faces) and Disciplines, may have Gifts conferred upon them, and may enter into Contracts found therein. In mechanics terms, the character reads the Book of Faces extensively for a number of hours and spends their Experiences however they desire, focusing on the things they find most interesting. If they want to use the Book of Faces again in the future, they must make a second roll.",
			:exceptional_success => "The willworker not only gives it valuable information, but demonstrates they are particularly capable of learning. They gain a full Experience point, but it can only be spent learning things from the Book of Faces"
		)

		start_chapter("Two", "Spells")

		spells.group_by(&:primary_arcanum).sort_by { |arcanum, spells| arcanum }.to_h.each do |arcanum, arcanum_spells|
			arcanum_name = arcanum.to_s.titleize

			puts "Compiling spells for #{arcanum_name}..."

			start_section("#{arcanum_name} Spells")

			arcanum_spells.group_by(&arcanum).sort_by { |rating, spells| rating }.to_h.each do |rating, rating_spells|
				rating_title = "#{rating.to_dots} #{Spell::RANKS[rating - 1]} of #{arcanum_name}"
				puts "\tCompiling #{rating_title} spells..."
				pdf.outline.add_subsection_to("#{arcanum_name} Spells") do
					@pdf.outline.section rating_title, :destination => @pdf.page_number
				end

				pdf.font_size FONT_SIZE_SECTION
				pdf.text rating_title

				rating_spells.sort_by { |spell| spell.name }.each do |spell|
					puts "\t\tCompiling #{spell.full_title} by #{spell.authors.join(', ')}"

					spell_ary = [{:text => "#{spell.name} (", :font => "Lilith", :color => "004E6D", :size => FONT_SIZE_SUBSECTION}]

					pdf.font_size FONT_SIZE_BODY
					previous = false
					spell.arcana.each do |arcanum|
						spell_ary << {:text => ", ", :font => "Lilith", :color => "004E6D", :size => FONT_SIZE_SUBSECTION} if previous
						spell_ary << {:text => arcanum.to_s.titleize, :font => "Lilith", :color => "004E6D", :size => FONT_SIZE_SUBSECTION}
						spell_ary << {:text => spell.send(arcanum).to_dots, :color => "004E6D", :size => FONT_SIZE_SUBSECTION, :character_spacing => -5}
						previous = true
					end

					spell_ary << {:text => " )", :font => "Lilith", :styles => [], :color => "004E6D", :size => FONT_SIZE_SUBSECTION, :character_spacing => -3}

					pdf.formatted_text spell_ary

					pdf.formatted_text [
						{:text => "Practice: ", :styles => [:bold], :font => "Goudy"},
						{:text => spell.practice, :font => "Goudy"},
					]

					pdf.formatted_text [
						{:text => "Primary Factor: ", :styles => [:bold], :font => "Goudy"},
						{:text => spell.primary_factor, :font => "Goudy"},
					]

					pdf.formatted_text [
						{:text => "Withstand: ", :styles => [:bold], :font => "Goudy"},
						{:text => spell.withstand, :font => "Goudy"},
					] if !spell.withstand.nil?

					pdf.formatted_text [
						{:text => "Suggested Rote Skills: ", :styles => [:bold], :font => "Goudy"},
						{:text => spell.suggested_rote_skills.join(', '), :font => "Goudy"},
					]

					pdf.formatted_text [
						{:text => spell.authors.length == 1 ? "Author: " : "Authors: ", :styles => [:bold], :font => "Goudy"},
						{:text => spell.authors.join(', '), :font => "Goudy"},
					]

					pdf.text spell.rules_text

					spell.reaches.each do |reach|
						pdf.formatted_text [
							{:text => "+#{reach[0]} Reach: ", :styles => [:bold], :font => "Goudy"},
							{:text => reach[1], :font => "Goudy"},
						]
					end

					spell.adds.each do |add|
						pdf.formatted_text [
							{:text => "#{add[:verb] ? add[:verb] : "Add"} #{add[:arcana].collect { |arcanum, rating| [arcanum, rating].to_arcanum_with_dots }.to_list(:separator => add[:separator])}: ", :styles => [:bold], :font => "Goudy"},
							{:text => add[:effect], :font => "Goudy"},
						]
					end
				end
			end

			pdf.start_new_page
		end

		pdf.render_file("book_of_faces.pdf")
	end
end

book_of_faces = BookOfFaces.new
book_of_faces.generate