/*
 * Copyright (c) 2000-2010 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package jetbrains.buildServer.serverSide;

import jetbrains.buildServer.web.openapi.PageExtension;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.PlaceId;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.jetbrains.annotations.NotNull;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Maxim Podkolzine (maxim.podkolzine@jetbrains.com)
 */
public class MyExtension implements PageExtension {
  private final String myPath;
  private final PluginDescriptor myDescriptor;

  public MyExtension(@NotNull PagePlaces pagePlaces,
                     @NotNull PluginDescriptor descriptor) {
    myDescriptor = descriptor;
    pagePlaces.getPlaceById(PlaceId.ALL_PAGES_FOOTER).addExtension(this);
    myPath = descriptor.getPluginResourcesPath("chatty-batty.html");
  }

  @NotNull
  public String getPluginName() {
    return myDescriptor.getPluginName();
  }

  @NotNull
  public String getIncludeUrl() {
    return myDescriptor.getPluginResourcesPath("chat.jsp");
  }

  @NotNull
  public List<String> getCssPaths() {
    return Arrays.asList(myDescriptor.getPluginResourcesPath("chat.css"));
  }

  @NotNull
  public List<String> getJsPaths() {
    return Arrays.asList(myDescriptor.getPluginResourcesPath("chat.js"));
  }

  public boolean isAvailable(@NotNull HttpServletRequest request) {
/*
    String url = WebUtil.getPathWithoutContext(request);
    return StringUtil.isEmpty(url) || url.equals("/") || url.equals("overview.html") || url.equals("/overview.html");
*/
    return true;
  }

  public void fillModel(@NotNull Map<String, Object> model, @NotNull HttpServletRequest request) {
    model.put("chatPath", myPath);
    model.put("adminMode", request.getParameter("iamadmin") != null);
  }
}
