/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.nuget.agent.install.impl;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.nuget.agent.install.PackageInstallParametersFactory;
import jetbrains.buildServer.nuget.agent.install.PackagesInstallParameters;
import jetbrains.buildServer.nuget.agent.install.PackagesUpdateParameters;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static jetbrains.buildServer.nuget.common.PackagesInstallerConstants.*;

/**
 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
 * Date: 07.07.11 18:09
 */
public class PackageInstallParametersFactoryImpl implements PackageInstallParametersFactory {
  @NotNull
  public PackagesInstallParameters loadParameters(@NotNull final BuildRunnerContext context) throws RunBuildException {
    return new PackagesInstallParameters() {

      private File resolvePath(@Nullable final String runnerParameter, @NotNull String name) throws RunBuildException {
        String path = context.getRunnerParameters().get(runnerParameter);
        if (StringUtil.isEmptyOrSpaces(path))
          throw new RunBuildException("Runner parameter '" + runnerParameter + "' was not found");

        File file = FileUtil.resolvePath(context.getBuild().getCheckoutDirectory(), path);
        if (!file.exists()) {
          throw new RunBuildException("Failed to find " + name + " at " + file);
        }

        return file;
      }


      @NotNull
      public File getSolutionFile() throws RunBuildException {
        return resolvePath(SLN_PATH, "Visual Studio .sln file");
      }

      @NotNull
      public File getNuGetExeFile() throws RunBuildException {
        return resolvePath(NUGET_PATH, "nuget.exe");
      }

      @NotNull
      public Collection<String> getNuGetPackageSources() {
        String sources = context.getRunnerParameters().get(NUGET_SOURCES);
        if (sources == null) return Collections.emptyList();

        List<String> list = new ArrayList<String>();
        for (String _source : sources.split("[\\r\\n]+")) {
          final String source = _source.trim();
          if (!source.isEmpty()) {
            list.add(source);
          }
        }

        return Collections.unmodifiableList(list);
      }

      public boolean getExcludeVersion() {
        return !StringUtil.isEmptyOrSpaces(context.getRunnerParameters().get(NUGET_EXCLUDE_VERSION));
      }

      @Nullable
      public PackagesUpdateParameters getUpdatePackages() {
        return null;
      }
    };
  }
}

@@ -1925,33 +1925,19 @@
  path = 
-context.getRunner
+get
 Paramete
@@ -1937,23 +1937,16 @@
 arameter
-s().get
 (runnerP
@@ -2762,33 +2762,19 @@
 urces = 
-context.getRunner
+get
 Paramete
@@ -2770,31 +2770,24 @@
 getParameter
-s().get
 (NUGET_SOURC
@@ -3239,69 +3239,18 @@
 urn 
-!StringUtil.isEmptyOrSpaces(context.getRunnerParameters().get
+getBoolean
 (NUG
@@ -3268,17 +3268,16 @@
 VERSION)
-)
 ;%0D%0A     
@@ -3372,19 +3372,528 @@
     
-return null
+if (!getBoolean(NUGET_UPDATE_PACKAGES))%0D%0A          return null;%0D%0A%0D%0A        final boolean safe = getBoolean(NUGET_UPDATE_PACKAGES_SAFE);%0D%0A        return new PackagesUpdateParameters() %7B%0D%0A          public boolean getUseSafeUpdate() %7B%0D%0A            return safe;%0D%0A          %7D%0D%0A        %7D;%0D%0A      %7D%0D%0A%0D%0A      private boolean getBoolean(String key) %7B%0D%0A        return !StringUtil.isEmptyOrSpaces(getParameter(key));%0D%0A      %7D%0D%0A%0D%0A      private String getParameter(String key) %7B%0D%0A        return context.getRunnerParameters().get(key)
 ;%0D%0A 

implement PackageInstallParametersFactory