package com.jasamedika.medifirst2000.converter;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.dao.custom.base.impl.FindConverterDao;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.service.KelompokUserService;
import com.jasamedika.medifirst2000.service.PegawaiService;
import com.jasamedika.medifirst2000.util.ExtendedSpringBeanUtil;
import com.jasamedika.medifirst2000.vo.KelompokUserVO;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

/**
 * Converter class between LoginUser and LoginUserVO
 * 
 * @author Roberto
 */
@Component
public class LoginUserAuthConverter extends FindConverterDao implements
		BaseVoConverter<LoginUserVO, LoginUser> {
/*
	@Override
	public LoginUser transferVOToModel(LoginUserVO vo, LoginUser model) {
		if (null == model)
			model = new LoginUser();

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

		return vo;
	}
	*/
	 
	public LoginUser transferVOToModel(LoginUserVO vo, LoginUser model) {
		if (null == model)
			model = new LoginUser();

		// fix this
		ExtendedSpringBeanUtil.copySpecificProperties(vo, model, new String[] {
				"id", "kataSandi", "namaUser", }, new String[] { "id",
				"kataSandi", "namaUser", });
		if (vo.getKelompokUser() != null) {
			KelompokUser kelompokUser = (KelompokUser) findObjectById(
					KelompokUser.class, vo.getKelompokUser().getId());
			model.setKelompokUser(kelompokUser);
		}
		return model;

	}

	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 loginUserVO = new LoginUserVO();
			transferModelToVO(loginUser, loginUserVO);
			vos.add(loginUserVO);
		}

		return vos;
	}

	public LoginUserVO transferModelToVO(LoginUser model, LoginUserVO vo) {
		if (null == vo)

			vo = new LoginUserVO();

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

		if (model.getKelompokUser() != null) {
			KelompokUserVO kelompokUserVo = kelompokUserService.findById(model
					.getKelompokUser().getId());
			vo.setKelompokUser(kelompokUserVo);
		}
		if (model.getPegawai() != null) {
			PegawaiVO kelompokUserVo = pgawaiService.findById(model
					.getPegawai().getId());
			vo.setPegawai(kelompokUserVo);
		}
		return vo;
	}

	@Autowired
	private KelompokUserService kelompokUserService;
	@Autowired
	private PegawaiService pgawaiService;
}
