package jetbrains.mps.ide.httpsupport.manager.plugin;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.channel.Channel;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import java.net.URISyntaxException;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import org.jetbrains.io.Responses;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import com.intellij.util.ExceptionUtil;
import jetbrains.mps.internal.collections.runtime.Sequence;
import com.intellij.util.io.NettyKt;
import com.intellij.openapi.util.text.StringUtil;
import java.net.URI;

@GeneratedClass(node = "r:05ff02e5-9836-4ae9-a454-eab43fa58c8f(jetbrains.mps.ide.httpsupport.manager.plugin)/7720980209309576946", model = "r:05ff02e5-9836-4ae9-a454-eab43fa58c8f(jetbrains.mps.ide.httpsupport.manager.plugin)")
public class HttpRequest {

  private final io.netty.handler.codec.http.HttpRequest request;
  private final QueryStringDecoder decoder;
  private final Channel channel;
  private final List<String> segments;
  private final String referrerHost;

  public HttpRequest(@NotNull io.netty.handler.codec.http.HttpRequest request, @NotNull QueryStringDecoder decoder, @NotNull Channel channel) throws URISyntaxException {
    this.request = request;
    this.decoder = decoder;
    this.channel = channel;
    this.segments = getSegmentsFor(getPath());
    this.referrerHost = getReferrerHost(request);
  }

  public List<String> getParameterValue(String key) {
    return decoder.parameters().get(key);
  }

  public String getPath() {
    return decoder.path();
  }

  public List<String> getSegments() {
    return segments;
  }

  public String getReferrerHost() {
    return referrerHost;
  }

  public String getMethod() {
    return request.method().name();
  }

  public void sendResponse(HttpResponseStatus status, String contentType, ByteBuf buffer) {
    // as long as MPS.IDEA exposes io.netty, have to reference FullHttpResponse here through MPS.IDEA
    // to satisfy Responses.send call
    FullHttpResponse response = Responses.response(contentType, buffer);
    response.setStatus(status);
    Responses.send(response, channel, request);
  }

  public void sendText(HttpResponseStatus status, String message) {
    this.sendResponse(status, "text/plain", Unpooled.copiedBuffer(message, CharsetUtil.UTF_8));
  }

  public void sendErrorResponse(HttpResponseStatus status, String message, Throwable error) {
    this.sendText(status, message + "\n\n" + ExceptionUtil.getThrowableText(error));
  }

  public static List<String> getSegmentsFor(String path) {
    if (path.startsWith("/")) {
      path = path.substring(1);
    }
    return Sequence.fromIterable(Sequence.fromArray(path.split("/"))).toList();
  }

  private static String getReferrerHost(io.netty.handler.codec.http.HttpRequest request) throws URISyntaxException {
    String refferer = NettyKt.getOrigin(request);
    if (refferer == null) {
      refferer = NettyKt.getReferrer(request);
    }

    return StringUtil.nullize((refferer == null ? null : new URI(refferer).getHost()));
  }
}
