package com.leanwo.activiti.dto.base;

import java.io.Serializable;
import java.math.BigDecimal;

import com.leanwo.activiti.util.FieldUtil;


/**
 * 字段值
 * 
 * @author ZhiJie
 *
 */
public class FieldValue implements Serializable {
	/**
	 * 如果是选择框，代表主键
	 */
	private Long id;
	
	/**
	 * 如果是多选框，代表主键
	 */
	private Long[] ids;
	
	/**
	 * 界面上显示的值
	 */
	private String[] displayValue;
	
	/**
	 * 是否是选择框
	 */
	private FieldType fieldType;
	
	public FieldValue(){
		
	}
	
	/**
	 * String, Date, Long, Integer, Double, Float
	 * @param displayValue
	 */
	public FieldValue(String[] displayValue) {
		fieldType = FieldType.String;
		this.displayValue = displayValue;
	}

	/**
	 * ? extends BaseClientEntity, Enum
	 * @param isSearchField 是否是搜索框、下拉框。
	 * @param displayValue
	 * @param id
	 */
	public FieldValue(String[] displayValue, Long id) {
		fieldType = FieldType.Key;
		this.displayValue = displayValue;
		this.id = id;
	}
	
	public FieldValue(String[] displayValue, Long[] ids, FieldType fieldType) {
		this.fieldType = FieldType.ManyToManyKey;
		this.displayValue = displayValue;
		this.ids = ids;
	}
	

	public String[] getDisplayValue() {
		return displayValue;
	}

	public void setDisplayValue(String[] displayValue) {
		this.displayValue = displayValue;
	}

	public Long getId() {
		return id;
	}

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

	public FieldType getFieldType() {
		return fieldType;
	}

	public void setFieldType(FieldType fieldType) {
		this.fieldType = fieldType;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	/**
	 * 判断是否为null
	 * @return
	 */
	public boolean isNull(){
		if(FieldType.Key.equals(fieldType)){
			return id == null;
		}else if(FieldType.ManyToManyKey.equals(fieldType)){
			if( ids == null || ids.length == 0){
				return true;
			}else{
				for(int i = 0; i < ids.length; i ++){
					if(ids[i] != null && ids[i] > 0){
						return false;
					}
				}
				return true;
			}
		} else{
			if(displayValue == null || displayValue.length == 0){
				return true;
			}else{				
				for(int i = 0; i < displayValue.length; i ++){
					if(displayValue[i] != null && displayValue[i].length() > 0){
						return false;
					}
				}
				return true;				
			}
		}
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(obj instanceof FieldValue){
			FieldValue fieldValue = (FieldValue)obj;
			if(fieldType.equals(fieldValue.getFieldType()) ==  false){
				return false;
			}else if(fieldType.equals(FieldType.Key)){
				return (id == null || fieldValue.getId() == null) ? false : id.equals(fieldValue.getId());
			}else if(FieldType.ManyToManyKey.equals(fieldType)){
				if((ids == null || ids.length == 0) && (fieldValue.getIds() == null || fieldValue.getIds().length == 0)){
					return true;
				}else if((ids == null || ids.length == 0) && !(fieldValue.getIds() == null || fieldValue.getIds().length == 0)){
					return false;
				}else if(!(ids == null || ids.length == 0) && (fieldValue.getIds() == null || fieldValue.getIds().length == 0)){
					return false;
				}else{
					if(ids.length != fieldValue.getIds().length){
						return false;
					}else{
						for(Long id1 : ids){
							boolean exist = false;
							for(Long id2 : fieldValue.getIds()){
								if(id1 != null && id2 != null && id1.equals(id2)){
									exist = true;
									break;
								}
							}
							if(exist == false){
								return false;
							}
						}
						return true;
					}
				}
			}else{
				if(displayValue == null && fieldValue.getDisplayValue() != null){
					return false;
				}else if(displayValue != null && fieldValue.getDisplayValue() == null){
					return false;
				}else if(displayValue == null && fieldValue.getDisplayValue() == null){
					return true;
				}else if(displayValue.length != fieldValue.getDisplayValue().length){
					return false;
				}else{
					String value1 = FieldUtil.getDisplayName(displayValue);
					String value2 = FieldUtil.getDisplayName(fieldValue.getDisplayValue());
					return value1.equals(value2);
				}
			}
		}else{
			return false;
		}
	}
	
	/**
	 * 克隆对象
	 */
	public FieldValue clone(){
		if(FieldType.Key.equals(fieldType)){
			return new FieldValue(copyDisplayValue(), this.id);
		}else if(FieldType.ManyToManyKey.equals(fieldType)){
			if(ids != null && ids.length > 0){
				Long[] newIds = new Long[ids.length];
				for(int i = 0; i < ids.length; i ++){
					newIds[i] = ids[i];
				}
				return new FieldValue(copyDisplayValue(), newIds, FieldType.ManyToManyKey);
			}
			return new FieldValue(copyDisplayValue(), new Long[0], FieldType.ManyToManyKey);
		} else{
			return new FieldValue(copyDisplayValue());
		}
	}
	
	/**
	 * 复制显示值
	 * @return
	 */
	protected String[] copyDisplayValue(){
		if(displayValue == null){
			return null;
		}else{
			String[] newDisplayValue = new String[this.displayValue.length];
			for(int i = 0; i < this.displayValue.length; i++){
				newDisplayValue[i] = this.displayValue[i];
			}
			return newDisplayValue;
		}
	}
	
	
	@Override
	public String toString() {
		StringBuilder stringBuilder = null;
		if(displayValue != null) {
			stringBuilder = new StringBuilder();
			for(String value : displayValue) {
				stringBuilder.append(value).append(",");
			}
		}
		
		if(fieldType.equals(FieldType.Key)){
			return id + "_" + (stringBuilder == null ? "" : stringBuilder.toString());
		}else if(fieldType.equals(FieldType.ManyToManyKey)){
			if(ids == null || ids.length == 0){
				return null;
			}
			StringBuilder stringBuilder1 = new StringBuilder();
			
			for(int i = 0; i < ids.length; i++){
				stringBuilder1.append("(id=").append(ids[i]);
				if(displayValue.length > i){
					stringBuilder1.append(",displayValue=").append(displayValue[i]);
				}
				stringBuilder1.append(")");
			}
			return stringBuilder1.toString();
		}else{
			return FieldUtil.getDisplayName(displayValue);
		}
	}
	
	/**
	 * 清空数值
	 */
	public void clear(){
		setId(null);
		setDisplayValue(new String[]{});
		setIds(new Long[]{});
	}
	
	public Double getDispalyNumberValue(int index) {
		Double double1 = Double.parseDouble(this.displayValue[index]);
		return double1;
	}
	
	public BigDecimal getDispalyBigDecimalValue(int index) {
		BigDecimal bigDecimal = new BigDecimal(this.displayValue[index]);
		return bigDecimal;
	}
	

	public String getDisplayValue0() {
		if(displayValue == null) {
			return null;
		}
		if(displayValue.length == 0) {
			return null;
		}
		return this.displayValue[0];
	}
	
	public Double getDisplayValue0Double() {
		String value = this.getDisplayValue0();
		if(value == null) {
			return null;
		}
		return new Double(value);
	}
	
	
	/**
	 * id包含在longs中的
	 * @param longs
	 * @return
	 */
	public Boolean idBelongTo(Long... ids) {
		if(this.id == null) {
			return false;
		}
		Boolean isflag = false;
		if(ids != null && ids.length > 0) {
			for(Long id : ids) {
				if(id.equals(this.id)) {
					isflag = true;
					break;
				}
			}
		}
		return isflag;
	}
	
	/**
	 * id不包含在longs中的
	 * @param longs
	 * @return
	 */
	public Boolean idNotBelongTo(Long... ids) {
		if(this.id == null) {
			return false;
		}
		Boolean isflag = true;
		if(ids != null && ids.length > 0) {
			for(Long id : ids) {
				if(id.equals(this.id)) {
					isflag = false;
					break;
				}
			}
		}
		return isflag;
	}
	
	

	/**
	 * id包含在longs中的
	 * @param longs
	 * @return
	 */
	public Boolean displayValueBelongTo(int displayValueIndex, String... texts) {
		if(this.displayValue == null || displayValueIndex >= this.displayValue.length || this.displayValue[displayValueIndex] == null) {
			return false;
		}
		Boolean isflag = false;
		if(texts != null && texts.length > 0) {
			for(String text : texts) {
				if(text.equals(this.displayValue[displayValueIndex])) {
					isflag = true;
					break;
				}
			}
		}
		return isflag;
	}
	
	/**
	 * id不包含在longs中的
	 * @param longs
	 * @return
	 */
	public Boolean displayNotValueBelongTo(int displayValueIndex, String... texts) {
		if(this.displayValue == null || displayValueIndex >= this.displayValue.length || this.displayValue[displayValueIndex] == null) {
			return false;
		}
		Boolean isflag = true;
		if(texts != null && texts.length > 0) {
			for(String text : texts) {
				if(text.equals(this.displayValue[displayValueIndex])) {
					isflag = false;
					break;
				}
			}
		}
		return isflag;
	}
}
