package org.onetwo.dbm.ui.json;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Optional;

import org.onetwo.common.spring.Springs;
import org.onetwo.common.utils.StringUtils;
import org.onetwo.dbm.ui.core.DefaultUISelectDataProviderService;
import org.onetwo.dbm.ui.meta.DUIEntityMeta;
import org.onetwo.dbm.ui.meta.DUIFieldMeta;
import org.onetwo.dbm.ui.meta.DUIFieldMeta.DUISelectMeta;
import org.onetwo.dbm.ui.spi.DUIMetaManager;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class DUISelectFieldJsonSerializer extends JsonSerializer<Object> {
	
    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
    	if (value==null) {
			return ;
		}
    	
    	if (value instanceof Boolean) {
    		jgen.writeBoolean((Boolean)value);
    	} else if (value instanceof BigDecimal) {
    		jgen.writeNumber((BigDecimal)value);
    	} else if (value instanceof Double) {
    		jgen.writeNumber((Double)value);
    	} else if (value instanceof Float) {
    		jgen.writeNumber((Float)value);
    	} else if (value instanceof Long) {
    		jgen.writeNumber((Long)value);
    	} else if (value instanceof Integer) {
    		jgen.writeNumber((Integer)value);
    	} else {
    		jgen.writeString(value.toString());
    	}
		
		Object object = jgen.getOutputContext().getCurrentValue();
		String fieldName = jgen.getOutputContext().getCurrentName();
		if (object==null || fieldName==null) {
			return ;
		}
		
		Optional<DUIEntityMeta> meta = getDUIMetaManager().find(object.getClass());
		if (!meta.isPresent()) {
			return ;
		} 
		
		DUIFieldMeta field = meta.get().getField(fieldName);
		if (StringUtils.isNotBlank(field.getListField()) && field.getSelect()!=null) {
			DUISelectMeta uiselect = field.getSelect();
			Object label = getUISelectDataProviderService().getListValue(uiselect, value);
			jgen.writeStringField(field.getListField(), label==null?"":label.toString());
		}
    }

	protected DUIMetaManager getDUIMetaManager() {
		return Springs.getInstance().getBean(DUIMetaManager.class);
	}
	
	protected DefaultUISelectDataProviderService getUISelectDataProviderService() {
		return Springs.getInstance().getBean(DefaultUISelectDataProviderService.class);
	}

}

@@ -58,37 +58,8 @@
 on;%0A
-import java.math.BigDecimal;%0A
 impo
@@ -1033,26 +1033,22 @@
 tanceof 
-BigDecimal
+Double
 ) %7B%0A    
@@ -1071,18 +1071,14 @@
 er((
-BigDecimal
+Double
 )val
@@ -1107,38 +1107,39 @@
 alue instanceof 
-Double
+Integer
 ) %7B%0A    %09%09jgen.w
@@ -1142,38 +1142,39 @@
 en.writeNumber((
-Double
+Integer
 )value);%0A    %09%7D 
@@ -1238,38 +1238,44 @@
 ((Float)value);%0A
-    %09%7D
+%09%09%7D /*%0A%09%09%09 *
  else if (value 
@@ -1292,23 +1292,17 @@
  Long) %7B
-%0A    %09%09
+ 
 jgen.wri
@@ -1315,38 +1315,33 @@
 er((Long)value);
-%0A    %09
+ 
 %7D else if (value
@@ -1325,32 +1325,38 @@
 alue); %7D else if
+%0A%09%09%09 *
  (value instance
@@ -1358,33 +1358,30 @@
 tanceof 
-Integer) %7B%0A    %09%09
+BigDecimal) %7B 
 jgen.wri
@@ -1394,37 +1394,90 @@
 er((
-Integer)value);%0A    %09%7D else %7B
+BigDecimal)value); %7D%0A%09%09%09 */ %0A    %09else %7B%0A    %09%09// long %E7%AD%89%E7%B1%BB%E5%9E%8B%E7%9A%84%E7%B2%BE%E5%BA%A6%E5%9C%A8%E5%89%8D%E7%AB%AFjs%E4%BC%9A%E4%B8%A2%E5%A4%B1%EF%BC%8C%E7%BB%9F%E4%B8%80%E5%BA%8F%E5%88%97%E5%8C%96%E4%B8%BAstring
 %0A   

fix dui serialize