#!/usr/bin/env ruby
# encoding: UTF-8

require 'pry'
# resolve bin path, ignoring symlinks
require "pathname"
bin_file = Pathname.new(__FILE__).realpath

# add self to libpath
$:.unshift File.expand_path("../../lib", bin_file)

# Fixes https://github.com/rubygems/rubygems/issues/1420
require "rubygems/specification"

class Gem::Specification
  def this; self; end
end

require 'bundler/setup'
require 'stepmod/utils/terms_extractor'

stepmod_dir = ARGV.first || Dir.pwd

general_concepts,
resource_concepts,
parsed_bibliography,
part_concepts,
part_resources,
part_modules = Stepmod::Utils::TermsExtractor.call(stepmod_dir)

def part_to_title(bibdata)
  case bibdata.part.to_i
  when 41
    "Part 41"
  when 42
    "Geometric and topological representation"
  when 43
    "Foundation representation"
  when 44
    "Product structure, concept and configuration"
  when 45
    "Part 45"
  when 46
    "Visual presentation"
  when 47
    "Shape tolerance"
  when 51
    "Mathematical representation"
  else
    bibdata.title_en
  end
end

def log message
  puts "[stepmod-utils] #{message}"
end

part_concepts.each do |(bibdata, current_part_concepts)|
  fn = "03x-stepmod-#{bibdata.part}.adoc"
  File.open(fn, 'w') { |file|
    file.puts("== #{part_to_title(bibdata)}\n\n")
    file.puts(current_part_concepts.map(&:to_mn_adoc).join("\n"))
  }
  log "INFO: written to: #{fn}"
end


part_resources.each do |(bibdata, current_part_resources)|
  fn = "04x-stepmod-entities-resources-#{bibdata.part}.adoc"
  File.open(fn, 'w') { |file|
    file.puts("== #{part_to_title(bibdata)}\n\n")
    file.puts(current_part_resources.map(&:to_mn_adoc).join("\n"))
  }
  log "INFO: written to: #{fn}"
end


part_modules.each do |(bibdata, part_modules_arm, part_modules_mim)|
  fn = "05x-stepmod-entities-modules-#{bibdata.part}.adoc"
  File.open(fn, 'w') { |file|
    file.puts("")
    unless part_modules_arm.empty?
      schema_name = part_modules_arm.first.first
      concepts = part_modules_arm.first.last

      # puts "SCHEMA NAME ARM: #{schema_name}"
      file.puts("== #{schema_name}\n\n")
      file.puts(concepts.map(&:to_mn_adoc).join("\n"))
    end

    file.puts("")

    unless part_modules_mim.empty?
      schema_name = part_modules_mim.first.first

      # puts "SCHEMA NAME MIM: #{schema_name}"
      concepts = part_modules_mim.first.last
      file.puts("== #{schema_name}\n\n")
      file.puts(concepts.map(&:to_mn_adoc).join("\n"))
    end

  }
  log "INFO: written to: #{fn}"
end

File.open('031-stepmod-general.adoc', 'w') { |file|
  file.puts(general_concepts.map(&:to_mn_adoc).join("\n"))
}
log "INFO: written to: 031-stepmod-general.adoc"

File.open('041-stepmod-entities-resources.adoc', 'w') { |file|
  file.puts(resource_concepts.map(&:to_mn_adoc).join("\n"))
}
log "INFO: written to: 041-stepmod-entities-resources.adoc"

File.open('991-generated-bibliography.adoc', 'w') { |file|
  file.puts(parsed_bibliography.map(&:to_mn_adoc).sort.uniq.join("\n"))
}
log "INFO: written to: 991-generated-bibliography.adoc"