package com.persagy.adm.diagram.core.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.adm.diagram.core.model.Diagram;
import com.persagy.adm.diagram.core.model.Line;
import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
import com.persagy.adm.diagram.core.model.template.MainPipe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 自定义ObjectMapper，为jackson定义不同场景下的处理逻辑
 *
 * @author zhaoyk
 */
@Component
public class MyObjectMapper {

	private final ObjectMapper objectMapper;

	private ObjectMapper mapper4Edit;

	private ObjectMapper mapper4View;

	@Autowired
	public MyObjectMapper(ObjectMapper objectMapper) {
		this.objectMapper = getCommonMapper(objectMapper);

		this.mapper4Edit = buildMapper4Edit();
		this.mapper4View = buildMapper4View();
	}

	public ObjectMapper mapper4Edit() {
		return mapper4Edit;
	}

	public ObjectMapper mapper4View() {
		return mapper4View;
	}

	private ObjectMapper getCommonMapper(ObjectMapper mapper) {
		//objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
		mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
		mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
		mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

		return mapper;
	}

	private ObjectMapper buildMapper4Edit() {
		ObjectMapper mapper = getCommonMapper(new ObjectMapper());
		mapper.addMixIn(Diagram.class, Diagram4Edit.class);
		mapper.addMixIn(DiagramTemplate.class, Template4Edit.class);

		return mapper;
	}

	private ObjectMapper buildMapper4View() {
		ObjectMapper mapper = getCommonMapper(new ObjectMapper());
		mapper.addMixIn(Diagram.class, Diagram4View.class);
		mapper.addMixIn(DiagramTemplate.class, Template4View.class);
		mapper.addMixIn(MainPipe.class, MainPipe4View.class);
		mapper.addMixIn(Line.class, Line4View.class);

		return mapper;
	}


	/** 编辑态数据结构 */
	@JsonIgnoreProperties({"nodes"})
	public interface Diagram4Edit {
	}

	@JsonIgnoreProperties({})
	public interface Template4Edit {
	}


	/** 显示态数据结构 */
	@JsonIgnoreProperties({})
	public interface Diagram4View {
	}

	@JsonIgnoreProperties({"diagramType", "id", "frame", "scatteredContainers"})
	public interface Template4View {
	}

	@JsonIgnoreProperties({"passbyRels", "connectEquips", "equipmentTypes", "dataFilter", "path", "relatedContainers"})
	public interface MainPipe4View {
	}

	@JsonIgnoreProperties({"passingPoints"})
	public interface Line4View {
	}

}
