/**
 * Copyright (c) 2010, Sebastian Sdorra
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of SCM-Manager; nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * http://bitbucket.org/sdorra/scm-manager
 *
 */



package sonia.scm.repository;

//~--- non-JDK imports --------------------------------------------------------

import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import sonia.scm.Type;
import sonia.scm.io.FileSystem;
import sonia.scm.plugin.ext.Extension;
import sonia.scm.repository.spi.GitRepositoryServiceProvider;
import sonia.scm.store.StoreFactory;
import sonia.scm.util.AssertUtil;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sonia.scm.SCMContextProvider;
import sonia.scm.schedule.Scheduler;
import sonia.scm.schedule.Task;

/**
 *
 * @author Sebastian Sdorra
 */
@Singleton
@Extension
public class GitRepositoryHandler
  extends AbstractSimpleRepositoryHandler<GitConfig>
{

  /** Field description */
  public static final String DIRECTORY_REFS = "refs";

  /** Field description */
  public static final String RESOURCE_VERSION =
    "sonia/scm/version/scm-git-plugin";

  /** Field description */
  public static final String TYPE_DISPLAYNAME = "Git";

  /** Field description */
  public static final String TYPE_NAME = "git";


  public static final String DOT_GIT = ".git";

  private static final Logger logger = LoggerFactory.getLogger(GitRepositoryHandler.class);

  /** Field description */
  public static final Type TYPE = new RepositoryType(TYPE_NAME,
                                    TYPE_DISPLAYNAME,
                                    GitRepositoryServiceProvider.COMMANDS);

  private static final Object LOCK = new Object();
  
  private Task task;
  
  //~--- constructors ---------------------------------------------------------

  /**
   * Constructs ...
   *
   *
   * @param storeFactory
   * @param fileSystem
   * @param scheduler
   */
  @Inject
  public GitRepositoryHandler(StoreFactory storeFactory, FileSystem fileSystem, Scheduler scheduler)
  {
    super(storeFactory, fileSystem);
    this.scheduler = scheduler;
  }

  //~--- get methods ----------------------------------------------------------

  @Override
  public void init(SCMContextProvider context)
  {
    super.init(context);
    scheduleGc();
  }

  @Override
  public void setConfig(GitConfig config)
  {
    super.setConfig(config);
    scheduleGc();
  }
  
  private void scheduleGc()
  {
    synchronized (LOCK){
      if ( task != null ){
        logger.debug("cancel existing git gc task");
        task.cancel();
        task = null;
      }
      String exp = getConfig().getGcExpression();
      if (!Strings.isNullOrEmpty(exp))
      {
        logger.info("schedule git gc task with expression {}", exp);
        task = scheduler.schedule(exp, GitGcTask.class);
      }
    }
  }
  
  /**
   * Method description
   *
   *
   * @param repository
   *
   * @return
   */
  @Override
  public BlameViewer getBlameViewer(Repository repository)
  {
    GitBlameViewer blameViewer = null;

    AssertUtil.assertIsNotNull(repository);

    String type = repository.getType();

    AssertUtil.assertIsNotEmpty(type);

    if (TYPE_NAME.equals(type))
    {
      blameViewer = new GitBlameViewer(this, repository);
    }
    else
    {
      throw new IllegalArgumentException("git repository is required");
    }

    return blameViewer;
  }

  /**
   * Method description
   *
   *
   * @param repository
   *
   * @return
   */
  @Override
  public ChangesetViewer getChangesetViewer(Repository repository)
  {
    GitChangesetViewer changesetViewer = null;

    AssertUtil.assertIsNotNull(repository);

    String type = repository.getType();

    AssertUtil.assertIsNotEmpty(type);

    if (TYPE_NAME.equals(type))
    {
      changesetViewer = new GitChangesetViewer(this, repository);
    }
    else
    {
      throw new IllegalArgumentException("git repository is required");
    }

    return changesetViewer;
  }

  /**
   * Method description
   *
   *
   * @param repository
   *
   * @return
   */
  @Override
  public DiffViewer getDiffViewer(Repository repository)
  {
    GitDiffViewer diffViewer = null;

    AssertUtil.assertIsNotNull(repository);

    String type = repository.getType();

    AssertUtil.assertIsNotEmpty(type);

    if (TYPE_NAME.equals(type))
    {
      diffViewer = new GitDiffViewer(this, repository);
    }
    else
    {
      throw new IllegalArgumentException("git repository is required");
    }

    return diffViewer;
  }

  /**
   * Method description
   *
   *
   * @return
   */
  @Override
  public ImportHandler getImportHandler()
  {
    return new GitImportHandler(this);
  }

  /**
   * Method description
   *
   *
   * @param repository
   *
   * @return
   */
  @Override
  public RepositoryBrowser getRepositoryBrowser(Repository repository)
  {
    AssertUtil.assertIsNotNull(repository);

    return new GitRepositoryBrowser(this, repository);
  }

  /**
   * Method description
   *
   *
   * @return
   */
  @Override
  public Type getType()
  {
    return TYPE;
  }

  /**
   * Method description
   *
   *
   * @return
   */
  @Override
  public String getVersionInformation()
  {
    return getStringFromResource(RESOURCE_VERSION, DEFAULT_VERSION_INFORMATION);
  }

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param repository
   * @param directory
   *
   * @throws IOException
   * @throws RepositoryException
   */
  @Override
  protected void create(Repository repository, File directory)
    throws RepositoryException, IOException
  {
    try (org.eclipse.jgit.lib.Repository gitRepository = build(directory)) {
      gitRepository.create(true);
    }
  }
  
  private org.eclipse.jgit.lib.Repository build(File directory) throws IOException {
    return new FileRepositoryBuilder()
      .setGitDir(directory)
      .readEnvironment()
      .findGitDir()
      .build();
  }

  /**
   * Method description
   *
   *
   * @return
   */
  @Override
  protected GitConfig createInitialConfig()
  {
    return new GitConfig();
  }

  //~--- get methods ----------------------------------------------------------

  /**
   * Method description
   *
   *
   * @return
   */
  @Override
  protected Class<GitConfig> getConfigClass()
  {
    return GitConfig.class;
  }

  /**
   * Method description
   *
   *
   * @param directory
   *
   * @return
   */
  @Override
  protected boolean isRepository(File directory)
  {
    return new File(directory, DIRECTORY_REFS).exists();
  }
  
  private final Scheduler scheduler;
}
