/**
 * 
 */
package org.mynonglam.controllers;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mynonglam.dao.MessageMod;
import org.mynonglam.entitys.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

@Controller
public class GetMessages {
	
	@Autowired
	private MessageMod msgMod;


	@RequestMapping(value = "getChatMsg", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getMessages(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("accountinfo");
	    String receiver = request.getParameter("receiver");
	    int offset = 0;
	    ArrayList<Message> list = new ArrayList<Message>();
	    try {
	    	offset = Integer.parseInt(request.getParameter("offset"));
	    } catch(Exception e){
			list.add(0, new Message("<div new='root' class='chatbox-msg-header'></div>", "", "", "", "", ""));
			return new Gson().toJson(list);
	    }
	    int temp_limit = offset;
	    int limit = 7;
	    
	    try {
	    	int record = msgMod.getRecordNumber(username, receiver);
	    	if(record < 7) offset = 0;
	    	else {
	    		if (offset  == 0) offset = record - limit;
		    	else offset -= limit;
		    	if (offset < 0) {
		    		offset = 0;
		    		limit = temp_limit;
		    	}
	    	}
	    	
			list = msgMod.getMessages(username, receiver, offset, limit);		
			
			if (offset != 0){
				list.add(0, new Message("<div new='" + offset + "' class='chatbox-msg-header'></div>", "", "", "", "", ""));
				return new Gson().toJson(list);
			} else {
				list.add(0, new Message("<div new='root' class='chatbox-msg-header'></div>", "", "", "", "", ""));
				return new Gson().toJson(list);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	@RequestMapping(value = "getMsgOffline", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getMsgOffline(HttpServletRequest request) throws UnsupportedEncodingException, SQLException {
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("accountinfo");
		ArrayList<Message> list = msgMod.getOffMessages(username);
		
		HashMap<String, Message> myMap = new HashMap<String, Message>();
		int amount = 1;
		TreeSet<String> mySet = new TreeSet<String>();
		for (Message message : list) {
			mySet.add(message.getId());
		}
		for (String sender : mySet) {
			for (Message message : list) {
				if (sender.equals(message.getId())){
					myMap.put(sender, new Message(sender, message.getSender(), message.getReceiver(), message.getContent(), "" + amount, "" + mySet.size()));
					amount++;
				} else {
					amount = 1;
				}
			}
		}
		return new Gson().toJson(myMap);
	}
	
	@RequestMapping(value = "setViewedOfflineMsg", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String setViewedOfflineMsg(HttpServletRequest request) throws UnsupportedEncodingException, SQLException {
		HttpSession session = request.getSession();
		String from = request.getParameter("from");
		String to = (String)session.getAttribute("accountinfo");
		msgMod.setViewedMessage(from, to);
		return "";
	}
	
}
