package info.bukova.isspst;

import info.bukova.isspst.data.Requirement;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

public class RequirementUrlResolver implements EntityUrlResolver {
	
	private String defaultUrl;
	@Autowired
	private HttpServletRequest request;
	
	public RequirementUrlResolver() {
		
	}

	@Override
	public String entityUrl(Object entity) {
		if (!(entity instanceof Requirement)) {
			return defaultUrl + "/app";
		}
		
		if (defaultUrl == null) {
			this.defaultUrl = request.getRequestURL().toString();
			this.defaultUrl = defaultUrl.substring(0, defaultUrl.indexOf(request.getServletPath()));
		}
		
		Requirement req = (Requirement)entity;
		
		if (req.getKind() != null && req.getKind() == Constants.REQ_TYPE_MATERIAL) {
			return defaultUrl + Constants.URL_MAP.get(req.getClass()) + "material/?select=" + String.valueOf(req.getId());
		} else {
			return defaultUrl + Constants.URL_MAP.get(req.getClass()) + "services/?select=" + String.valueOf(req.getId());
		}
	}

}
