package com.wechat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wechat.common.utils.StringsUtils;
import com.wechat.global.CustomException;
import com.wechat.global.base.BaseController;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeInformation;
import com.wechat.model.dbEntity.MdeTrends;
import com.wechat.model.requestDto.FriendRelationReq;
import com.wechat.model.requestDto.SendTrendsResp;
import com.wechat.model.requestDto.TrendCommentInfo;
import com.wechat.model.requestDto.TrendThumbUpInfo;
import com.wechat.model.requestDto.UserCodeWithPage;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.MainPageService;
import com.wechat.service.MdeTrendsService;

@Controller
@RequestMapping("mainPage")
public class MainPageController extends BaseController{
	
	@Autowired
	private MainPageService mainPageService;
	
	@Autowired
	private CommonService commonService;
	@Autowired
	private MdeTrendsService trendsService;
	
	/**
	 * 主页初始化（广告内容加载）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<Map<String,Object>> initMainPage(HttpServletRequest request) throws Exception {
		Map<String,Object> map = mainPageService.init();
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	
	/**
	 * 获取可能认识的人
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getMaybeFriends",method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<Map<String,Object>> getMaybeFriends(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		String identityFlag = commonService.getIdentityFlag(request);
		String lang = commonService.getLanguage(request);
		
		Map<String,Object> map = mainPageService.maybeFriends(userCode,lang,identityFlag);
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	
	/**
	 * 获取热门资讯
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getInforOfZixun",method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<List<MdeInformation>> getInforOfZixun(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		// 热门资讯
		List<MdeInformation> list = commonService.getInformationsOfZixun(userCode);	
		return new ResultEntity<List<MdeInformation>>(InfoMsg.SUCCESS_REQUEST,list);
	}
	
	
	/**
	 * 获取热门资讯、动态广告详情
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getHotInformations",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<MdeInformation> getHotInformatinos(HttpServletRequest request,@RequestParam String infoId) throws Exception {
		String userCode = commonService.getUserCode(request);
		String lang = commonService.getLanguage(request);
		return new ResultEntity<MdeInformation>(InfoMsg.SUCCESS_REQUEST,mainPageService.getHotInformatinoByID(userCode,lang,infoId));
	}
	
	/**
	 * 获取初始好友动态
	 * @return
	 * @throws Exception
	 */
/*	@RequestMapping(value="getTrendsAndInfo",method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<Map<String,Object>> getTrendsAndInfo(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		String lang = commonService.getLanguage(request);
		
		Map<String,Object> map = mainPageService.trendsAndInfo(userCode,lang);
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
*/
	
	/**
	 * 获取推荐内容
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getRecomments",method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<Map<String,Object>> getRecomments(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		String identityFlag = commonService.getIdentityFlag(request);
		String lang = commonService.getLanguage(request);
		
		Map<String,Object> map = mainPageService.recomments(userCode,lang,identityFlag);
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	
	/**
	 * 获取更多好友动态
	 * @param param > userCode index size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getMoreTrendsOrHotInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<Map<String,Object>> getMoreTrendsOrHotInfo2(@RequestBody UserCodeWithPage param,HttpServletRequest request) throws Exception {
		String lang = commonService.getLanguage(request);
		String userCode = commonService.getUserCode(request);
		Map<String,Object> map = mainPageService.getMoreTrendsOrHotInfo(param,lang,userCode);
		
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	
	/**
	 * 发送动态内容
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="sendDynamicMessage",method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> sendDynamicMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<MultipartFile> picture = ((MultipartHttpServletRequest)request).getFiles("picture");
		if(picture.size()>9) {
			throw new CustomException(InfoMsg.ERROR_IMG_BEYOUND_LIMIT);
		}
		String trendDesc = request.getParameter("trendDesc");
		trendDesc = trendDesc.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");//过滤四字节和六字节特殊字符(如颜文字)
		String linkInfo = request.getParameter("linkInfo");
		String showFlag = request.getParameter("showFlag");
		String userCode = commonService.getUserCode(request);
		if(StringsUtils.isEmpty(showFlag)) throw new CustomException(InfoMsg.ERROR_EMPTY_REQUEST);
		trendDesc = StringsUtils.isEmpty(trendDesc) ? "" :trendDesc;
		if(trendDesc.length() >= 255) {
			return new ResultEntity<String>(InfoMsg.ERROR_LENGTH_OVER_LIMIT);
		}
		int i = mainPageService.insertDynamicMessage(picture, trendDesc,showFlag,userCode,linkInfo);
		if(i == 1) {
			return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
		}
		return new ResultEntity<String>(InfoMsg.ERROR_SAVE_FIELD);
	}
	/**
	 * 获取链接简介
	 * @param request
	 * @param response
	 * @param url
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getUrlInfo",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<String> getUrlInfo(HttpServletRequest request, HttpServletResponse response,@RequestParam String url) throws Exception {
		return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST,mainPageService.formatURL(url));
	}
	
	/**
	 * 发送动态内容 手机端
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="sendDynamicMessageMB",method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> sendDynamicMessageMB(HttpServletRequest request, @RequestBody SendTrendsResp resp) throws Exception {
		
		// 手机端发布动态， 图片上传到微信服务器上，返回id ，接口这边直接通过图片Id获取服务器上的图片
		String trendDesc = resp.getTrendDesc().replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "*");//过滤四字节和六字节特殊字符(如颜文字);
		String showFlag = resp.getShowFlag();
		String userCode = commonService.getUserCode(request);
		if(StringsUtils.isEmpty(showFlag)) throw new CustomException(InfoMsg.ERROR_EMPTY_REQUEST);
		trendDesc = StringsUtils.isEmpty(trendDesc) ? "" :trendDesc;
		int i = mainPageService.sendDynamicMessageMB(resp.getPicture(), trendDesc,showFlag,userCode,resp.getLinkInfo());
		if(i == 1) {
			return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
		}
		return new ResultEntity<String>(InfoMsg.ERROR_SAVE_FIELD);
	}
	
	/**
	 * 评论 动态
	 * 参数: 动态信息id, 发布者userCodeE, 评论内容, 评论者用户姓名、评论者职称、评论者头像路径
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="commentTrend",method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> commentTrend(@RequestBody TrendCommentInfo info,HttpServletRequest request) throws Exception {
		String lang = commonService.getLanguage(request);
		String userCode = commonService.getUserCode(request);// 评论者userCode
		info.setCommentatorCode(userCode);
		String comment = mainPageService.commentTrend(info,lang);
		return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST,comment);
	}
	
	
	/**
	 * 动态点赞 (保存或取消)
	 * 参数: 动态信息id, 用户CODE, 用户姓名、职称、头像路径
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="thumbUpTrend",method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<MdeTrends> thumbUpTrend(@RequestBody TrendThumbUpInfo info,HttpServletRequest request) throws Exception {
		String lang = commonService.getLanguage(request);
		String userCode = commonService.getUserCode(request);
		info.setCommentatorCode(userCode);
		mainPageService.thumbUpTrend(info,lang);
		return new ResultEntity<MdeTrends>(InfoMsg.SUCCESS_REQUEST,trendsService.getTrendsById(info.getTrendId(), lang));
	}
	/**
	 * 动态分享
	 * 参数: 动态信息trendId, 动态描述 trendDesc, 好友可见标记showFlag
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="shareTrend",method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> shareTrend(@RequestBody Map<String, Object> param,HttpServletRequest request) throws Exception {
		String lang = commonService.getLanguage(request);
		String userCode = commonService.getUserCode(request);
		param.put("currentUserCode", userCode);
		param.put("lang", lang);
		int i = mainPageService.shareTrend(param);
		if(i == 1) {
			return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
		}
		return new ResultEntity<String>(InfoMsg.ERROR_SAVE_FIELD);
	}
	
	/**
	 * 添加好友
	 * @param user  userCode1(申请人) userCode2(被申请人)
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="makeFriends",method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> makeFriends(@RequestBody FriendRelationReq user) throws Exception {
		if(user.getUserCode1().equals(user.getUserCode2())) {
			return new ResultEntity<String>(InfoMsg.ERROR_MAKE_FRIEND_MYSELF);
		}
		int i = mainPageService.makeFriends(user.getUserCode1(),user.getUserCode2());
		if(i == 1) {
			return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
		}
		return new ResultEntity<String>(InfoMsg.ERROR_SAVE_FIELD);
	}
	
	
	/**
	 * 获取用户通知数量
	 * @param userCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getNoticeCount",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<Map<String, Object>> getNoticeCount(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		return new ResultEntity<Map<String, Object>>(InfoMsg.SUCCESS_REQUEST,mainPageService.getNoticeCount(userCode));
	}
	
	/**
	 * 获取用户通知信息和数量
	 * @param  userCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getNoticeInfo",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<Map<String,Object>> getNoticeInfo(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		Map<String,Object> map = mainPageService.getNoticeInfo(userCode);
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	

	
	/**
	 * 获取动态详情
	 * @param request
	 * @param trendId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="getTrendsDetail",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<MdeTrends>> getTrendsDetail(HttpServletRequest request, @RequestParam(required=false) Long trendId) throws Exception {
		String userCode = commonService.getUserCode(request);
		String lang = commonService.getLanguage(request);
		return new ResultEntity<List<MdeTrends>>(InfoMsg.SUCCESS_REQUEST,mainPageService.getTrendsDetail(trendId,userCode,lang));
	}
	/**
	 * 分型动态到领英
	 * @param request
	 * @param code 领英返回的code
	 * @param trendId 被分享的动态id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="shareTrendToLinkedIn",method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<String> shareTrendToLinkedIn(
			HttpServletRequest request, 
			@RequestParam String code,
			@RequestParam String trendId) 
					throws Exception {
		String userCode = commonService.getUserCode(request);
		String lang = commonService.getLanguage(request);
		return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST,mainPageService.shareTrendToLinkedIn(trendId,userCode,code,lang));
	}
	
}
