package com.jasamedika.medifirst2000.converter;

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

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.dao.custom.base.impl.FindConverterDao;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.vo.LoginUserVO;

/**
 * Converter class between LoginUser and LoginUserVO
 * 
 * @author Roberto
 */
@Component
public class LoginUserConverter extends FindConverterDao implements
		BaseVoConverter<LoginUserVO, LoginUser> {

	@Override
	public LoginUser transferVOToModel(LoginUserVO vo, LoginUser model) {
		if (null == model)
			model = new LoginUser();
		
//		BeanUtils.copyProperties(vo, model);

		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, LoginUser.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return model;
	}

	@Override
	public List<LoginUserVO> transferListOfModelToListOfVO(List<LoginUser> models, List<LoginUserVO> vos) {
		if (null == vos)
			vos = new ArrayList<LoginUserVO>();

		if (null == models)
			return vos;

		for (LoginUser loginUser : models) {
			LoginUserVO vo = new LoginUserVO();
			vo=transferModelToVO(loginUser, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public LoginUserVO transferModelToVO(LoginUser model, LoginUserVO vo) {
		if (null == vo)
			vo = new LoginUserVO();
		
		BeanUtils.copyProperties(model, vo);
		
//		try {
//			String[] fieldsToInclude = null;
//			//Alter Syamsu
//			//Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
//			Map<String, Object> serialized = model.ToMap();
//	        Gson gson = new Gson();
//
//	        String json = gson.toJson(serialized);
//	        vo = gson.fromJson(json, LoginUserVO.class);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

		return vo;
	}
	
}
