module Pod
  class Installer

    # Controller class responsible of creating and configuring the static
    # library target in Pods project. It also creates the support file needed
    # by the target.
    #
    class TargetInstaller

      # @param  [Project] project @see project
      # @param  [Library] library @see library
      #
      def initialize(sandbox, library)
        @sandbox = sandbox
        @library = library
      end

      # Creates the target in the Pods project and the relative support files.
      #
      # @return [void]
      #
      def install!
        add_target
        add_pod_references
        create_suport_files_group

        create_xcconfig_file
        create_target_header
        create_prefix_header
        create_bridge_support_file
        create_copy_resources_script
        create_acknowledgements
        create_dummy_source
      end

      #-----------------------------------------------------------------------#

      public

      # @!group API
      #
      # This is the tentative API for the podfile and the specification hooks.

      # @return [Sandbox] sandbox the sandbox where the support files should
      #         be generated.
      #
      attr_reader :sandbox

      # @return [Library] The library whose target needs to be generated.
      #
      attr_reader :library

      # @return [PBXNativeTarget] the target generated by the installation
      #         process.
      #
      # @note   Generated by the {#add_target} step.
      #
      attr_reader :target

      # @return [Project] the Pods project of the sandbox.
      #
      def project
        sandbox.project
      end

      # @return [Array<LocalPod>] the local pods of this target.
      #
      def pods
        library.local_pods
      end

      # @return [TargetDefinition] the target definition of the library.
      #
      def target_definition
        library.target_definition
      end

      #--------------------------------------#

      # @!group Hooks compatiblity

      # @todo This has to be removed, but this means the specs have to be
      #       updated if they need a reference to the prefix header.
      #
      def prefix_header_filename
        UI.warn "The usage of the TargetInstaller#prefix_header_filename is deprecated."
        library.prefix_header_path.relative_path_from(sandbox.root)
      end

      #-----------------------------------------------------------------------#

      private

      # @!group Installation steps

      # Adds the target for the library to the Pods project with the
      # appropriate build configurations.
      #
      # @note   The `PODS_HEADERS_SEARCH_PATHS` overrides the xcconfig.
      #
      # @todo   Add integration test for build configurations and don't add the
      #         build configurations to the project if they are not needed.
      #
      # @return [void]
      #
      def add_target
        name = library.label
        platform = library.platform.name
        deployment_target = library.platform.deployment_target.to_s
        @target = project.new_target(:static_library, name, platform, deployment_target)

        settings = {}
        if library.platform.requires_legacy_ios_archs?
          settings['ARCHS'] = "armv6 armv7"
        end
        if target_definition.inhibit_all_warnings?
          settings['GCC_WARN_INHIBIT_ALL_WARNINGS'] = 'YES'
        end

        @target.build_settings('Debug').merge!(settings)
        @target.build_settings('Release').merge!(settings)

        library.user_build_configurations.each do |name, type|
          unless @target.build_configurations.map(&:name).include?(name)
            build_config = project.new(Xcodeproj::Project::XCBuildConfiguration)
            build_config.name = name
            settings = @target.build_settings(type.to_s.capitalize)
            build_config.build_settings = settings
            target.build_configurations << build_config
            project.build_configurations << build_config
          end
        end

      library.target = @target
      end

      # Adds the build files of the pods to the target and adds a refence to
      # the frameworks of the Pods.
      #
      # @note   The Frameworks are used only for presentation purporses as the
      #         xcconfig is the authoritative source about their information.
      #
      # @return [void]
      #
      def add_pod_references
        pods.each do |pod|
          pod.add_build_files_to_target(target)

          pod.frameworks.each do |framework|
            framework_ref = project.add_system_framework(framework, target)
          end
        end
      end

      # Creates the group that holds the references to the support files
      # generated by this installer.
      #
      # @return [void]
      #
      def create_suport_files_group
        name = target_definition.label
        @support_files_group = project.support_files_group.new_group(name)
      end

      #--------------------------------------#

      # Generates the contents of the xcconfig file and saves it to disk.
      #
      # @note   The `ALWAYS_SEARCH_USER_PATHS` flag is enabled to support
      #         libraries like `EmbedReader`.
      #
      # @return [void]
      #
      def create_xcconfig_file
        path = library.xcconfig_path
        UI.message "- Generating xcconfig file at #{UI.path(path)}" do
          gen = Generator::XCConfig.new(sandbox, pods, library.relative_pods_root)
          gen.set_arc_compatibility_flag = target_definition.podfile.set_arc_compatibility_flag?
          gen.save_as(path)
          library.xcconfig = gen.xcconfig
          xcconfig_file_ref = add_file_to_support_group(path)

          target.build_configurations.each do |c|
            c.base_configuration_reference = xcconfig_file_ref
            Generator::XCConfig.pods_project_settings.each do |key, value|
              c.build_settings[key] = value
            end
          end
        end
      end

      # Generates a header which allows to inspect at compile time the installed
      # pods and the installed specifications of a pod.
      #
      def create_target_header
        path = library.target_header_path
        UI.message "- Creating target header at #{UI.path(path)}" do
          generator = Generator::TargetHeader.new(library.specs)
          generator.save_as(path)
          add_file_to_support_group(path)
        end
      end

      # Creates a prefix header file which imports `UIKit` or `Cocoa` according
      # to the platform of the target. This file also include any prefix header
      # content reported by the specification of the pods.
      #
      # @return [void]
      #
      def create_prefix_header
        path = library.prefix_header_path
        UI.message "- Generating prefix header at #{UI.path(path)}" do
          generator = Generator::PrefixHeader.new(target_definition.platform, pods)
          generator.imports << library.target_header_path.basename
          generator.save_as(path)
          add_file_to_support_group(path)

          target.build_configurations.each do |c|
            relative_path = path.relative_path_from(sandbox.root)
            c.build_settings['GCC_PREFIX_HEADER'] = relative_path.to_s
          end
        end
      end

      # Generates the bridge support metadata if requested by the {Podfile}.
      #
      # @note   The bridge support metadata is added to the resources of the
      #         library because it is needed for environments interpreted at
      #         runtime.
      #
      # @return [void]
      #
      def create_bridge_support_file
        if target_definition.podfile.generate_bridge_support?
          path = library.bridge_support_path
          UI.message "- Generating BridgeSupport metadata at #{UI.path(path)}" do
            relative_headers = pods.map { |pod| pod.header_files.map {|head| sandbox.relativize(head)} }.flatten
            headers = relative_headers.map { |header| sandbox.root + header }
            generator = Generator::BridgeSupport.new(headers)
            generator.save_as(path)
            add_file_to_support_group(path)
            @bridge_support_file = path.relative_path_from(sandbox.root)
          end
        end
      end

      # Creates a script that copies the resources to the bundle of the client
      # target.
      #
      # @note   The bridge support file needs to be created before the prefix
      #         header, otherwise it will not be added to the resources script.
      #
      # @return [void]
      #
      def create_copy_resources_script
        path = library.copy_resources_script_path
        UI.message "- Generating copy resources script at #{UI.path(path)}" do
          resources = pods.map { |p| p.resource_files.map {|res| sandbox.relativize(res)} }.flatten
          resources << bridge_support_file if bridge_support_file
          generator = Generator::CopyResourcesScript.new(resources)
          generator.save_as(path)
          add_file_to_support_group(path)
        end
      end

      # Generates the acknowledgement files (markdown and plist) for the target.
      #
      # @return [void]
      #
      def create_acknowledgements
        basepath = library.acknowledgements_basepath
        Generator::Acknowledgements.generators.each do |generator_class|
          path = generator_class.path_from_basepath(basepath)
          UI.message "- Generating acknowledgements at #{UI.path(path)}" do
            generator = generator_class.new(target_definition, pods)
            generator.save_as(path)
            add_file_to_support_group(path)
          end
        end
      end

      # Generates a dummy source file for each target so libraries that contain
      # only categories build.
      #
      # @return [void]
      #
      def create_dummy_source
        path = library.dummy_source_path
        UI.message "- Generating acknowledgements at #{UI.path(path)}" do
          generator = Generator::DummySource.new(library.label)
          generator.save_as(path)
          file_reference = add_file_to_support_group(path)
          target.source_build_phase.add_file_reference(file_reference)
        end
      end

      #-----------------------------------------------------------------------#

      private

      # @!group Private helpers.

      # @return [PBXGroup] the group where the file references to the support
      #         files should be stored.
      #
      attr_reader :support_files_group

      # @return [Pathname] the path of the bridge support file relative to the
      #         sandbox.
      #
      # @return [Nil] if no bridge support file was generated.
      #
      attr_reader :bridge_support_file

      # Adds a reference to the given file in the support group of this target.
      #
      # @param  [Pathname] path
      #         The path of the file to which the reference should be added.
      #
      # @return [PBXFileReference] the file reference of the added file.
      #
      def add_file_to_support_group(path)
        relative_path = path.relative_path_from(sandbox.root)
        support_files_group.new_file(relative_path)
      end
    end
  end
end

