package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.JenisSuratDao;
import com.jasamedika.medifirst2000.dao.NotaDinasDao;
import com.jasamedika.medifirst2000.dao.NotaDinasJabatanDao;
import com.jasamedika.medifirst2000.entities.Jabatan;
import com.jasamedika.medifirst2000.entities.JenisSurat;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.NotaDinas;
import com.jasamedika.medifirst2000.entities.NotaDinasJabatan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.service.NotaDinasService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.GridNotaDinas;
import com.jasamedika.medifirst2000.vo.JabatanVO;
import com.jasamedika.medifirst2000.vo.NotaDinasJabatanVO;
import com.jasamedika.medifirst2000.vo.NotaDinasVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

@Service("NotaDinasService")
public class NotaDinasServiceImpl extends BaseVoServiceImpl implements NotaDinasService{
	
	
	@Autowired
	private NotaDinasDao notaDinasDao;
	 
	@Autowired
	private NotaDinasJabatanDao notaDinasJabatanDao;
	
	@Autowired
	private JenisSuratDao jenisSuratDao;
	
	@Autowired
	private BaseConverterImpl<NotaDinasVO, NotaDinas> notaDinasConverter;
	
	@Autowired
	private BaseConverterImpl<NotaDinasJabatanVO, NotaDinasJabatan> notaDinasJabatanConverter;
	 
	@Autowired
	private BaseConverterImpl<JabatanVO, Jabatan> jabatanConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter; 
	@Autowired
	private LoginUserService loginUserService;
	
	@Override
	@Transactional
	public Map<String, Object> addNotaDinas(NotaDinasVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		//Save Nota Dinas
		NotaDinas notaDinas = new NotaDinas();
		 
		if(CommonUtil.isNotNullOrEmpty(vo)){
			notaDinas = notaDinasConverter.transferVOToModel(vo, new NotaDinas());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPegawaiPembuat())){
			notaDinas.setPegawaiPembuat(pegawaiConverter.transferVOToModel(vo.getPegawaiPembuat(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getJabatan())){
			notaDinas.setJabatan(jabatanConverter.transferVOToModel(vo.getJabatan(), new Jabatan()));
		}
		notaDinas.setNoSurat(getNoSurat());
	
		List<NotaDinasJabatan> notaDinasJabatanList=new ArrayList<NotaDinasJabatan>();
		if(CommonUtil.isNotNullOrEmpty(vo.getNotaDinasJabatan())){
			for(NotaDinasJabatanVO notaDinasJabatanVO : vo.getNotaDinasJabatan()){
				NotaDinasJabatan notaDinasJabatan = new NotaDinasJabatan();
				
				notaDinasJabatan.setNotaDinas(notaDinas);
				
				if(CommonUtil.isNotNullOrEmpty(notaDinasJabatanVO.getJabatan())){
					notaDinasJabatan.setJabatan(jabatanConverter.transferVOToModel(notaDinasJabatanVO.getJabatan(), new Jabatan()));
				}
				
				if(CommonUtil.isNotNullOrEmpty(notaDinasJabatanVO.getPegawai())){
					notaDinasJabatan.setPegawai(pegawaiConverter.transferVOToModel(notaDinasJabatanVO.getPegawai(), new Pegawai()));
				}
				
				notaDinasJabatanList.add(notaDinasJabatan);
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getJabatan())){
			notaDinas.setJabatan(jabatanConverter.transferVOToModel(vo.getJabatan(), new Jabatan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getPegawaiPembuat())){
			notaDinas.setPegawaiPembuat(pegawaiConverter.transferVOToModel(vo.getPegawaiPembuat(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getJenisSuratString())){
			JenisSurat jenisSurat= jenisSuratDao.findByIdName(vo.getJenisSuratString());
			if (CommonUtil.isNullOrEmpty(jenisSurat)){
				jenisSurat = new JenisSurat();
				jenisSurat.setName(vo.getJenisSuratString());
				jenisSuratDao.save(jenisSurat);
			}
			notaDinas.setJenisSurat(jenisSurat);
		}
		notaDinas.getNotaDinasJabatan().clear();
		notaDinas.getNotaDinasJabatan().addAll(notaDinasJabatanList);		

		NotaDinas resultModel = new NotaDinas();
		 
		if(CommonUtil.isNotNullOrEmpty(notaDinas)){
			resultModel = notaDinasDao.save(notaDinas);
		}
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("draft", resultModel.getNoRec());
		}
		
		return result;
	}
	
	
	
	@Override
	public String getNoSurat() {
		String currentDate = null;
		Calendar calendar = Calendar.getInstance();
		if (CommonUtil.isNotNullOrEmpty(calendar)) {
			//Calendar calendar = Calendar.getInstance();
			Integer date = calendar.get(Calendar.DATE);
			String dateString = StringUtil.formatNumber(String.valueOf(date), 2);
			Integer month = calendar.get(Calendar.MONTH);
			String monthString = StringUtil.formatNumber(String.valueOf(month), 2);
			Integer year = calendar.get(Calendar.YEAR);
			Integer count=notaDinasDao.getCount();
			System.out.println("Masuk kesini "+count);
			String stringKdTerima = StringUtil.formatNumber(String.valueOf(count + 1), 6);
			currentDate = "NOTADINAS" + year.toString() + monthString + dateString + stringKdTerima;
		}
		return currentDate;
		
	}



	@Override
	public Map<String, Object> gridNotaDinas(Integer page, Integer limit, String sort, String dir,Specification<NotaDinasJabatan> spec) {
		

		
		Direction direction=null;
		
		if(sort.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, limit, direction, sort);
		Page<NotaDinasJabatan> resultPage = notaDinasJabatanDao.findAll(spec, pageable);
		List<NotaDinasJabatan> draftSuratList = resultPage.getContent();
		List<GridNotaDinas>listGridDraftSurat=new ArrayList<GridNotaDinas>();
		for(NotaDinasJabatan model:draftSuratList){
			GridNotaDinas gridNotaDinas=new GridNotaDinas();
			if(CommonUtil.isNotNullOrEmpty(model.getNotaDinas())){
				gridNotaDinas.setHal(model.getNotaDinas().getHal());
				gridNotaDinas.setNomor(model.getNotaDinas().getNoSurat());
				gridNotaDinas.setTanggal(model.getNotaDinas().getTanggal());
				if(CommonUtil.isNotNullOrEmpty(model.getJabatan())){
					gridNotaDinas.setRuanganPengirim(model.getJabatan().getNamaJabatan());
				}
				
				if(CommonUtil.isNotNullOrEmpty(model.getPegawai())){
					gridNotaDinas.setRuanganPenerima(model.getPegawai().getNamaLengkap());
				}
			}
			if(CommonUtil.isNotNullOrEmpty(model.getJabatan())){
				gridNotaDinas.setRuanganPenerima(model.getJabatan().getNamaJabatan());
			}
			if(CommonUtil.isNotNullOrEmpty(model.getPegawai())){
				gridNotaDinas.setPegawaiPenerima(model.getPegawai().getNamaLengkap());
			}
			listGridDraftSurat.add(gridNotaDinas);
		}
		
		return constructMapReturn(listGridDraftSurat, resultPage.getTotalElements(), resultPage.getTotalPages());
	
		
	}



	@Override
	@Transactional(readOnly=false)
	public NotaDinasVO findByNoRec(String noRec) {
		NotaDinas draftSurat=notaDinasDao.findById(noRec);
		NotaDinasVO notaDinasVO=new NotaDinasVO();
		if(CommonUtil.isNotNullOrEmpty(draftSurat)){
			
			notaDinasVO=notaDinasConverter.transferModelToVO(draftSurat, new NotaDinasVO());
			
			
			/*if(CommonUtil.isNotNullOrEmpty(draftSurat.getDokumen().getSurat())){
				notaDinasVO.setSurat(suratConverter.transferModelToVO(draftSurat.getDokumen().getSurat(), new SuratVO()));
			}*/
			for(NotaDinasJabatan notaDinasRuangan:draftSurat.getNotaDinasJabatan()){
				NotaDinasJabatanVO notaDinasRuanganVO=notaDinasJabatanConverter.transferModelToVO(notaDinasRuangan, new NotaDinasJabatanVO());
				notaDinasVO.getNotaDinasJabatan().add(notaDinasRuanganVO);
			}
			
		}
		return notaDinasVO;
	}



	 
	@Override
	public List<Map<String, Object>> getInboxNotaDinas(String dateStart, String dateEnd, Integer idRuangan,
			String flag,Integer idPegawai) {
		LoginUser loginuser= loginUserService.getLoginUser();
		 	List<Map<String, Object>> result= notaDinasDao.getInboxNotaDinas(dateStart, dateEnd, idPegawai);
		 if (CommonUtil.isNotNullOrEmpty(result)){
			return	result;
		}else{
			return null;
		}
		 
	}


	@Override
	public List<Map<String, Object>> getOutboxNotaDinas(String dateStart, String dateEnd, Integer idRuangan,
			String flag,Integer idPegawai) {
		LoginUser loginuser= loginUserService.getLoginUser();
		 	List<Map<String, Object>> result= notaDinasDao.getOutboxNotaDinas(dateStart, dateEnd, idPegawai);
		 if (CommonUtil.isNotNullOrEmpty(result)){
			return	result;
		}else{
			return null;
		}
		 
	}



	@Override
	public String saveAsRead(NotaDinasJabatanVO vo) {
		NotaDinasJabatan notaDinasJabatan=notaDinasJabatanDao.findById(vo.getNoRec());
		notaDinasJabatan.setIsRead(Boolean.TRUE);
		notaDinasJabatanDao.save(notaDinasJabatan);
		return notaDinasJabatan.getNoRec();
	}
	
	

}
