/*
 * Copyright 2000-2012 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.clouds.vmware;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import jetbrains.buildServer.clouds.*;
import jetbrains.buildServer.clouds.vmware.settings.VMWareSettings;
import jetbrains.buildServer.clouds.vmware.vmrun.VMRunFactory;
import jetbrains.buildServer.serverSide.AgentDescription;
import jetbrains.buildServer.serverSide.InvalidProperty;
import jetbrains.buildServer.serverSide.PropertiesProcessor;
import jetbrains.buildServer.util.NamedDeamonThreadFactory;
import org.jetbrains.annotations.NotNull;

/**
 * @author Eugene Petrenko
 *         Created: 07.12.2009 19:12:37
 */
public class VMWareCloudFactory implements CloudClientFactory {
  private final VMRunFactory myRunFactory;

  public VMWareCloudFactory(@NotNull final CloudRegistrar reg, final VMRunFactory runFactory) {
    myRunFactory = runFactory;
    reg.registerCloudFactory(this);
  }

  @NotNull
  public CloudClientEx createNewClient(@NotNull final CloudState state, @NotNull final CloudClientParameters params) {
    return new VMWareCloudClientEx(new VMWareSettings(params), myRunFactory, new ExecutorServiceFactory() {
      @NotNull
      public ScheduledExecutorService createExecutorService(@NotNull final String duty) {
        return Executors.newSingleThreadScheduledExecutor(new NamedDeamonThreadFactory("vmware-" + duty));
      }
    });
  }

  @NotNull
  public String getCloudCode() {
    return "vmware";
  }

  @NotNull
  public String getDisplayName() {
    return "VMWare";
  }

  public String getEditProfileUrl() {
    return null;
  }

  @NotNull
  public Map<String, String> getInitialParameterValues() {
    return Collections.emptyMap();
  }

  @NotNull
  public PropertiesProcessor getPropertiesProcessor() {
    return new PropertiesProcessor() {
      public Collection<InvalidProperty> process(final Map<String, String> properties) {
        return Collections.emptyList();
      }
    };
  }

  public boolean canBeAgentOfType(@NotNull final AgentDescription description) {
    final Map<String, String> ps = description.getDefinedParameters();
    return ps.containsKey(VMWareConstants.WM_IMAGE_ID) && ps.containsKey(VMWareConstants.WM_INSTANCE_ID);
  }
}
