/*
 * Copyright 2000-2013 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.vmrun.local;

import com.intellij.openapi.diagnostic.Logger;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import jetbrains.buildServer.clouds.CloudInstanceUserData;
import jetbrains.buildServer.clouds.QuotaException;
import jetbrains.buildServer.clouds.vmware.settings.VMImageInfo;
import jetbrains.buildServer.clouds.vmware.vmrun.VMRunParameters;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author Eugene Petrenko
 *         Created: 07.12.2009 20:16:02
 */
public class LocalVMRun {
  private static final Logger LOG = Logger.getInstance(LocalVMRun.class.getName());

  private final ScheduledExecutorService myExecutor;
  private final WMMachineInjection myInjection;
  private final AtomicBoolean myMachineStarting = new AtomicBoolean(false);
  private final LocalImagesStore myLocalImagesStore;
  private final VMRunSettings mySettings;

  public LocalVMRun(@NotNull final VMRunSettings settings,
                    @NotNull final ScheduledExecutorService executor,
                    @NotNull final WMMachineInjection injection,
                    @NotNull final LocalImagesStore localImagesStore) {
    mySettings = settings;
    myExecutor = executor;
    myInjection = injection;
    myLocalImagesStore = localImagesStore;
  }

  @NotNull
  public synchronized Collection<VMImageInfo> getRunningImages() {
    final ListVMCommand cmd = new ListVMCommand(mySettings, myLocalImagesStore.getLocalImages());
    cmd.startVMRun();
    return cmd.getInfos();
  }

  public synchronized void startMachine(@NotNull final VMRunParameters params, @NotNull final CloudInstanceUserData data) {
    if (myMachineStarting.compareAndSet(false, true)) {
      try {
        lockedStartMachine(params, data);
      } finally {
        myMachineStarting.set(false);
      }
    } else {
      throw new QuotaException("Only one machine could be started in a time.");
    }
  }

  private synchronized void lockedStartMachine(@NotNull final VMRunParameters params, @NotNull final CloudInstanceUserData data) {
    final VMImageInfo info = params.getImageInfo();
    LOG.info("Starting machine: " + info);

    checkCanStartNewMachine(info);

    final File file = getTempFile();
    try {
      LOG.info("Starting image: " + params);

      doStartMachine(params, data, info, file);

      LOG.info("Started image: " + params);
    } catch (Throwable t) {
      LOG.warn("Failed to start image: " + info + ". " + t.getMessage(), t);
      throw new VMRunException("Failed to start image: " + info + ". " + t.getMessage(), t);
    } finally {
      FileUtil.delete(file);
    }
  }

  private boolean checkInstanceContained(@NotNull Collection<VMImageInfo> images, @NotNull final VMImageInfo toFind) {
    final File lookupPath = new File(toFind.getImagePath());
    for (VMImageInfo image : images) {
      if (new File(image.getImagePath()).equals(lookupPath)) {
        return true;
      }
    }
    return false;
  }

  private void checkCanStartNewMachine(final VMImageInfo info) {
    final Collection<VMImageInfo> runningInfo = getRunningImages();
    int running = runningInfo.size();
    int allowed = mySettings.getAllowedNumberOfMachinesToRun();

    LOG.info("Currently running machines: " + running + " of " + allowed);

    if (running >= allowed + 1 || checkInstanceContained(runningInfo, info)) {
      throw new QuotaException("Failed to start more than " + allowed + " machines.");
    }
  }

  private void doStartMachine(final VMRunParameters params, final CloudInstanceUserData data, final VMImageInfo info, final File file) {
    new StartCommand(mySettings, info).startVMRun();

    LOG.info("Machine " + info.getImagePath() + " started.");
    myExecutor.submit(new Runnable() {
      public void run() {
        try {
          myInjection.injectSettings(info, params, data, file);
        } catch (VMRunException e) {
          LOG.warn("Failed to inject buildAgent.properties file into started machine " + info.getImagePath() + ". Terminating...");
          doTerminateBrokenInstance(info);
        }
      }
    });
  }

  private void doTerminateBrokenInstance(final VMImageInfo info) {
    try {
      terminateMachine(info);
    } catch (Exception e) {
      //
    }
  }

  private File getTempFile() {
    final File file;
    try {
      file = FileUtil.createTempFile("push-agent", ".properties");
    } catch (IOException e) {
      throw new VMRunException("Failed to create temp file to start image.");
    }
    return file;
  }


  public synchronized void terminateMachine(@NotNull final VMImageInfo image) {
    new StopCommand(mySettings, image).startVMRun();
  }

  public int getAllowedMachinesToRun() {
    return mySettings.getAllowedNumberOfMachinesToRun();
  }
}
