package com.palmnest.service.wechat.impl;

import com.palmnest.application.bean.*;
import com.palmnest.application.core.util.FileUploadUtils;
import com.palmnest.dao.wechat.ReplayContentDao;
import com.palmnest.model.estateserve.Picandtextdetailstemp;
import com.palmnest.model.estateserve.Replykeywords;
import com.palmnest.model.estateserve.Textpicvoicetemp;
import com.palmnest.service.wechat.ReplayContentManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 回复关键字模板服务
 *
 * @author yb
 * @date 2014-04-17
 */
@Slf4j
@Component("replayContentManager")
public class ReplayContentManagerImpl implements ReplayContentManager {


	@Autowired
	private ReplayContentDao replayContentDao;

	private String defaultKeywordName = "default";

	/**
	 * 根据关键字查询方法数据
	 *
	 * @param keywordName
	 * @param developid
	 * @return
	 */
	public OutMessage queryReplayContent(String keywordName, String developid) {
		try {
			List<Replykeywords> words = replayContentDao.getReplykeyworsList(developid, keywordName);
			if (!CollectionUtils.isEmpty(words)) {
				Replykeywords word = words.get(0);
				//文本、语音、图片
				if ("1".equals(word.getReplyContentType())) {
					List<Textpicvoicetemp> sctemp = replayContentDao.getTextpicvoicetempsList(word.getReplyKeywordsId());
					if (!CollectionUtils.isEmpty(sctemp)) {
						Textpicvoicetemp temp = sctemp.get(0);
						//文本
						if ("1".equals(temp.getReplyType())) {
							TextOutMessage text = new TextOutMessage();
							text.setContent(temp.getText());
							return text;
						}
						//图片
						else if ("2".equals(temp.getReplyType())) {
							ImageOutMessage image = new ImageOutMessage();
							image.setImage(new Image(temp.getReplyId()));
							return image;
						}
						//语音
						else if ("3".equals(temp.getReplyType())) {
							VoiceOutMessage voice = new VoiceOutMessage();
							voice.setVoice(new Voice(temp.getReplyId()));
							return voice;
						} else {
							//默认模板
							return getCommonDefalutModel(defaultKeywordName, developid);
						}
					} else {
						//默认模板
						return getCommonDefalutModel(defaultKeywordName, developid);
					}
				}
				//图文
				else if ("2".equals(word.getReplyContentType())) {
					List<Picandtextdetailstemp> details = replayContentDao.getPicandtextdetailstempsList(word.getReplyKeywordsId());
					if (!CollectionUtils.isEmpty(details)) {
						NewsOutMessage msg      = new NewsOutMessage();
						List<Articles> articles = new ArrayList<Articles>();
						for (int i = 0; i < details.size(); i++) {
							Articles ar = new Articles();
							ar.setTitle(details.get(i).getTitle());
							ar.setDescription(details.get(i).getDescription());
							ar.setPicUrl(FileUploadUtils.getVisitPrefix() + details.get(i).getPicUrl());
							ar.setUrl(details.get(i).getUrl());
							articles.add(ar);
						}
						msg.setArticles(articles);
						return msg;
					} else {
						//默认模板
						return getCommonDefalutModel(defaultKeywordName, developid);
					}
				} else {
					//默认模板
					return getCommonDefalutModel(defaultKeywordName, developid);
				}
			} else {
				return getCommonDefalutModel(defaultKeywordName, developid);
			}
		} catch (Exception e) {
			log.info("keywordName:" + keywordName);
			log.error("queryReplayContent occur error.{}", e, e.getMessage());
			return getCommonModel();
		}
	}

	public OutMessage getCommonDefalutModel(String keywordName, String developid) {
		try {
			List<Replykeywords> words = replayContentDao.getReplykeyworsList(developid, keywordName);
			if (!CollectionUtils.isEmpty(words)) {
				Replykeywords word = words.get(0);
				//文本、语音、图片
				if ("1".equals(word.getReplyContentType())) {
					List<Textpicvoicetemp> sctemp = replayContentDao.getTextpicvoicetempsList(word.getReplyKeywordsId());
					if (!CollectionUtils.isEmpty(sctemp)) {
						Textpicvoicetemp temp = sctemp.get(0);
						//文本
						if ("1".equals(temp.getReplyType())) {
							TextOutMessage text = new TextOutMessage();
							text.setContent(temp.getText());
							return text;
						}
						//图片
						else if ("2".equals(temp.getReplyType())) {
							ImageOutMessage image = new ImageOutMessage();
							image.setImage(new Image(temp.getReplyId()));
							return image;
						}
						//语音
						else if ("3".equals(temp.getReplyType())) {
							VoiceOutMessage voice = new VoiceOutMessage();
							voice.setVoice(new Voice(temp.getReplyId()));
							return voice;
						} else {
							return getCommonModel();
						}
					} else {
						return getCommonModel();
					}
				}
				//图文
				else if ("2".equals(word.getReplyContentType())) {
					List<Picandtextdetailstemp> details = replayContentDao.getPicandtextdetailstempsList(word.getReplyKeywordsId());
					if (!CollectionUtils.isEmpty(details)) {
						NewsOutMessage msg      = new NewsOutMessage();
						List<Articles> articles = new ArrayList<Articles>();
						for (int i = 0; i < details.size(); i++) {
							Articles ar = new Articles();
							ar.setTitle(details.get(i).getTitle());
							ar.setDescription(details.get(i).getDescription());
							ar.setPicUrl(FileUploadUtils.getVisitPrefix() + details.get(i).getPicUrl());
							ar.setUrl(details.get(i).getUrl());
							articles.add(ar);
						}
						msg.setArticles(articles);
						return msg;
					} else {
						return getCommonModel();
					}
				} else {
					return getCommonModel();
				}
			} else {
				return getCommonModel();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("keywordName:" + keywordName);
			log.error("getCommonDefalutModel occur error.{}", e, e.getMessage());
			return getCommonModel();
		}
	}

	public OutMessage getCommonModel() {
		/*TextOutMessage text=new TextOutMessage();
		text.setContent("");
		return text;*/
		return null;
	}

}
