package com.swfarm.biz.aliexpress.bo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import com.swfarm.biz.oa.bo.User;
import com.swfarm.pub.utils.VelocityUtils;

public class AliExpressOrderMessageWorkingFolder implements Serializable {

	private Long id;

	private String name;

	private String displayName;

	private String customerServiceName;

	private String accountNumber;

	private String filterRule;

	private Integer totalMessageCount;

	private Integer newMessageCount;

	private Integer oldMessageCount;

	private Set aliExpressOrderMessages = new HashSet();

	private Set customerServiceUsers = new HashSet();

	private Long[] customerServiceUserIds;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getCustomerServiceName() {
		return customerServiceName;
	}

	public void setCustomerServiceName(String customerServiceName) {
		this.customerServiceName = customerServiceName;
	}

	public String getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	public String getFilterRule() {
		return filterRule;
	}

	public void setFilterRule(String filterRule) {
		this.filterRule = filterRule;
	}

	public Integer getTotalMessageCount() {
		if (totalMessageCount == null) {
			totalMessageCount = 0;
		}
		return totalMessageCount;
	}

	public void setTotalMessageCount(Integer totalMessageCount) {
		this.totalMessageCount = totalMessageCount;
	}

	public Integer getNewMessageCount() {
		if (newMessageCount == null) {
			newMessageCount = 0;
		}
		return newMessageCount;
	}

	public void setNewMessageCount(Integer newMessageCount) {
		this.newMessageCount = newMessageCount;
	}

	public Integer getOldMessageCount() {
		if (oldMessageCount == null) {
			oldMessageCount = 0;
		}
		return oldMessageCount;
	}

	public void setOldMessageCount(Integer oldMessageCount) {
		this.oldMessageCount = oldMessageCount;
	}

	public Set getAliExpressOrderMessages() {
		if (aliExpressOrderMessages == null) {
			aliExpressOrderMessages = new HashSet();
		}
		return aliExpressOrderMessages;
	}

	public void setAliExpressOrderMessages(Set ebayMessages) {
		this.aliExpressOrderMessages = ebayMessages;
	}

	public Set getCustomerServiceUsers() {
		if (customerServiceUsers == null) {
			customerServiceUsers = new HashSet();
		}
		return customerServiceUsers;
	}

	public List getCustomerServiceUserList() {
		List customerServiceList = new ArrayList();
		customerServiceList.addAll(this.getCustomerServiceUsers());
		Collections.sort(customerServiceList, new Comparator() {

			public int compare(Object obj1, Object obj2) {
				User customerService1 = (User) obj1;
				User customerService2 = (User) obj2;
				return customerService1.getUsername().compareTo(
						customerService2.getUsername());
			}

		});
		return customerServiceList;
	}

	public void addCustomerServiceUser(User customerServiceUser) {
		if (!this.getCustomerServiceUsers().contains(customerServiceUser)) {
			this.customerServiceUsers.add(customerServiceUser);
		}
	}

	public void removeCustomerServiceUser(User customerServiceUser) {
		if (this.getCustomerServiceUsers().contains(customerServiceUser)) {
			this.customerServiceUsers.remove(customerServiceUser);
		}
	}

	public void setCustomerServiceUsers(Set customerServiceUsers) {
		this.customerServiceUsers = customerServiceUsers;
	}

	public Long[] getCustomerServiceUserIds() {
		return customerServiceUserIds;
	}

	public void setCustomerServiceUserIds(Long[] customerServiceUserIds) {
		this.customerServiceUserIds = customerServiceUserIds;
	}

	public Boolean filter(String accountNumber, String senderName,
			String content) {
		Map dataMap = new HashMap();
		dataMap.put("accountNumber", accountNumber);
		dataMap.put("senderName", senderName);
		dataMap.put("senderNameFirstChar", "" + senderName.charAt(0));
		dataMap.put("content", content);
		VelocityContext context = VelocityUtils.createVelocityContext();
		for (Iterator iter = dataMap.entrySet().iterator(); iter.hasNext();) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			context.put(key, entry.getValue());
		}
		Template template = VelocityUtils.getTemplateFromString(filterRule);
		if (template != null) {
			String evalContent = VelocityUtils.mergeToString(context, template);
			if (StringUtils.isNotEmpty(evalContent)
					&& "true".equals(evalContent.trim())) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		System.out.println("test".charAt(0));
	}
}
