package org.mynonglam.controllers;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

import org.mynonglam.dao.AccountDAO;
import org.mynonglam.dao.FriendSearchingMod;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.SearchingFriend;
import org.mynonglam.server.Notificate;
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 SearchFriend {
	
	@Autowired
	private FriendSearchingMod searchMod;
	
	@RequestMapping(value = "getSearchingResult", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getSearchingResult(HttpServletRequest request) throws SQLException {
		String keyword = request.getParameter("keyword");
		int offset = Integer.parseInt(request.getParameter("offset"));
		int limit = 8;
		//int record = searchMod.getRecordNumber(keyword);
		ArrayList<SearchingFriend> list = searchMod.seach(keyword, offset, limit);
		if (list.size() == 8) {
			list.remove(list.size() - 1);
			list.add(new SearchingFriend("more", "" + (offset + 7), "", ""));
		}
		return new Gson().toJson(list);
	}
	
	@RequestMapping(value = "sendFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String sendFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String res = "true";
		String from = (String) session.getAttribute("accountinfo");
		String to = request.getParameter("to");
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy - hh:mm");
		String date = sdf.format(new Date());
		boolean c1 = searchMod.checkAlreadyFriendRequest(from, to);
		boolean c2 = searchMod.checkOtherSendFriendRequest(from, to);
		boolean result = false;
		if (c1 == true || c2 == true) 
			res = "false";
		else {
			result = searchMod.addFriend(1, from, to, date, "", 0, 0, 1);
			if (result == false) res = "false";
		}
		Account acc = new AccountDAO().getAccount(from);
		Notificate.sendPrivate(to, "noti\0addfriend\0add\0" + acc.getEmail() + "\0" + acc.getFullname() + "\0" + acc.getAvatar() + "\0Đã gửi 1 yêu cầu kết bạn");
		return res;
	}
	
	@RequestMapping(value = "cancelFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String cancelFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		String to = request.getParameter("to");
		searchMod.deleteRelationship(from, to);
		Notificate.sendPrivate(to, "noti\0addfriend\0cancel\0" + from);
		return "true";
	}
	
	@RequestMapping(value = "acceptFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String acceptFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		String to = request.getParameter("to");
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy - hh:mm");
		String date = sdf.format(new Date());
		searchMod.acceptFriend(to, from, date);
		Account acc = new AccountDAO().getAccount(from);
		// goi de tra ve
		Notificate.sendPrivate(to, "noti\0addfriend\0accept\0" + acc.getEmail() + "\0" + acc.getFullname() + "\0" + acc.getAvatar() + "\0Đã chấp nhận yêu cầu kết bạn");
		
		return "true";
	}
	
	@RequestMapping(value = "getMyFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getMyFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		ArrayList<SearchingFriend> list = searchMod.getMyRequest(from);
		return new Gson().toJson(list);
	}
	
	@RequestMapping(value = "getFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		ArrayList<SearchingFriend> list = searchMod.getFriendRequest(from);
		return new Gson().toJson(list);
	}
	
	@RequestMapping(value = "getHintFriends", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String getHintFriends(HttpServletRequest request, HttpSession session) throws SQLException {
		String myEmail = (String) session.getAttribute("accountinfo");
		ArrayList<SearchingFriend> list = searchMod.getHintFriends(myEmail);
		return new Gson().toJson(list);
	}
	
	@RequestMapping(value = "countUnviewedMyFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String countUnviewedMyFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		return "" + searchMod.countUnviewedFriendRequest(from);
	}
	
	@RequestMapping(value = "setViewedFriendRequest", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String setViewedFriendRequest(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		searchMod.setRequestViewed(from);
		return "0";
	}
	
	@RequestMapping(value = "checkFriendRequestStatus", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public @ResponseBody String checkFriendRequestStatus(HttpServletRequest request, HttpSession session) throws SQLException {
		String from = (String) session.getAttribute("accountinfo");
		String to = request.getParameter("to");
		boolean c1 = searchMod.checkAlreadyFriendRequest(from, to);
		boolean c2 = searchMod.checkOtherSendFriendRequest(from, to);
		boolean isFriends = searchMod.checkIsFriends(from, to);
		if (isFriends) return "friend";
		if (c1) return "sending";
		else if (c2) return "accept";
		return "0";
	}
}
