package org.kantega.playground.fresh.srv;

import fj.Ord;
import fj.data.TreeMap;

import static fj.P.p;

public class Response {

  public final Status                  status;
  public final String                  body;
  public final TreeMap<String, String> headers;

  public Response(Status status, String body, TreeMap<String, String> headers) {
    this.status = status;
    this.body = body;
    this.headers = headers;
  }

  public Response withHeader(String key, String value) {
    return new Response(status, body, headers.set(key, value));
  }

  public static Response string(String body) {
    return new Response(Status.OK, body, TreeMap.treeMap(Ord.stringOrd, p("Content-Type", "text/plain")));
  }

  public static Response json(String body) {
    return new Response(Status.OK, body, TreeMap.treeMap(Ord.stringOrd, p("Content-Type", "application/json")));
  }

  public enum StatusFamily {success, redirect, clienterror, servererror}

  public enum Status {

    OK(StatusFamily.success, 200),
    NoContent(StatusFamily.success, 204),
    BadRequest(StatusFamily.clienterror, 201),
    Forbidden(StatusFamily.clienterror, 401),
    NotFound(StatusFamily.clienterror, 404),
    ServerError(StatusFamily.servererror, 500);

    //todo etc...

    public final StatusFamily family;
    public final int          code;

    Status( StatusFamily family,int code) {
      this.code = code;
      this.family = family;
    }

  }
}
