#!/usr/bin/env ruby
require 'rubygems'
require 'ostruct'
require 'clamp'
require 'logging'
require 'kafo/string_helper'
require 'kafo/exceptions'
require 'logger'

$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'kafo'))
require 'configuration'

KafoConfigure = OpenStruct.new
def KafoConfigure.exit(code)
  Kernel.exit(1)
end

module Kafo
  class KafoExportParams < Clamp::Command
    TYPES = %w(md html asciidoc)

    option ['-c', '--config'], 'FILE', 'Config file for which should we generate params',
           :required => true

    option ['-f', '--format'], 'FORMAT',
           'Config file for which should we generate params', :default => 'md' do |format|
      format = format.downcase
      raise ArgumentError unless TYPES.include?(format)
      format
    end

    def execute
      c                         = Configuration.new(config, false)
      KafoConfigure.config      = c
      KafoConfigure.root_dir    = File.expand_path(c.app[:installer_dir])
      KafoConfigure.modules_dir = File.expand_path(c.app[:modules_dir])
      KafoConfigure.logger      = Logger.new(STDOUT)

      exporter = self.class.const_get(format.capitalize).new(c)
      exporter.print_out
    end

    class Html
      include StringHelper

      def initialize(config)
        @config = config
      end

      def print_out
        puts '<div id="installer-options">'
        puts '  <table class="table table-bordered table-condensed">'
        header
        puts '    <tbody>'

        @config.modules.sort.each do |mod|
          mod.params.sort.each do |param|
            puts '      <tr>'
            puts "        <td style='white-space:nowrap'>#{parametrize(param)}</td>"
            puts "        <td>#{param.doc.join(' ')}</td>"
            puts '      </tr>'
          end
        end

        puts '    </tbody>'
        puts '  </table>'
        puts '</div>'
      end

      private

      def header
        puts '    <thead>'
        puts '      <tr>'
        puts '        <th>Option</th>'
        puts '        <th>Description</th>'
        puts '      </tr>'
        puts '    </thead>'
      end
    end

    class Asciidoc
      include StringHelper

      def initialize(config)
        @config = config
      end

      def print_out
        @config.modules.sort.each do |mod|
          puts "Parameters for '#{mod.name}':\n\n"
          mod.params.sort.each do |param|
            puts "#{parametrize(param)}::"
            puts param.doc.join("\n")
            puts "\n"
          end
          puts "\n"
        end
      end
    end

    class Md
      include StringHelper

      def initialize(config)
        @config = config
        @max    = max_description_length
      end

      def print_out
        puts "| #{('Parameter name').ljust(40)} | #{'Description'.ljust(@max)} |"
        puts "| #{'-'*40} | #{'-' * @max} |"
        @config.modules.sort.each do |mod|
          mod.params.sort.each do |param|
            puts "| #{parametrize(param).ljust(40)} | #{param.doc.join(' ').ljust(@max)} |"
          end
        end
      end

      private

      def header
        @header ||= "| #{'-'*40} | #{'-' * @max} |"
      end

      def max_description_length
        doc_lengths = @config.modules.map { |mod| mod.params.map { |param| param.doc.join(' ').length } }.flatten
        doc_lengths << 52
        doc_lengths.max
      end
    end
  end
end

Kafo::KafoExportParams.run
