class MoveDistributionManagement
  @@reactorHandler = ReactorHandler.new
  @@modulePomPaths = nil
  def moveDistributionManagementFromFirstModule (project_directory_path,externalPomUrl)
    distMgmtFlag = true
    @@modulePomPaths =  @@reactorHandler.fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false)
    if (!externalPomUrl.nil?)
      begin
        Nokogiri::HTML(open(externalPomUrl)) #This is to validate the url
      rescue
        raise "Parent POM URL is not valid !!!"
      end
      pomDoc = Nokogiri::XML(open("#{externalPomUrl}"))
      if !pomDoc.at("project/distributionManagement").nil?
        distMgmtFlag = false
        removeDistributionManagementFromModules()
      else        
        firstModulePomDoc = Nokogiri::XML(open("#{@@modulePomPaths[0]}"))
        distMgmtTag = firstModulePomDoc.at("project/distributionManagement")
        writeDistributionManagementInReactorPom(project_directory_path,distMgmtTag)
        removeDistributionManagementFromModules()
      end
    else     
      firstModulePomDoc = Nokogiri::XML(open("#{@@modulePomPaths[0]}"))
      distMgmtTag = firstModulePomDoc.at("project/distributionManagement")
      writeDistributionManagementInReactorPom(project_directory_path,distMgmtTag)
      removeDistributionManagementFromModules()
    end
    pom_document = @@reactorHandler.parse_xml_from_file("#{project_directory_path}/pom.xml")
    if distMgmtFlag and pom_document.at("project/distributionManagement").nil?
      comment = " TODO: There is no <distributionManagement> defined for this reactor project. Please add this section before taking into production."
      modulesNode = pom_document.at("project/modules")
      commentNode = Nokogiri::XML::Comment.new(pom_document,comment)
      modulesNode.add_next_sibling(commentNode)
      dependencyHandler = DependencyHandler.new
      pomObj = dependencyHandler.add_node_element('project',modulesNode,pom_document)
      File.write("#{project_directory_path}/pom.xml",pomObj)
      mvnReactorization = MvnReactorization.new
      mvnReactorization.generateFormatedXml(project_directory_path)
    end
  end

  def writeDistributionManagementInReactorPom (project_directory_path,distMgmtTag)
    if !distMgmtTag.nil?
      reactorPomPath = "#{project_directory_path}/pom.xml"
      pom_document = @@reactorHandler.parse_xml_from_file(reactorPomPath)
      comment = " TODO: The reactor service had moved the <distributionManagement> section of first project to reactor\'s POM and removed from all the child modules. Please review this section before deploying these artifacts"
      commentNode = Nokogiri::XML::Comment.new(pom_document,comment)
      distMgmtTag.first_element_child.before(commentNode)
      dependencyHandler = DependencyHandler.new
      pomObj = dependencyHandler.add_node_element('project',distMgmtTag,pom_document)
      File.write(reactorPomPath,pomObj)
      mvnReactorization = MvnReactorization.new
      mvnReactorization.generateFormatedXml(project_directory_path)
      puts "Moved distributionManagement to reactor pom!!!"
    end
  end

  def removeDistributionManagementFromModules()
    @@modulePomPaths.each do |modulePom|
      pom_document = @@reactorHandler.parse_xml_from_file(modulePom)
      distMgmtNode = pom_document.at('project/distributionManagement')
      distMgmtNode.remove if !distMgmtNode.nil?
      File.write(modulePom,pom_document)
    end
    puts "distributionManagement removed from child modules!!!"
  end
end