package dev.peerat.framework;

public class Context{
	
	private RequestType type;
	private String path;
	private User user;
	private int responseCode;
	private HttpWriter writer;
	private String[] headers;
	
	public Context(RequestType type, String path, User user, HttpWriter writer, String... headers){
		this.type = type;
		this.path = path;
		this.user = user;
		this.writer = writer;
		this.headers = headers;
	}
	
	public RequestType getType(){
		return this.type;
	}
	
	public String getPath(){
		return this.path;
	}
	
	public boolean isLogged(){
		return user != null;
	}
	
	void setUser(User user){
		this.user = user;
	}
	
	public <U extends User> U getUser(){
		return (U) user;
	}
	
	public void response(int code, String... headers) throws Exception{
		if(headers != null && headers.length > 0){
			if(this.headers.length > 0){
				String[] copy = new String[this.headers.length+headers.length];
				System.arraycopy(this.headers, 0, copy, 0, this.headers.length);
				System.arraycopy(headers, 0, copy, this.headers.length, headers.length);
				this.headers = copy;
			}else{
				this.headers = headers;
			}
		}
		this.responseCode = code;
		this.writer.response(code, this.headers);
	}
	
	public int getResponseCode(){
		return this.responseCode;
	}

}
