require 'escape'
require 'active_support/core_ext/array/conversions'

module Pod
  module Generator

    class Documentation
      extend Executable

      executable :appledoc
      attr_reader :pod, :specification, :target_path, :options

      def initialize(pod)
        @pod = pod
        @specification = pod.top_specification
        @target_path = pod.sandbox.root + 'Documentation' + pod.name
        @options = @specification.documentation || {}
      end

      def name
        @specification.name + ' ' + @specification.version.to_s
      end

      def company
        if @specification.authors
          @specification.authors.keys.sort.to_sentence
        else
          'no-company'
        end
      end

      def copyright
        company
      end

      def description
        @specification.description || 'Generated by CocoaPods.'
      end

      def docs_id
        'org.cocoapods'
      end

      def files
        @pod.documentation_headers.map{ |f| f.relative_path_from(@pod.root).to_s }
      end

      def index_file
        @pod.readme_file.relative_path_from(@pod.root).to_s if @pod.readme_file
      end

      def spec_appledoc_options
        @options[:appledoc] || []
      end

      # @note The appledoc tool terminates with an exits status of 1 if a
      #       warning was logged
      #
      def appledoc_options
        options = [
          '--project-name', name,
          '--docset-desc', description,
          '--project-company', company,
          '--docset-copyright', copyright,
          '--company-id', docs_id,
          '--ignore', '.m',
          '--keep-undocumented-objects',
          '--keep-undocumented-members',
          '--keep-intermediate-files',
          '--exit-threshold', '2'
        ]
        options += ['--index-desc', index_file] if index_file
        options += spec_appledoc_options
      end

      def already_installed?
        index = spec_appledoc_options.index('--company-id')
        company_id = index ? spec_appledoc_options[index + 1] : docs_id
        Pathname.new(File.expand_path("~/Library/Developer/Shared/Documentation/DocSets/#{company_id}.#{name.gsub(/ /,'-')}.docset")).exist?
      end

      # @todo passing the files explicitly clutters output and chokes on very
      #       long list (AWSiOSSDK).  It is possible to just pass the dir of
      #       the pod, however this would include other files like demo
      #       projects.
      #
      def generate(install = false)
        if `which appledoc`.strip.empty?
          UI.warn "[!] Skipping documentation generation because appledoc can't be found.",
            actions = [], verbose_only = true
          return
        end

        options = appledoc_options
        options += ['--output', @target_path.to_s]
        options += install ? ['--create-docset'] : ['--no-create-docset']
        options += files

        @target_path.mkpath
        @pod.chdir do
          appledoc Escape.shell_command(options)
        end

        if $?.exitstatus != 0
          UI.warn "[!] Appledoc encountered an error (exitstatus: #{$?.exitstatus}), an update might be available to solve the issue."
        end

      end
    end
  end
end
