package com.wechat.service;

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

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.beust.jcommander.internal.Maps;
import com.wechat.common.Constants;
import com.wechat.common.utils.StringsUtils;
import com.wechat.global.base.dao.Pagination;
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.responseDto.ResultEntity;

@Service
public class HumanVeinService {

	@Autowired
	CommonService commonService;

	@Autowired
	MdeUserService mdeUserService;

	@Autowired
	MdeFriendRelationService mdeFriendRelationService;

	@Resource
	private MdeTrendsService trendsService;

	@Value("#{configProperties['friends.myFriends.limitSize']}")
	private int friendsLimitSize;

	/**
	 * 人脉主页初始化
	 * 
	 * @param userCode
	 * @param size 
	 * @param identityFlag 
	 * @return
	 */
	public Map<String, Object> init(String userCode, String lang, String size, String identityFlag) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 查找好友userCode
		int pageSize=0;
		if(!StringsUtils.isEmpty(size)) {
			pageSize = Integer.valueOf(size);
		}else {
			pageSize = friendsLimitSize;
		}
		Pagination page = new Pagination(1, pageSize);
		Map<Object, Object> param = new HashMap<Object, Object>();
		param.put("userCode", userCode);
		page.setParams(param);
		page.setSize(Integer.valueOf(pageSize));
		// 好友列表
		map.put("friends", mdeUserService.getFriends(page));
		// 新的好友邀请信息
		List<MdeFriendRelation> requestForFriends = commonService.getFriendInvitation(userCode);
		map.put("requestForFriends", requestForFriends);
		// 好友动态
		map.put("trendsOfFriends", trendsService.getFriendTrendsWithLimit(userCode, lang));
		// 可能认识的人
		map.put("maybeFriends", commonService.getMaybeFriends(userCode,identityFlag,lang));
		// 广告
		map.put("advertisementList", commonService.getAdImages(Constants.PC_HUMAN_VEIN_LEFT_AD));
		return map;
	}
	
	
	/**
	 * 人脉主页初始化
	 * 
	 * @param userCode
	 * @param size 
	 * @param identityFlag 
	 * @return
	 */
	public Map<String, Object> init(String userCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 新的好友邀请信息
		List<MdeFriendRelation> requestForFriends = commonService.getFriendInvitation(userCode);
		map.put("requestForFriends", requestForFriends);
		// 广告
		map.put("advertisementList", commonService.getAdImages(Constants.PC_HUMAN_VEIN_LEFT_AD));
		return map;
	}
	
	/**
	 * 获取好友动态和好友列表
	 * 
	 * @param userCode
	 * @param lang 
	 * @return
	 */
	public Map<String, Object> getFriendsAndTrends(String userCode, String lang, String size) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 查找好友userCode
		int pageSize=0;
		if(!StringsUtils.isEmpty(size)) {
			pageSize = Integer.valueOf(size);
		}else {
			pageSize = friendsLimitSize;
		}
		Pagination page = new Pagination(1, pageSize);
		Map<Object, Object> param = new HashMap<Object, Object>();
		param.put("userCode", userCode);
		page.setParams(param);
		page.setSize(Integer.valueOf(pageSize));
		// 好友
		map.put("friends", mdeUserService.getFriends(page));
		// 好友动态
		map.put("trendsOfFriends", trendsService.getFriendTrendsWithLimit(userCode, lang));
		return map;
	}
	


	/**
	 * 获取好友列表
	 * 
	 * @param req
	 * @param page
	 * @return
	 */
	public PagingResult<MdeUser> getPagefriends(HttpServletRequest req, PageReq page) {
		Pagination pagination = new Pagination(page.getIndex(), page.getSize());
		String userCode = commonService.getUserCode(req);
		Map<Object, Object> param = new HashMap<Object, Object>();
		param.put("userCode", userCode);
		pagination.setParams(param);
		return mdeUserService.getFriends(pagination);
	}


	/**
	 * 好友搜索
	 * 
	 * @param userCode
	 * @return
	 */
	public PagingResult<MdeUser> friendSearch(Map<String, String> map, HttpServletRequest req) {
		String currentUserCode = commonService.getUserCode(req);
		String keyWord = map.get("keyWord");
		if (StringsUtils.isEmpty(keyWord)) {
			PagingResult<MdeUser> pagingResult = new PagingResult<>();
			pagingResult.setCurrentPage(Integer.valueOf(map.get("index")));
			pagingResult.setTotalSize(0);
			pagingResult.setPageSize(Integer.valueOf(map.get("size")));
			pagingResult.setResultList(new ArrayList<MdeUser>());
			pagingResult.setPageNoSize(10);
			return pagingResult;
		}
		Pagination pagination = new Pagination(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("size")));
		Map<Object, Object> maps = Maps.newHashMap();
		maps.put("keyWord", keyWord);
		maps.put("userCode", currentUserCode);
		pagination.setParams(maps);
		return mdeUserService.findByKeyWord(pagination);
		
	}

	/**
	 * 好友添加确认(0同意，1拒绝)
	 * 
	 * @param userCode
	 * @return
	 */
	public ResultEntity<String> friendsConfirm(String userCodeReq, String userCodeResp, String confirm) {
		try {
			// 查找申请记录
			MdeFriendRelation mdeFriendRelation = 
					mdeFriendRelationService.findRequestRecordByCodes(userCodeReq, userCodeResp);
			// 如果有记录
			if (mdeFriendRelation != null) {
				if (StringsUtils.isEqual(confirm, "0")) {
					// 同意
					mdeFriendRelation.setConfirmTime(new Date());
					mdeFriendRelation.setUpdateBy(userCodeResp);
					mdeFriendRelationService.update(mdeFriendRelation);
					commonService.setConfirmedFriendship(userCodeReq, userCodeResp);
				} else if (StringsUtils.isEqual(confirm, "1")) {
					// 拒绝
					mdeFriendRelationService.deleteRecords(mdeFriendRelation);
				}
				return new ResultEntity<String>(InfoMsg.SUCCESS_REQUEST);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultEntity<String>(InfoMsg.ERROR_SAVE_FIELD);
	}


}
