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

import java.util.ArrayList;

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

import org.mynonglam.dao.FriendListDAO;
import org.mynonglam.dao.PhoneDAO;
import org.mynonglam.dao.PlaceDAO;
import org.mynonglam.dao.ProfesstionDAO;
import org.mynonglam.entitys.FriendList;
import org.mynonglam.entitys.Phone;
import org.mynonglam.entitys.Place;
import org.mynonglam.entitys.Professtion;
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;

/**
 * @author Dao
 *
 */
@Controller
public class AccountGetControl {
	
	private ProfesstionDAO professtionDAO;
	private PhoneDAO phoneDAO;
	private PlaceDAO placeDAO;
	private FriendListDAO friendListDAO;

	/**
	 * 
	 */
	public AccountGetControl() {
		professtionDAO = new ProfesstionDAO();
		phoneDAO = new PhoneDAO();
		placeDAO = new PlaceDAO();
		friendListDAO = new FriendListDAO();
	}


	@RequestMapping(value = "getinfoprofestion", method = RequestMethod.POST)
	public @ResponseBody Professtion getProfesstion(HttpServletRequest request, HttpSession session) {
		Professtion result = null;
		
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idpro = request.getParameter("idprore");
			if(idpro != null && idpro.trim() != "") {
				result = professtionDAO.getProfesstion(idpro);
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "getinfophone", method = RequestMethod.POST)
	public @ResponseBody Phone getInfoPhone(HttpServletRequest request, HttpSession session) {
		Phone result = null;
		
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idphone = request.getParameter("idphonere");
			if(idphone != null) {
				result = phoneDAO.getPhoneForAccountByIDPhone(idphone);
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "getinfoplace", method = RequestMethod.POST)
	public @ResponseBody Place getInfoPlace(HttpServletRequest request, HttpSession session) {
		Place result = null;
		
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idplace = request.getParameter("idplacere");
			if(idplace != null) {
				result = placeDAO.getPlaceForAccount(idplace);
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "getlistfriendsacc", method = RequestMethod.POST)
	public @ResponseBody String getFriendsAcc(HttpServletRequest request, HttpSession session) {
		String result = "null";
		
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			ArrayList<FriendList> friends = friendListDAO.getFriends(email);
			result = new Gson().toJson(friends);
		} else {
			result = new Gson().toJson(result);
		}
		
		return result;
	}
	
	@RequestMapping(value = "getlistfriendsacclimit", method = RequestMethod.POST)
	public @ResponseBody String getFriendsAccLimit(HttpServletRequest request, HttpSession session) {
		String result = "null";
		
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String emailaccload = request.getParameter("idacc");
			int page = Integer.valueOf(request.getParameter("pagere"));
			int numload = 6;
			ArrayList<FriendList> friends = friendListDAO.getFriendsLimit(emailaccload, ((page - 1) * numload), numload);
			if(friends.size() == 0) {
				result = new Gson().toJson("noload");
			} else {
				result = new Gson().toJson(friends);
			}
		} else {
			result = new Gson().toJson(result);
		}
		System.out.println(result);
		
		return result;
	}
	
}
