package com.wechat.service;

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

import javax.annotation.Resource;

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

import com.wechat.dao.MdeTrendsDao;
import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.base.interceptor.Page;
import com.wechat.model.dbEntity.MdeInformation;
import com.wechat.model.dbEntity.MdeTrends;
import com.wechat.model.dto.TrendsDto;

@Service
public class MdeTrendsService {

	@Resource
	MdeTrendsDao trendsDao;
	
	@Resource
	CommonService commonService;
	
	@Value("#{configProperties['trends.friendTrends.limitSize']}")
	private int friendsTrendsLimitSize;
	@Value("#{configProperties['trends.friendTrendsWithLimit.limitDay']}")
	private int limitDay;
	
	/**
	 * 获取好友动态
	 * @param lang 
	 * @return
	 */
	public Page<TrendsDto> getFriendTrendsForMainpage(Page<TrendsDto> page, String userCode, String lang){

		Page<TrendsDto> pageResult = trendsDao.getFriendTrends(page,userCode);
		List<TrendsDto> list = pageResult.getResultList();
		//获取广告
		List<MdeInformation> infoList = new ArrayList<MdeInformation>();
		
		// 将广告植入动态中
		if(list.size()>=3) {
			infoList = commonService.getInformationsOfAd("", page.getCurrentPage());
			if(infoList.size() >= 1) {
				list.add(2, changeAdToTrend(infoList.get(0)));
			}
		}else {
			List<TrendsDto> newTrendList = new ArrayList<TrendsDto>();
			infoList = commonService.getAllInformationsOfAd();
			for(MdeInformation info : infoList)
			{
				newTrendList.add(changeAdToTrend(info));
			}
			list.addAll(list.size(), newTrendList);
		}
		
		return pageResult;
	}
	

	/**
	 * 获取好友动态
	 * @param lang 
	 * @return
	 */
	public PagingResult<TrendsDto> getFriendTrendsForMainpage(Pagination page, String lang){
		PagingResult<TrendsDto> pageResult = trendsDao.getFriendTrends(page);
		List<TrendsDto> list = pageResult.getResultList();
		//获取广告
		List<MdeInformation> infoList = new ArrayList<MdeInformation>();
		
		// 将广告植入动态中
		if(list.size()>=3) {
			infoList = commonService.getInformationsOfAd("", page.getPage());
			if(infoList.size() >= 1) {
				list.add(2, changeAdToTrend(infoList.get(0)));
			}
		}else {
			List<TrendsDto> newTrendList = new ArrayList<TrendsDto>();
			infoList = commonService.getAllInformationsOfAd();
			for(MdeInformation info : infoList)
			{
				newTrendList.add(changeAdToTrend(info));
			}
			list.addAll(list.size(), newTrendList);
		}
		
		pageResult.setResultList(list);
		return pageResult;
	}
	
	public TrendsDto changeAdToTrend(MdeInformation info) {
		TrendsDto trend =  new TrendsDto();
		trend.setAdFlag(true);
		trend.setUserCode(info.getUserCode());
		trend.setUserHeadPortrait(info.getUserHeadPortrait());
		trend.setUserName(info.getName());
		trend.setForeignName(info.getForeignName());
		trend.setTrendDesc(info.getInfoDesc());
		trend.setTrendPicurl(info.getInfoPicurl());
		//0.跳转链接 1 图文跳转
		if(info.getIsHref().equalsIgnoreCase("0")) {
			trend.setAdHref(info.getInfoUrl());
		}else {
			trend.setAdContent(info.getInfoId()+"");
		}
		//其他属性设置
		trend.setCommentCount(0);
		trend.setLikeCount(0);
		trend.setOriginTrendId(0l);
		trend.setOriginUserCode("0");
		return trend;
	}
	/**
	 * 获取好友动态(好友动态,添加的好友信息等) 固定条数
	 * @return
	 */
	public List<MdeTrends> getFriendTrendsWithLimit(String userCode,String lang){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("userCode", userCode);
		param.put("lang", lang);
		param.put("limitDay", limitDay);
		param.put("limitSize", friendsTrendsLimitSize);
		return commonService.addDomainUrl2TrendsImg(trendsDao.getFriendTrendsWithLimit(param),lang);
	}

	/**
	 * 新增动态
	 * @param statementName
	 * @param trends
	 * @return
	 */
	public int insert(MdeTrends trends) {
		return trendsDao.insert("MdeTrendsMapper.insert", trends);
	}
	
	/**
	 * 保存动态评论
	 * @param trendId
	 * @param trendCommentInfoStr
	 * @return
	 */
	public int updateCommentWho(long trendId, String trendCommentInfoStr) {
		MdeTrends trend = new MdeTrends();
		trend.setTrendId(new Long(trendId));
		trend.setCommentWho(trendCommentInfoStr);
		trend.setCommentCount(1);
		return trendsDao.update("MdeTrendsMapper.update", trend);
	}
	/**
	 * 动态 点赞
	 * @param trendId
	 * @param trendThuumbUpInfoStr
	 * @return
	 */
	public int updateLikeWho(MdeTrends trend) {
		return trendsDao.update("MdeTrendsMapper.update", trend);
	}
	/**
	 * 分享动态
	 * @param trends_
	 * @return
	 */
	public int insertSharedTrends(MdeTrends trends) {
		return trendsDao.insertSharedTrends(trends);
	}
	public MdeTrends getTrendsById(Long trendId,String lang) {
		return commonService.addDomainUrl2TrendsImg(trendsDao.getInfoById(trendId),lang);
	}
	
	public List<MdeTrends> getTrendsByIds(List<Long> trendIds, String lang) {
		// TODO Auto-generated method stub
		return commonService.addDomainUrl2TrendsImg(trendsDao.getTrendsByIds(trendIds),lang);
	}
	
}
