/*
 * 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.tools.installer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

import jetbrains.buildServer.core.runtime.osgx.IToolDescriptor;
import jetbrains.buildServer.core.runtime.osgx.Version;

import org.apache.log4j.Logger;

public class ToolsInstallerController {

//  "<%=ToolsInstallerController.getInstance().setToolId('${selectedToolId}')%>""
  
  private static final Logger LOG = Logger.getLogger(ToolsInstallerController.class);
  private static ToolsInstallerController ourInstance;
  private String myCurrentToolId;

  public ToolsInstallerController(/*, final ValueProviderRegistry valueProviderRegistry*/) {
    ourInstance = this;
  }

  public static ToolsInstallerController getInstance() {
    return ourInstance;
  }

  public List<ToolDescriptor> getToolDescriptions() throws IOException {
    TreeSet<ToolDescriptor> descriptions = new TreeSet<ToolDescriptor>();
    for (IToolDescriptor tool : ToolsInstallerFeature.getInstance().tools()) {
      descriptions.add(new ToolDescriptor(tool.getId(), tool.getDescription()));
    }
    return new ArrayList<ToolDescriptor>(descriptions);
  }

  public void setToolId(String id) {
    myCurrentToolId = id;
    LOG.debug(String.format("Set Tool Id to '%s'", id));
  }

  public List<Version> getToolVersions(/*String id*/) throws IOException {
    final TreeSet<Version> versions = new TreeSet<Version>(new Comparator<Version>(){
      public int compare(Version o1, Version o2) {
        return o2.compareTo(o1);//reverse order
      }});
    for (IToolDescriptor tool : ToolsInstallerFeature.getInstance().tools()) {
      if (tool.getId().equals(myCurrentToolId)) {
        if (!IToolDescriptor.ALL.equals(tool.getVersion()))
          versions.add(new Version(tool.getVersion()));
      }
    }
    final ArrayList<Version> out = new ArrayList<Version>(versions);    
    LOG.debug(String.format("Asked versions for '%s': %s", myCurrentToolId, out));    
    return out;
  }

  public static class ToolDescriptor implements Comparable<ToolDescriptor> {

    private String myId;
    private String myDescription;

    public ToolDescriptor() {
    }

    ToolDescriptor(String id, String description) {
      myId = id;
      myDescription = description;
    }

    public String getId() {
      return myId;
    }

    public void setId(String id) {
      this.myId = id;
    }

    public String getDescription() {
      return myDescription;
    }

    public void setDescription(String Description) {
      myDescription = Description;
    }

    @Override
    public int hashCode() {
      return getId().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
      if (obj instanceof ToolDescriptor) {
        return getId().equals(((ToolDescriptor) obj).getId());
      }
      return super.equals(obj);
    }

    @Override
    public String toString() {
      return getDescription();
    }

    public int compareTo(ToolDescriptor o) {
      return getDescription().compareTo(o.getDescription());
    }

  }

}
