package com.jasamedika.medifirst2000.converter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.dao.custom.base.impl.FindConverterDao;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.vo.PasienVO;

/**
 * Converter class between Pasien and PasienVO
 * 
 * @author Roberto
 */
@Component
public class PasienConverter extends FindConverterDao implements
		BaseVoConverter<PasienVO, Pasien> {

	public Pasien transferVOToModel(PasienVO vo, Pasien model) {
		if (null == model)
			model = new Pasien();

		// fix this
//		ExtendedSpringBeanUtil.copySpecificProperties(vo, model, new String[] {
//				"id", "nama", "alamat", }, new String[] { "id", "nama",
//				"alamat", });

		// model.setGroupPasien(vo.getGroupPasien());
		try {
		
	//String[] fieldsToInclude = null;
			List<String> fieldsToInclude = new ArrayList<String>();
			for(Field field : model.GetFields(model.getClass()))
			{
				String str =field.getName();
				fieldsToInclude.add(str);
			}
			Map<String, Object> serialized = vo.serialize(fieldsToInclude.toArray(new String[0]),vo.getClass().getSimpleName());
			Gson gson = new Gson();
	        String json = gson.toJson(serialized);
	        model = gson.fromJson(json, Pasien.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return model;

	}

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

		if (null == models)
			return vos;

		for (Pasien pasien : models) {
			PasienVO pasienVO = new PasienVO();
			transferModelToVO(pasien, pasienVO);
			vos.add(pasienVO);
		}

		return vos;
	}

	public PasienVO transferModelToVO(Pasien model, PasienVO vo) {
		if (null == vo)

		vo = new PasienVO();

		try {
			Gson gson =   new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
					.create();//new Gson();
	        String json = gson.toJson(model.ToMap());
	        vo = new Gson().fromJson(json, PasienVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	
	public PasienVO transferModelToVOCustom(Pasien model, PasienVO vo,String[] fieldsToInclude) {
		if (null == vo)

		vo = new PasienVO();

		try {
			//Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
			Gson gson = new Gson();
	        String json = gson.toJson(model.ToMap());
	        vo = gson.fromJson(json, PasienVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}

	

}
