package com.wechat.controller;

import java.io.IOException;
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.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wechat.common.utils.StringsUtils;
import com.wechat.global.base.BaseController;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeFriendRelation;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.requestDto.PageReq;
import com.wechat.model.requestDto.UserCodeWithPage;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.HumanVeinService;

@Controller
@RequestMapping("humanVein")
public class HumanVeinController extends BaseController {

	@Autowired
	HumanVeinService humanVeinService;

	@Autowired
	CommonService commonService;

	/**
	 * 人脉主页初始化
	 * 	  获取好友邀请信息和广告内容
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "")
	@ResponseBody
	public ResultEntity<Map<String, Object>> initHumanVeinPage(HttpServletRequest req) {
		String userCode = commonService.getUserCode(req);
		Map<String, Object> map = humanVeinService.init(userCode);

		return new ResultEntity<Map<String, Object>>(InfoMsg.SUCCESS_REQUEST, map);
	}
	
	/**
	 * 获取页面中好友更新动态和好友列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "getFriendsAndTrends",method=RequestMethod.POST)
	@ResponseBody
	public ResultEntity<Map<String, Object>> getFriendsAndTrends(HttpServletRequest req,@RequestBody PageReq page) {
		String userCode = commonService.getUserCode(req);
		String lang = commonService.getLanguage(req);
		Map<String, Object> map = humanVeinService.getFriendsAndTrends(userCode,lang, String.valueOf(page.getSize()));

		return new ResultEntity<Map<String, Object>>(InfoMsg.SUCCESS_REQUEST, map);
	}
	
	
	/**
	 * 可能认识的人
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "getMaybeFriends",method=RequestMethod.GET)
	@ResponseBody
	public ResultEntity<Map<String, Object>> getMaybeFriends(HttpServletRequest req) {
		String userCode = commonService.getUserCode(req);
		String lang = commonService.getLanguage(req);
		String identityFlag = commonService.getIdentityFlag(req);
		
		Map<String, Object> map = new HashMap<String, Object>();
		// 可能认识的人
		map.put("maybeFriends", commonService.getMaybeFriends(userCode,identityFlag,lang));

		return new ResultEntity<Map<String, Object>>(InfoMsg.SUCCESS_REQUEST, map);
	}
		

	/**
	 * 获取好友列表
	 * @param page
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "getPagefriends",method=RequestMethod.POST)
	@ResponseBody
	public ResultEntity<PagingResult<MdeUser>> getPagefriends(@RequestBody PageReq page,HttpServletRequest req) {
		PagingResult<MdeUser> pagingResult = humanVeinService.getPagefriends(req, page);
		return new ResultEntity<>(InfoMsg.SUCCESS_REQUEST,pagingResult);
	}
	
	/**
	 * 好友搜索
	 * 
	 * @param map
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "friendSearch", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<PagingResult<MdeUser>> friendSearch(@RequestBody Map<String, String> map,HttpServletRequest req) throws IOException {
		return new ResultEntity<>(InfoMsg.SUCCESS_REQUEST,humanVeinService.friendSearch(map,req));
	}

	/**
	 * 新的好友邀请信息获取
	 * 
	 * @param req
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping(value = "getMakeFriendInfo")
//	@ResponseBody
//	public ResultEntity<List<MdeFriendRelation>> getMakeFriendInfo(HttpServletRequest req) throws IOException {
//		String userCode = commonService.getUserCode(req);
//		return humanVeinService.getMakeFriendInfo(userCode);
//	}

	/**
	 * 好友添加确认(0同意，1拒绝)
	 * 
	 * @param map
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "friendsConfirm", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> friendsConfirm(@RequestBody Map<String, Object> map, HttpServletRequest req) throws IOException {
		// 好友申请人
		String userCodeReq = map.get("userCodeReq").toString();
		// 好友处理人 map.get("userCodeResp").toString();
		String userCodeResp = commonService.getUserCode(req) + "";
		// 0同意，1拒绝
		String confirm = map.get("confirm").toString();
		if (StringsUtils.isEmpty(userCodeReq) || StringsUtils.isEmpty(confirm)) {
			return new ResultEntity<>(InfoMsg.ERROR_NULL_PARAM);
		}
		return humanVeinService.friendsConfirm(userCodeReq, userCodeResp, confirm);
	}

	/**
	 * 我的好友
	 * 
	 * @param pageNum
	 * @param req
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping(value = "getFriendsInfo", method = RequestMethod.POST)
//	@ResponseBody
//	public ResultEntity<PagingResult<MdeUser>> getFriendsInfo(@RequestBody UserCodeWithPage param, HttpServletRequest req)
//			throws IOException {
//		String userCode = commonService.getUserCode(req);
//		param.setUserCode(userCode);
//		PagingResult<MdeUser> result = humanVeinService.findFriends(param);
//		return new ResultEntity<PagingResult<MdeUser>>(InfoMsg.SUCCESS_REQUEST, result);
//	}
	
	
	/**
	 * 删除 同意好友申请
	 * @param userCode
	 * @param req
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "deleteComfirmedFriendship", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<String> deleteComfirmedFriendship(@RequestParam String userCode, HttpServletRequest req)
			throws IOException {
		String currentUserCode = commonService.getUserCode(req);
		commonService.deleteComfirmedFriendship(currentUserCode, userCode);
		return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
	}
	
}
