package com.jasamedika.medifirst2000.converter;

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

import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.vo.PegawaiVO_;

@Component
public class MapPegawaiJabatanToUnitKerjaConverter implements BaseVoConverter<PegawaiVO_, Pegawai> {

	@Override
	public Pegawai transferVOToModel(PegawaiVO_ vo, Pegawai model) {
		if (null == model)
			model = new Pegawai();

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

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

		if (null == models)
			return vos;

		for (Pegawai pegawai : models) {
			PegawaiVO_ vo = new PegawaiVO_();
			vo=transferModelToVO(pegawai, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public PegawaiVO_ transferModelToVO(Pegawai model, PegawaiVO_ vo) {
		if (null == vo)
			vo = new PegawaiVO_();
		try {
			
			List<String> fieldsToInclude = new ArrayList<String>();
			for (Field field : model.GetFields(model.getClass())) {
				String str = field.getName();
				String name = field.getName();
				if(name.equals("serialVersionUID"))continue;
				Boolean valid = false;
				for (java.lang.annotation.Annotation annotation : field.getDeclaredAnnotations()) {
					if (annotation instanceof JoinColumn) {
						valid=true;
					} else if (annotation instanceof Column) {
						Column column = (Column)annotation; 
						if(column.name().endsWith("Fk"))
							if(field.getName().endsWith("Id")==false)
								valid=true;
					}else if (annotation instanceof OneToMany) {
						
						valid=true;
					}
						
				}
				if(valid ==false)
					fieldsToInclude.add(str);
				
			}
			Map<String, Object> serialized = model.ToMap();
			Gson gson = new Gson();
//			serialized.put("jenisPegawai", model.getJenisPegawai());
//			serialized.put("pegawai", model.getId());
//			serialized.put("jabatanInternal", model.getJabatanInternal());
//			serialized.put("unitKerja", model.getUnitKerja());
			serialized.put("unitKerjaPegawai", model.getUnitKerja().getUnitKerja());
			serialized.put("isCanCreateJadwal", model.getJabatanInternal().getNamaJabatan().contains("Kepala")? true : false);
			serialized.put("isPrimary", true);
			serialized.put("statusEnabled1", true);
	        String json = gson.toJson(serialized);
	        vo = gson.fromJson(json, PegawaiVO_.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	}

}
