package com.projectning.service.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
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 com.projectning.service.exceptions.NotFoundException;
import com.projectning.service.exceptions.SessionExpiredException;
import com.projectning.service.manager.HelperManager;
import com.projectning.service.manager.RelationshipManager;
import com.projectning.service.manager.UserManager;
import com.projectning.util.ErrorMessage;

@Controller
public class RelationshipController {
	
	@Autowired private RelationshipManager relationshipManager;
	@Autowired private HelperManager helperManager;
	@Autowired private UserManager userManager;
	
	@RequestMapping("/send_friend_request")
    public ResponseEntity<Map<String, Object>> sendFriendRequest(@RequestBody Map<String, Object> request) {
		Map<String, Object> respond = new HashMap<String, Object>();
		try{
			String accessToken = (String) request.get("accessToken");
			Map<String, Object> result = helperManager.decodeJWT(accessToken);
			
			helperManager.checkSessionTimeOut((String)result.get("expire"));
			
			int id = userManager.getUserId((String)result.get("username"), accessToken);
			
			userManager.checkUserIdExistance((int)request.get("receiverId"));
			
			relationshipManager.sendFriendRequest(id, (int)request.get("receiverId"));
			
			respond.put("error", "");
		}catch(NullPointerException e){
			respond.put("error", ErrorMessage.INCORRECT_PARAM.getMsg());
		}catch(IllegalStateException e){
			respond.put("error", e.getMessage());
		}catch(NotFoundException e){
			respond.put("error", e.getMessage());
		}catch(SessionExpiredException e){
			respond.put("error", ErrorMessage.SESSION_EXPIRED.getMsg());
		}
		return new ResponseEntity<Map<String, Object>>(respond, HttpStatus.OK);
		
	}
	
	@RequestMapping("/get_friend_id_list")
	public ResponseEntity<Map<String, Object>> getFriendList(@RequestBody Map<String, Object> request){
		Map<String, Object> respond = new HashMap<String, Object>();
		try{
			String accessToken = (String) request.get("accessToken");
			Map<String, Object> result = helperManager.decodeJWT(accessToken);
			
			helperManager.checkSessionTimeOut((String)result.get("expire"));
			
			int id = userManager.getUserId((String)result.get("username"), accessToken);
			
			List<Integer> idList = relationshipManager.getFriendIDList(id);
			
			respond.put("friendList", idList);
			respond.put("error", "");
		}catch(NullPointerException e){
			respond.put("error", ErrorMessage.INCORRECT_PARAM.getMsg());
		}catch(IllegalStateException e){
			respond.put("error", e.getMessage());
		}catch(NotFoundException e){
			respond.put("error", e.getMessage());
		}catch(SessionExpiredException e){
			respond.put("error", ErrorMessage.SESSION_EXPIRED.getMsg());
		}
		return new ResponseEntity<Map<String, Object>>(respond, HttpStatus.OK);
	}
	
	@RequestMapping("/get_sorted_friend_list")
	public ResponseEntity<Map<String, Object>> getSortedFriendList(@RequestBody Map<String, Object> request){
		Map<String, Object> respond = new HashMap<String, Object>();
		try{
			/*
			String accessToken = (String) request.get("accessToken");
			Map<String, Object> result = helperManager.decodeJWT(accessToken);
			
			helperManager.checkSessionTimeOut((String)result.get("expire"));
			
			int id = userManager.getUserId((String)result.get("username"), accessToken);
			
			List<Integer> idList = relationshipManager.getFriendIDList(id);
			*/
			
			Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
			
			Map<String, Object> allen = new HashMap<String, Object>();
			allen.put("name", "allen");
			allen.put("id", 1);
			
			Map<String, Object> abe = new HashMap<String, Object>();
			abe.put("name", "abe");
			abe.put("id", 2);
			
			Map<String, Object> bob = new HashMap<String, Object>();
			bob.put("name", "bob");
			bob.put("id", 3);
			
			Map<String, Object> tom = new HashMap<String, Object>();
			tom.put("name", "tom");
			tom.put("id", 4);
			
			List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>();
			list1.add(abe);
			list1.add(allen);
			list2.add(bob);
			list3.add(tom);

			result.put("T", list3);
			result.put("B", list2);
			result.put("A", list1);
			
			respond.put("friendList", result);
			respond.put("error", "");
		}catch(NullPointerException e){
			respond.put("error", ErrorMessage.INCORRECT_PARAM.getMsg());
		}catch(IllegalStateException e){
			respond.put("error", e.getMessage());
		}catch(NotFoundException e){
			respond.put("error", e.getMessage());
		}catch(SessionExpiredException e){
			respond.put("error", ErrorMessage.SESSION_EXPIRED.getMsg());
		}
		return new ResponseEntity<Map<String, Object>>(respond, HttpStatus.OK);
	}

}

@@ -3988,17 +3988,17 @@
 name%22, %22
-a
+A
 llen%22);%0A
@@ -4104,17 +4104,17 @@
 name%22, %22
-a
+A
 be%22);%0A%09%09
@@ -4216,17 +4216,17 @@
 name%22, %22
-b
+B
 ob%22);%0A%09%09
@@ -4328,17 +4328,17 @@
 name%22, %22
-t
+T
 om%22);%0A%09%09
@@ -4632,17 +4632,17 @@
 %0A%09%09%09list
-2
+1
 .add(bob
@@ -4651,17 +4651,17 @@
 %0A%09%09%09list
-3
+1
 .add(tom
@@ -4718,16 +4718,178 @@
 list2);%0A
+%09%09%09result.put(%22Z%22, list1);%0A%09%09%09result.put(%22S%22, list1);%0A%09%09%09result.put(%22K%22, list1);%0A%09%09%09result.put(%22N%22, list1);%0A%09%09%09result.put(%22V%22, list1);%0A%09%09%09result.put(%22C%22, list1);%0A
 %09%09%09resul
@@ -4940,22 +4940,21 @@
 dList%22, 
-result
+list1
 );%0A%09%09%09re

Test move sort logic into mobile side