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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.mynonglam.dao.AlbumDAO;
import org.mynonglam.dao.CoomentPhotoDAO;
import org.mynonglam.dao.Emoticon;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.Album;
import org.mynonglam.entitys.CommentPhoto;
import org.mynonglam.entitys.LinkPhoto;
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 AlbumControl {

	private AlbumDAO albumDAO;
	private CoomentPhotoDAO comentPhotoDAO;

	/**
	 * 
	 */
	public AlbumControl() {
		albumDAO = new AlbumDAO();
		comentPhotoDAO = new CoomentPhotoDAO();
	}

	@RequestMapping(value = "ajaxcreatealbumdata", method = RequestMethod.POST)
	public @ResponseBody
	String insertAlbum(HttpServletRequest request, HttpSession session) {
		String result = "no";
		boolean insert = false;
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idalbum = request.getParameter("id");
			if (idalbum == null) {
				String namealbum = (String) request.getParameter("albumname");
				if (namealbum == null) {
					namealbum = "Album chưa đặt tên";
				}
				Album album = new Album();
				idalbum = nextID("A", 10, albumDAO.getIDAlbumLast());
				album.setIdalbum(idalbum);
				album.setName(namealbum);
				album.setDescription(namealbum);
				Account account = new Account();
				account.setEmail(email);
				album.setAccount(account);
				album.setDatecreate(new SimpleDateFormat("yyyy/MM/dd")
						.format(new Date()));

				if (albumDAO.insertAlbum(album)) {
					String link = (String) request.getParameter("linkre");
					String des = (String) request.getParameter("desre");
					if (idalbum != null && link != null && des != null) {
						LinkPhoto linkPhoto = new LinkPhoto();
						linkPhoto.setIdphoto(nextID("L", 10,
								albumDAO.getIDLinkPhotoLast()));
						Album album1 = new Album();
						album1.setIdalbum(idalbum);
						linkPhoto.setAlbum(album1);
						linkPhoto.setDiscription(des);
						linkPhoto.setLink(link);
						linkPhoto.setDatecreate(new SimpleDateFormat(
								"yyyy/MM/dd").format(new Date()));

						if (albumDAO.insertLinkPhoto(linkPhoto)) {
							result = idalbum;
						}
					}
					result = idalbum;
				}
			} else {
				String link = (String) request.getParameter("linkre");
				String des = (String) request.getParameter("desre");
				if (idalbum != null && link != null && des != null) {
					LinkPhoto linkPhoto = new LinkPhoto();
					linkPhoto.setIdphoto(nextID("L", 10,
							albumDAO.getIDLinkPhotoLast()));
					Album album = new Album();
					album.setIdalbum(idalbum);
					linkPhoto.setAlbum(album);
					linkPhoto.setDiscription(des);
					linkPhoto.setLink(link);
					linkPhoto.setDatecreate(new SimpleDateFormat("yyyy/MM/dd")
							.format(new Date()));

					if (albumDAO.insertLinkPhoto(linkPhoto)) {
						result = idalbum;
					}
				}
			}

		}

		return result;
	}

	/**
	 * @RequestMapping(value = "ajaxcreatealbumdata", method =
	 *                       RequestMethod.POST) public @ResponseBody boolean
	 *                       insertAlbum(HttpServletRequest request, HttpSession
	 *                       session) {
	 * 
	 *                       boolean result = false; String json = (String)
	 *                       request.getParameter("datare");
	 *                       System.out.println(json); //if (json != null) {
	 *                       Test ada = new Gson().fromJson(json, Test.class);
	 *                       System.out.println( "askjdhajs " + ada);
	 * 
	 *                       // if(namealbum != null) { // Album album = new
	 *                       Album(); // idalbum = nextID("A", 10,
	 *                       albumDAO.getIDAlbumLast()); //
	 *                       album.setIdalbum(idalbum); //
	 *                       album.setName(namealbum); //
	 *                       album.setDescription(namealbum); // Account account
	 *                       = new Account(); // account.setEmail(email); //
	 *                       album.setAccount(account); //
	 *                       album.setDatecreate(new
	 *                       SimpleDateFormat("yyyy/MM/dd").format(new Date()));
	 *                       // // insert = albumDAO.insertAlbum(album); // }
	 *                       //}
	 * 
	 *                       return result; }
	 */

	@RequestMapping(value = "getalbumviewprofile", method = RequestMethod.POST)
	public @ResponseBody
	String getAlbumViewProfile(HttpServletRequest request, HttpSession session) {
		String result = "null";

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String emailload = (String) request.getParameter("emailget");
			if (emailload != null) {
				List<Album> albums = albumDAO.getAlbumsView(emailload);
				result = new Gson().toJson(albums);
				System.out.println(result);
			}
		}

		return result;
	}

	@RequestMapping(value = "getalbumdetailviewprofile", method = RequestMethod.POST)
	public @ResponseBody
	String getAlbumDetailViewProfile(HttpServletRequest request,
			HttpSession session) {
		String result = "null";

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idalbum = (String) request.getParameter("idalbumre");
			if (idalbum != null) {
				int page = Integer.valueOf(request.getParameter("pagere"));
				Album album = albumDAO.getAlbumByID(idalbum);
				if (album != null) {
					int numload = 6;// 12
					List<LinkPhoto> photos = albumDAO.getAlbumDetailsView(
							idalbum, ((page - 1) * numload), numload);
					if (photos.size() == 0) {
						result = "noload";
						result = new Gson().toJson(result);
					} else {
						album.setLinkphotos(photos);
						result = new Gson().toJson(album);
					}
				}
			}
		} else {
			result = new Gson().toJson(result);
		}
		System.out.println(result);

		return result;
	}

	@RequestMapping(value = "getcommentphotoviewprofile", method = RequestMethod.POST)
	public @ResponseBody
	String getCommentPhotoViewProfile(HttpServletRequest request,
			HttpSession session) {
		String result = "null";

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idphoto = (String) request.getParameter("idphotore");
			// System.out.println(idphoto);
			if (idphoto != null) {
				LinkPhoto linkPhoto = albumDAO.getLinkPhoto(idphoto);
				if (linkPhoto != null) {
					List<CommentPhoto> comments = albumDAO
							.getCommentPhotoView(idphoto);
					Map<String, Object> temp = new HashMap<String, Object>();
					temp.put("photo", linkPhoto);
					temp.put("comments", comments);
					result = new Gson().toJson(temp);
				}
			}
		} else {
			result = new Gson().toJson(result);
		}
		System.out.println(result);

		return result;
	}

	@RequestMapping(value = "acccommentphoto", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, String> accountCommentPhoto(HttpServletRequest request,
			HttpSession session) {
		String idnextcomment = "";
		boolean insert = false;
		String contentcomment = "";
		Map<String, String> result = new HashMap<String, String>();
		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			contentcomment = (String) request.getParameter("contentre");
			String idphoto = (String) request.getParameter("photore");
			if (contentcomment != null && idphoto != null) {
				contentcomment = new Emoticon().replaceEmo(contentcomment);
				idnextcomment = nextID("C", 10,
						comentPhotoDAO.getIDCommentPhotoLast());
				CommentPhoto comment = new CommentPhoto();
				comment.setIdcomphoto(idnextcomment);
				comment.setContent(contentcomment);
				Account account = new Account();
				account.setEmail(email);
				comment.setAccount(account);
				LinkPhoto linkPhoto = new LinkPhoto();
				linkPhoto.setIdphoto(idphoto);
				comment.setLinkphoto(linkPhoto);
				comment.setCommentdate(new SimpleDateFormat("yyyy/MM/dd")
						.format(new Date()));

				insert = comentPhotoDAO.insertCommentPhoto(comment);
			}
		}

		if (insert) {
			result.put("result", "true");
			result.put("idcomphoto", idnextcomment);
			result.put("content", contentcomment);
		} else {
			result.put("result", "false");
		}

		return result;
	}

	@RequestMapping(value = "ajaxdeletecommentphoto", method = RequestMethod.POST)
	public @ResponseBody
	boolean ajaxDeleteCommentPhoto(HttpServletRequest request,
			HttpSession session) {
		boolean result = false;

		String email = (String) session.getAttribute("accountinfo");
		if (email != null) {
			String idcomment = request.getParameter("idcomphotore");
			if (idcomment != null) {
				result = comentPhotoDAO.deleteCommentPhoto(idcomment, 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;
	}

}
