/*
 * Copyright 2000-2016 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.server.toolRegistry;

import jetbrains.buildServer.nuget.server.toolRegistry.impl.InstalledTool;
import jetbrains.buildServer.nuget.server.toolRegistry.impl.ToolsRegistry;
import jetbrains.buildServer.tools.ToolProvider;
import jetbrains.buildServer.tools.ToolType;
import jetbrains.buildServer.tools.ToolTypeExtension;
import jetbrains.buildServer.tools.ToolVersion;
import jetbrains.buildServer.util.CollectionsUtil;
import jetbrains.buildServer.util.Converter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
 * Created by Evgeniy.Koshkin on 15-Jan-16.
 */
public class NuGetToolProvider implements ToolProvider {

  private static final ToolTypeExtension NUGET_TOOL_TYPE = new ToolTypeExtension() {
    @NotNull
    public String getType() {
      return ToolConstants.NUGET_TOOL_TYPE_ID;
    }

    @NotNull
    @Override
    public String getDisplayName() {
      return "NuGet.exe";
    }

    @Nullable
    @Override
    public String getDescription() {
      return "Installed NuGet versions are automatically distributed to all build agents and can be used in NuGet-related runners.";
    }
  };
  @NotNull
  private final ToolsRegistry myToolsRegistry;

  public NuGetToolProvider(@NotNull ToolsRegistry toolsRegistry) {
    myToolsRegistry = toolsRegistry;
  }

  @NotNull
  public ToolType getType() {
    return NUGET_TOOL_TYPE;
  }

  @NotNull
  public Collection<ToolVersion> getInstalledToolVersions() {
    return CollectionsUtil.convertCollection(myToolsRegistry.getTools(), new Converter<ToolVersion, InstalledTool>() {
      public ToolVersion createFrom(@NotNull final InstalledTool source) {
        return new ToolVersion(NUGET_TOOL_TYPE, source.getVersion());
      }
    });
  }
}

@@ -653,16 +653,71 @@
 istry;%0A%0A
+import jetbrains.buildServer.nuget.common.ToolIdUtils;%0A
 import j
@@ -843,29 +843,35 @@
 ry.impl.
-ToolsRegistry
+NuGetToolsInstaller
 ;%0Aimport
@@ -897,71 +897,52 @@
 ver.
-tools.ToolProvider;%0Aimport jetbrains.buildServer.tools.ToolType
+nuget.server.toolRegistry.impl.ToolsRegistry
 ;%0Aim
@@ -974,33 +974,17 @@
 r.tools.
-ToolTypeExtension
+*
 ;%0Aimport
@@ -1010,25 +1010,28 @@
 ver.
-tools.ToolVersion
+util.CollectionsUtil
 ;%0Aim
@@ -1058,35 +1058,30 @@
 Server.util.
-Collections
+String
 Util;%0Aimport
@@ -1085,44 +1085,43 @@
 ort 
-jetbrains.buildServer.util.Converter
+org.apache.commons.io.FilenameUtils
 ;%0Aim
@@ -1204,16 +1204,37 @@
 lable;%0A%0A
+import java.io.File;%0A
 import j
@@ -1853,16 +1853,17 @@
  %7D%0A  %7D;%0A
+%0A
   @NotNu
@@ -1864,18 +1864,16 @@
 @NotNull
-%0A 
  private
@@ -1909,16 +1909,78 @@
 egistry;
+%0A  @NotNull private final NuGetToolsInstaller myToolInstaller;
 %0A%0A  publ
@@ -2036,16 +2036,60 @@
 Registry
+, @NotNull NuGetToolsInstaller toolInstaller
 ) %7B%0A    
@@ -2121,16 +2121,53 @@
 gistry;%0A
+    myToolInstaller = toolInstaller;%0A
   %7D%0A%0A  @
@@ -2387,133 +2387,625 @@
 (), 
-new Converter%3CToolVersion, InstalledTool%3E() %7B%0A      public ToolVersion createFrom(@NotNull final InstalledTool source) %7B
+source -%3E new ToolVersion(NUGET_TOOL_TYPE, source.getVersion()));%0A  %7D%0A%0A  @Nullable%0A  @Override%0A  public ToolVersion installTool(@Nullable String version, @NotNull File toolContent) throws jetbrains.buildServer.tools.ToolException %7B%0A    if(StringUtil.isEmptyOrSpaces(version))%7B%0A      version = getNuGetVersion(toolContent);%0A    %7D%0A    if(StringUtil.isEmptyOrSpaces(version)) return null;%0A%0A    try %7B%0A      myToolInstaller.installNuGet(FilenameUtils.removeExtension(toolContent.getName()), toolContent);%0A    %7D catch (ToolException e) %7B%0A      throw new jetbrains.buildServer.tools.ToolException(e.getMessage(), e);
 %0A    
+%7D%0A%0A
     
@@ -3048,43 +3048,510 @@
 PE, 
-source.getVersion());%0A      %7D%0A    %7D
+version);%0A  %7D%0A%0A  @Override%0A  public void removeTool(@Nullable String toolVersion) %7B%0A    //TODO: simplify, skip ID processing, version is enough%0A    for(InstalledTool installedTool : myToolsRegistry.getTools())%7B%0A      if(installedTool.getVersion().equalsIgnoreCase(toolVersion))%7B%0A        myToolsRegistry.removeTool(installedTool.getId());%0A      %7D%0A    %7D%0A  %7D%0A%0A  private String getNuGetVersion(File toolContent) %7B%0A    return ToolIdUtils.getVersionFromId(FilenameUtils.removeExtension(toolContent.getName())
 );%0A 

implement install / remove operation via new tools API