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

import java.util.HashMap;
import java.util.Map;

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

import org.mynonglam.dao.AccountDAO;
import org.mynonglam.dao.LikingDAO;
import org.mynonglam.dao.PhoneDAO;
import org.mynonglam.dao.PlaceDAO;
import org.mynonglam.dao.ProfesstionDAO;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.Liking;
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;

/**
 * @author Dao
 * 
 */
@Controller
public class AccountUpdateControl {

	private AccountDAO accountDAO;
	private LikingDAO likingDAO;
	private ProfesstionDAO professtionDAO;
	private PhoneDAO phoneDAO;
	private PlaceDAO placeDAO;

	/**
	 * 
	 */
	public AccountUpdateControl() {
		accountDAO = new AccountDAO();
		likingDAO = new LikingDAO();
		professtionDAO = new ProfesstionDAO();
		phoneDAO = new PhoneDAO();
		placeDAO = new PlaceDAO();
	}

	//
	@RequestMapping(value = "registrationaccount", method = RequestMethod.POST)
	public @ResponseBody String registrationAccount(HttpServletRequest request, HttpSession session) {
		String result = "fail";

		String email = (String) request.getParameter("emailre").trim();
		if(email != null) {
			String fullname = (String) request.getParameter("fullnamere").trim();
			String pass = (String) request.getParameter("passre").trim();
			String birthday = (String) request.getParameter("birthdayre").trim();
			
			boolean gender = Boolean.getBoolean(request.getParameter("genderre").trim());
			if(fullname != null && pass != null && birthday != null) {
				//check email exit
				if(!accountDAO.checkEmailExits(email)) {
					Account account = new Account();
					account.setFullname(fullname);
					account.setEmail(email);
					account.setPassword(Account.md5(pass));
					account.setDayofbirth(birthday);
					account.setGender(gender);
					// insert
					if(accountDAO.insertAccount(account)) {
						result = "success";
					}
				} else {
					result = "exits";
				}
			}
		}
		
		return result;
	}
	
	/**
	 * update info basic by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateinfobasic", method = RequestMethod.POST)
	public @ResponseBody
	boolean updateInfoBasic(HttpServletRequest request, HttpSession session) {
		boolean result = false;

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String gender = (String) request.getParameter("gen").trim();
			String country = (String) request.getParameter("city").trim();
			String dayofbirth = (String) request.getParameter("dayofb").trim();

			Account account = new Account();
			account.setEmail(email);
			account.setCountry(country);
			try {
				account.setGender(Boolean.parseBoolean(gender));
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			account.setDayofbirth(dayofbirth);

			result = accountDAO.updateInfo(account);
		}
		return result;
	}

	/**
	 * update liking by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateinfoliking", method = RequestMethod.POST)
	public @ResponseBody boolean updateInfoLiking(HttpServletRequest request, HttpSession session) {
		boolean result = false;

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String exits = (String) request.getParameter("exits").trim();
			String musics = (String) request.getParameter("musics").trim();
			String movies = (String) request.getParameter("movies").trim();
			String books = (String) request.getParameter("books").trim();
			String likings = (String) request.getParameter("likings").trim();

			Liking liking = new Liking();
			liking.setHobbies(likings);
			liking.setBooks(books);
			liking.setMovies(movies);
			liking.setMusics(musics);
			liking.setEmail(email);

			if (exits.equals("yes")) {
				// update
				result = likingDAO.updateLikingForAccount(liking);
			} else {
				// insert
				result = likingDAO.insertLikingForAccount(liking);
			}
		}

		return result;
	}
	
	/**
	 * insert professtion by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "insertinfoprofestion", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> insertInfoProfes(HttpServletRequest request, HttpSession session) {
		Map<String, String> result = new HashMap<String, String>();
		String email = (String) session.getAttribute("accountinfo");
		boolean insert = false;
		String idpro = "";
		if (email != null) {
			String company = request.getParameter("comre").trim();
			String city = request.getParameter("cityre").trim();
			String desi = request.getParameter("desire").trim();
			String ystart = request.getParameter("ystartre").trim();
			String yend = request.getParameter("yendre").trim();
			
			if(company != null && city != null && desi != null && ystart != null && yend != null) {
				Account account = new Account();
				account.setEmail(email);
				Professtion professtion = new Professtion();
				idpro = nextID("P", 10, professtionDAO.getIDProfesLast());
				professtion.setIdpro(idpro);
				professtion.setAccount(account);
				professtion.setCity(city);
				professtion.setCompanyname(company);
				professtion.setDesignation(desi);
				professtion.setStartyear(ystart);
				professtion.setEndyear(yend);
				
				insert = professtionDAO.insertProfesstionForAccount(professtion);
			}
		}
		
		if(insert) {
			result.put("check", "True");
			result.put("idpro", idpro);
		} else {
			result.put("check", "False");
		}
		return result;
	}
	
	/**
	 * update infomation professtion by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateinfoprofestion", method = RequestMethod.POST)
	public @ResponseBody boolean updateInfoProfes(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idpro = request.getParameter("idprore").trim();
			String company = request.getParameter("comre").trim();
			String city = request.getParameter("cityre").trim();
			String desi = request.getParameter("desire").trim();
			String ystart = request.getParameter("ystartre").trim();
			String yend = request.getParameter("yendre").trim();
			
			if(company != null && city != null && desi != null && ystart != null && yend != null) {
				Professtion professtion = new Professtion();
				professtion.setIdpro(idpro);
				professtion.setCity(city);
				professtion.setCompanyname(company);
				professtion.setDesignation(desi);
				professtion.setStartyear(ystart);
				professtion.setEndyear(yend);
				
				result = professtionDAO.updateProfesstionForAccount(professtion);
			}
		}
		return result;
	}
	
	
	/**
	 * delete professtion by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "deleteinfoprofestion", method = RequestMethod.POST)
	public @ResponseBody boolean deleteProfesstion(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idpro = request.getParameter("idprore").trim();
			if(idpro != null && idpro.trim() != "") {
				result = professtionDAO.deleteProfesstionAccount(idpro, email);
			}
		}
		return result;
	}
	
	/**
	 * insert phone number by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "insertinfophone", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> insertPhone(HttpServletRequest request, HttpSession session) {
		Map<String, String> result = new HashMap<String, String>();
		String email = (String) session.getAttribute("accountinfo");
		boolean insert = false;
		String idphone = "";
		if (email != null) {
			String sphone = request.getParameter("phonere").trim();
			if(sphone!= null && sphone.trim() != "") {
				Account account = new Account();
				account.setEmail(email);
				Phone phone = new Phone();
				phone.setAccount(account);
				idphone = nextID("D", 10, phoneDAO.getIDPhoneLast());
				phone.setIdphone(idphone);
				phone.setNumber(sphone.trim());
				
				insert = phoneDAO.insertPhone(phone);
			}
		}
		
		if(insert) {
			result.put("check", "True");
			result.put("idphone", idphone);
		} else {
			result.put("check", "False");
		}
		return result;
	}
	
	/**
	 * update phone number by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateinfophone", method = RequestMethod.POST)
	public @ResponseBody boolean updateInfoPhone(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idphone = request.getParameter("idphonere").trim();
			String number = request.getParameter("phonere").trim();
			
			if(idphone != null && number != null) {
				Phone phone = new Phone();
				phone.setIdphone(idphone);
				phone.setNumber(number);
				
				result = phoneDAO.updatePhone(phone);
			}
		}
		return result;
	}
	
	/**
	 * delete phone number by account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "deleteinfophonenumber", method = RequestMethod.POST)
	public @ResponseBody boolean deleteInfoPhone(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idphone = request.getParameter("idphonere").trim();
			
			if(idphone != null && idphone.trim().length() > 0) {		
				result = phoneDAO.deletePhoneNumber(idphone, email);
			}
		}
		return result;
	}
	
	/**
	 * insert place account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "insertinfoplaceacc", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> insertPlace(HttpServletRequest request, HttpSession session) {
		Map<String, String> result = new HashMap<String, String>();
		String email = (String) session.getAttribute("accountinfo");
		boolean insert = false;
		String idplace = "";
		if (email != null) {
			String address = request.getParameter("addressre").trim();
			String city = request.getParameter("cityre").trim();
			String yStart = request.getParameter("startre").trim();
			String yEnd = request.getParameter("endre").trim();
			if(address!= null && city != null && yStart != null && yEnd != null) {
				Account account = new Account();
				account.setEmail(email);
				Place place = new Place();
				place.setAccount(account);
				idplace = nextID("P", 10, placeDAO.getIDPlaceLast());
				place.setIdplace(idplace);
				place.setAddress(address);
				place.setCity(city);
				place.setStartyear(yStart);
				place.setEndyear(yEnd);
				
				insert = placeDAO.insertPlaceAccount(place);
			}
		}
		
		if(insert) {
			result.put("check", "True");
			result.put("idplace", idplace);
		} else {
			result.put("check", "False");
		}
		return result;
	}
	
	/**
	 * update place for account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateinfoplaceacc", method = RequestMethod.POST)
	public @ResponseBody boolean updateInfoPlace(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idplace = request.getParameter("idplacere").trim();
			String address = request.getParameter("addressre").trim();
			String city = request.getParameter("cityre").trim();
			String start = request.getParameter("startre").trim();
			String end = request.getParameter("endre").trim();
			
			if(idplace != null && address != null && start != null && end != null) {
				Place place = new Place();
				place.setIdplace(idplace);
				place.setAddress(address);
				place.setCity(city);
				place.setStartyear(start);
				place.setEndyear(end);
				
				result = placeDAO.updatePlace(place);
			}
		}
		return result;
	}
	
	/**
	 * delete place for account
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "deleteinfoplaceacc", method = RequestMethod.POST)
	public @ResponseBody boolean deleteInfoPlace(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idplace = request.getParameter("idplacere").trim();
			
			if(idplace != null && idplace.trim().length() > 0) {		
				result = placeDAO.deletePlace(idplace, email);
			}
		}
		return result;
	}

	/**
	 * next id
	 * @param sStart
	 * @param lenght
	 * @param sLast
	 * @return
	 */
	public String nextID(String sStart, int lenght, String sLast) {
		String result = sStart;
		
		if (sLast.length() > 0) {
			String temp = sLast.substring(sStart.length());
			int number = Integer.parseInt(temp) + 1;
			int numlenght = (number + "").length();
			for(int i = (sStart.length() + numlenght); i < lenght; i++) {
				result += "0";
			}
			result += number;
		} else {
			for(int i = (sStart.length() + 1); i < lenght; i++) {
				result += "0";
			}
			result += "1";
		}
		
		return result;
	}
	
//	@RequestMapping(value = "test", method = RequestMethod.POST)
//	public @ResponseBody Map<String, String> test() {
//		Map<String, String> result = new HashMap<String, String>();
//		result.put("result", "True"); 
//		return result;
//	}

}
