package com.jasamedika.medifirst2000.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.entities.GenericView;
import com.jasamedika.medifirst2000.vo.GenericViewVO;

/**
 * Converter class between GenericView and GenericViewVO
 * 
 * @author Askur
 */
@Component
public class GenericViewConverter implements BaseVoConverter<GenericViewVO, GenericView> {
	

	public GenericView transferVOToModel(GenericViewVO vo, GenericView model) {
		if (null == model)
			model = new GenericView();
		try {
			String[] fieldsToInclude = null;
			Map<String, Object> serialized = vo.serialize(fieldsToInclude,vo.getClass().getName());
			Gson gson = new Gson();
	        String json = gson.toJson(serialized);
	        model = gson.fromJson(json, GenericView.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;

	}

	public List<GenericViewVO> transferListOfModelToListOfVO(List<GenericView> models,
			List<GenericViewVO> vos) {
		if (null == vos)
			vos = new ArrayList<GenericViewVO>();

		if (null == models)
			return vos;

		for (GenericView GenericView : models) {
			GenericViewVO vo = new GenericViewVO();
			vo = transferModelToVO(GenericView, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public GenericViewVO transferModelToVO(GenericView model, GenericViewVO vo) {

		if (null == vo)
			vo = new GenericViewVO();
		try {
			String[] fieldsToInclude = null;
			Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
			Gson gson = new Gson();
	        String json = gson.toJson(serialized);
	        vo = gson.fromJson(json, GenericViewVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	
	}

	
}
