package com.kingdee.eas.custom.utils;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.message.common.KDMessageAttachment;

import com.kingdee.bos.util.BOSUuid;

import com.kingdee.eas.base.permission.IUser;
import com.kingdee.eas.base.permission.UserFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.person.IPerson;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.mobile.CellphoneNumberTypeEnum;
import com.kingdee.eas.mobile.EmailAddressInfo;
import com.kingdee.eas.mobile.EmailSendMessageInfo;
import com.kingdee.eas.mobile.MimeTypeEnum;
import com.kingdee.eas.mobile.MobileException;
import com.kingdee.eas.mobile.MobileMessageAddressInfo;
import com.kingdee.eas.mobile.SendMobileMessageInfo;
import com.kingdee.eas.mobile.dao.DefaultEmailMessageDAO;
import com.kingdee.eas.mobile.dao.DefaultSendMoMsgDao;
import com.kingdee.eas.mobile.framework.ServerCenterFactory;
import com.kingdee.eas.mobile.msg.MobileConnectException;
import com.kingdee.eas.mobile.msg.util.MsgUtil;
import com.kingdee.eas.mobile.util.AppMsgUtil;
import com.kingdee.eas.mobile.util.MobileUtil;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.util.StringUtils;
import java.util.List;



// 发送邮件的重写类
public class SendUtils extends MsgUtil {
	
	public static boolean msgSend(Context ctx, String title, int priority, boolean revertible, String content,
			String personId, int msgType, KDMessageAttachment[] attachment, String assignID)
			throws EASBizException, BOSException {
		return msgSend(ctx, title, priority, revertible, content, personId, msgType, attachment, assignID,
				(MimeTypeEnum) null);
	}
	
	public static boolean msgSend(Context ctx, String title, int priority, boolean revertible, String content,
			String personId, int msgType, KDMessageAttachment[] attachment, String assignID, MimeTypeEnum mimeType)
			throws EASBizException, BOSException {
		PersonInfo person = null;
		String address = null;
		if (!StringUtils.isEmpty(personId)) {
			IPerson iPerson = PersonFactory.getLocalInstance(ctx);
			IObjectPK pk = null;
			pk = new ObjectUuidPK(personId);
			person = iPerson.getPersonInfo(pk);
			boolean succeed = false;
			switch (msgType) {
				case 1 :
					address = person.getCell();
					if (StringUtils.isEmpty(address)) {
						throw new MobileConnectException(MobileConnectException.USER_NOT_LOIGN);
					}

					succeed = sendMobileMsg(ctx, title, priority, revertible, content, address, assignID);
					return succeed;
				case 2 :
					address = person.getEmail();
					if (StringUtils.isEmpty(address)) {
						//throw new MobileConnectException(MobileConnectException.USER_NOT_LOIGN_EMAIL);
						return succeed;
					}

					succeed = sendMail(ctx, title, priority, revertible, content, address, attachment, assignID,
							mimeType);
					ServerCenterFactory.getServerCenter(ctx);
					return succeed;
				case 3 :
					address = person.getRtx();
					if (StringUtils.isEmpty(address)) {
						throw new MobileConnectException(MobileConnectException.USER_NOT_LOIGN_RTX);
					}

					succeed = sendRTXMsg(ctx, title, priority, revertible, content, address);
					ServerCenterFactory.getServerCenter(ctx);
					return succeed;
				default :
					throw new MobileException(MobileException.MSGTYPE_ERROR);
			}
		} else {
			throw new MobileConnectException(MobileConnectException.USER_NOT_LOIGN);
		}
	}
	private static boolean sendMail(Context ctx, String title, int priority, boolean revertible, String content,
			String address, KDMessageAttachment[] attachment, String assignID) throws EASBizException, BOSException {
		return sendMail(ctx, title, priority, revertible, content, address, attachment, assignID, (MimeTypeEnum) null);
	}
	
	private static boolean sendMail(Context ctx, String title, int priority, boolean revertible, String content,
			String address, KDMessageAttachment[] attachment, String assignID, MimeTypeEnum mimeType)
			throws EASBizException, BOSException {
		EmailSendMessageInfo emailSendMsgInfo = new EmailSendMessageInfo();
		if (mimeType != null) {
			emailSendMsgInfo.setMimeType(mimeType);
		}

		emailSendMsgInfo.setPriority(AppMsgUtil.getPriorityEnum(priority));
		emailSendMsgInfo.setRevertible(revertible);
		emailSendMsgInfo.setAssignID(assignID);
		emailSendMsgInfo.setContent(content);
		emailSendMsgInfo.setTitle(title);
		EmailAddressInfo addressInfo = new EmailAddressInfo();
		UserInfo userInfo = ContextUtil.getCurrentUserInfo(ctx);
		if (userInfo != null) {
			addressInfo.setSendBizUser(userInfo);
			PersonInfo sendPersonInfo = AppMsgUtil.getPersonByUser(ctx, userInfo);
			if (sendPersonInfo != null && !StringUtils.isEmpty(sendPersonInfo.getEmail())) {
				addressInfo.setFrom(sendPersonInfo.getEmail());
			}

			emailSendMsgInfo.setTitle(title);
		} else {
			emailSendMsgInfo.setTitle(title);
		}

		addressInfo.setTo(address);
		emailSendMsgInfo.setAddress(addressInfo);
		boolean sendSuccess = false;
		IObjectPK pk = DefaultEmailMessageDAO.addNew(ctx, emailSendMsgInfo);
		if (pk != null) {
			sendSuccess = true;
		} else {
			//logger.info("add email: pk is null.");
		}

		return sendSuccess;
	}
	
	private static boolean sendMobileMsg(Context ctx, String title, int priority, boolean revertible, String content,
			String receiveMobileNumber, String assignID) throws EASBizException, BOSException {
		SendMobileMessageInfo sendMoMsgInfo = new SendMobileMessageInfo();
		sendMoMsgInfo.setSendHandcraft(true);
		sendMoMsgInfo.setRevertible(revertible);
		sendMoMsgInfo.setPriority(AppMsgUtil.getPriorityEnum(priority));
		sendMoMsgInfo.setContent(title);
		sendMoMsgInfo.setTitle(content);
		sendMoMsgInfo.setAssignID(assignID);
		MobileMessageAddressInfo addressInfo = new MobileMessageAddressInfo();
		UserInfo userInfo = ContextUtil.getCurrentUserInfo(ctx);
		if (userInfo != null) {
			addressInfo.setSendBizUser(userInfo);
			PersonInfo sendPersonInfo = AppMsgUtil.getPersonByUser(ctx, userInfo);
			if (sendPersonInfo != null && !StringUtils.isEmpty(sendPersonInfo.getCell())) {
				addressInfo.setSendMobileNumber(sendPersonInfo.getCell());
				addressInfo.setSendCellNumberType(
						CellphoneNumberTypeEnum.getEnum(MobileUtil.getCellphoneNumberType(sendPersonInfo.getCell())));
			}

			sendMoMsgInfo.setTitle(title);
		} else {
			sendMoMsgInfo.setTitle(content);
		}

		addressInfo.setReceiveMobileNumber(receiveMobileNumber);
		addressInfo.setReceiveCellNumberType(
				CellphoneNumberTypeEnum.getEnum(MobileUtil.getCellphoneNumberType(receiveMobileNumber)));
		UserInfo receiveUserInfo = null;
		String receiveUserId = AppMsgUtil.getUserIdByMobileNumber(ctx, receiveMobileNumber);
		IUser iUser = UserFactory.getLocalInstance(ctx);
		if (!StringUtils.isEmpty(receiveUserId)) {
			try {
				receiveUserInfo = iUser.getUserInfo(new ObjectUuidPK(receiveUserId));
			} catch (Exception var15) {
				receiveUserInfo = null;
			}
		}

		addressInfo.setReceiveBizUser(receiveUserInfo);
		sendMoMsgInfo.setAddress(addressInfo);
		IObjectPK pk = null;
		boolean sendSuccess = false;
		pk = DefaultSendMoMsgDao.addNew(ctx, sendMoMsgInfo);
		if (pk != null) {
			sendSuccess = true;
		}

		return sendSuccess;
	}
	
	private static boolean sendRTXMsg(Context ctx, String title, int priority, boolean revertible, String content,
			String address) throws EASBizException, BOSException {
		return false;
	}

	public static int msgGroupSend(Context ctx, String title, int priority, boolean revertible, String content,
			List userList, int msgType, KDMessageAttachment[] attachment) throws EASBizException, BOSException {
		if (msgType != 2 && msgType != 1) {
			throw new MobileException(MobileException.MSGTYPE_ERROR);
		} else {
			IUser iUser = UserFactory.getLocalInstance(ctx);
			IObjectPK pk = null;
			BOSUuid uuid = null;
			UserInfo receiveUser = null;
			PersonInfo receivePerson = null;
			String address = null;
			int userNum = userList.size();
			int sentNum = 0;
			boolean sendSucceed = false;

			for (int i = 0; i < userNum; ++i) {
				try {
					if (userList.get(i) != null && !StringUtils.isEmpty(userList.get(i).toString())) {
						uuid = BOSUuid.read(userList.get(i).toString().trim());
						pk = new ObjectUuidPK(uuid);
						receiveUser = iUser.getUserInfo(pk);
						if (receiveUser.getPerson() != null) {
							receivePerson = AppMsgUtil.getPersonByUser(ctx, receiveUser);
							if (receivePerson != null) {
								switch (msgType) {
									case 2 :
										address = receivePerson.getEmail();
										if (StringUtils.isEmpty(address)) {
											break;
										}

										sendSucceed = sendMail(ctx, title, priority, revertible, content, address,
												attachment, (String) null);
									case 1 :
										address = receivePerson.getCell();
										if (StringUtils.isEmpty(address)) {
											break;
										}

//										sendSucceed = newMsgMobile(ctx, revertible, title, content, priority,
//												receiveUser, address);
									default :
										if (sendSucceed) {
											++sentNum;
											sendSucceed = false;
										}
								}
							}
						}
					}
				} catch (Exception var19) {
					//logger.info("Exception in MsgUtil.msgGroupSend方法" + var19.getMessage());
				}
			}

			return sentNum;
		}
		
	}
}
