/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.message.web;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ffii.core.utils.CriteriaUtils;
import com.ffii.core.utils.NumberUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.web.ServletRequestUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.message.Message;
import com.ffii.tbms.message.service.MessageService;

/**
 * Message Controller
 *
 * @author Patrick
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController extends AbstractController {

	@Autowired
	private MessageService messageService;

	@RequestMapping(value = "/list.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String threadList(Model model, HttpServletRequest request, 
	@RequestParam(defaultValue = "false") Boolean unRead) throws Exception {

		Map<String, Object> args = new HashMap<>();
		CriteriaUtils.addInteger(request, args,"start");
		CriteriaUtils.addInteger(request, args,"limit");
		CriteriaUtils.addStringLike(request, args,"query");
		args.put("unRead", unRead);

		List<Map<String, Object>> records = messageService.search(args);
		Map<String, Object> count = messageService.searchCount(args);

		model.addAttribute(Params.RECORDS, records);
		model.addAttribute("total", NumberUtils.intValue(count.get("count")));
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}
	
	@RequestMapping(value = "/new", method = { RequestMethod.GET, RequestMethod.POST })
	public String getNewCount(Model model, HttpServletRequest request) throws Exception {

		Map<String, Object> args = new HashMap<>();
		args.put("unRead", true);

		Map<String, Object> count = messageService.searchCount(args);

		model.addAttribute("new", NumberUtils.intValue(count.get("count")));
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/update-readtime.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String markReadTime(Model model, HttpServletRequest request) throws Exception {

		int[] ids = ServletRequestUtils.getIntParameters(request, "ids");
		if (ids != null) {
			for (int id : ids) {
				Message instance = messageService.find(id);
				instance.setReadTime(new Date());
				messageService.saveOrUpdate(instance);
			}
		}
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		return JsonView.NAME;
	}
}
