package ca.intelliware.ihtsdo.mlds.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import ca.intelliware.ihtsdo.mlds.registration.Application;
import ca.intelliware.ihtsdo.mlds.registration.ApplicationRepository;

@Controller
public class ApplicationController {
	@Resource
	ApplicationRepository applicationRepository;
	@Resource
	SessionService sessionService;

	@RequestMapping(value="api/applications")
	public @ResponseBody Iterable<Application> getApplications() {
		return applicationRepository.findAll();
	}
	
	@RequestMapping(value="api/applications/approve")
	public Object approveApplication(@RequestParam String email) {
		List<Application> applications = applicationRepository.findByUsername(email);
		if (applications.size() == 0) {
			return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
		}
		
		Application application = applications.get(0);
		
		application.setApproved(true);
		applicationRepository.save(application);
		
		return new ResponseEntity<>(HttpStatus.OK);
	}
	
	@RequestMapping(value="/api/applications/create",method=RequestMethod.POST)
	public Object createApplication(@RequestBody JsonNode request) {
		System.out.println("in call " + request);
		JsonNode organization = request.get("organization");
		JsonNode contact =  request.get("contact");
		
		List<Application> applications = applicationRepository.findByUsername(sessionService.getUsernameOrNull());
		Application application = new Application();
		
		if (applications.size() > 0) {
			application = applications.get(0);
		}
		
		application.setUsername(sessionService.getUsernameOrNull());
		application.setType(request.get("type").asText());
		application.setApplicantType(request.get("applicantType").asText());
		
		application.setName(contact.get("name").asText());
		application.setPhoneNumber(contact.get("phone").asText());

		if (request.get("applicantType").asText() == "Organisation") {
			application.setName(organization.get("name").asText());
			application.setAddress(organization.get("address").asText());
			application.setCity(organization.get("city").asText());
			application.setCountry(organization.get("country").asText());
			application.setPhoneNumber(organization.get("phonenumber").asText());
			application.setExtension(organization.get("extension").asText());
			application.setPosition(organization.get("position").asText());
			application.setWebsite(organization.get("website").asText());
		}
			
		application.setSnoMedLicence(request.get("snoMedTC").asBoolean());
		
		application.setApproved(false);
		applicationRepository.save(application);
		
		return new ResponseEntity<>(HttpStatus.OK);
	}
}

@@ -50,67 +50,17 @@
 ava.
-io.IOException;%0Aimport java.util.List;%0Aimport java.util.Map
+util.List
 ;%0A%0Ai
@@ -547,270 +547,8 @@
 y;%0A%0A
-import com.fasterxml.jackson.core.JsonFactory;%0Aimport com.fasterxml.jackson.core.JsonParseException;%0Aimport com.fasterxml.jackson.databind.JsonMappingException;%0Aimport com.fasterxml.jackson.databind.JsonNode;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0A
 impo
@@ -674,16 +674,65 @@
 itory;%0A%0A
+import com.fasterxml.jackson.databind.JsonNode;%0A%0A
 @Control
@@ -2929,16 +2929,50 @@
 %09%09%7D%0A%09%09%09%0A
+%09%09// FIXME MB map unset to false?%0A
 %09%09applic

Make "accept license" checkbox required