package com.jasamedika.medifirst2000.service.impl;

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

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.converter.DraftSuratConverter;
import com.jasamedika.medifirst2000.dao.DokumenDao;
import com.jasamedika.medifirst2000.dao.DraftSuratDao;
import com.jasamedika.medifirst2000.dao.DraftSuratRuanganDao;
import com.jasamedika.medifirst2000.entities.DetailSpekAnggaran;
import com.jasamedika.medifirst2000.entities.Dokumen;
import com.jasamedika.medifirst2000.entities.DraftSurat;
import com.jasamedika.medifirst2000.entities.JenisDokumen;
import com.jasamedika.medifirst2000.entities.JenisSurat;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.DraftSuratRuangan;
import com.jasamedika.medifirst2000.entities.Surat;
import com.jasamedika.medifirst2000.service.DraftSuratService;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.DraftSuratRuanganVO;
import com.jasamedika.medifirst2000.vo.DraftSuratVO;
import com.jasamedika.medifirst2000.vo.GridDetailAnggaran;
import com.jasamedika.medifirst2000.vo.GridDraftSuratVO;
import com.jasamedika.medifirst2000.vo.JenisSuratVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SuratVO;

@Service("DraftSuratService")
public class DraftSuratServiceImpl extends BaseAngaranServiceImpl implements DraftSuratService{
	
	@Autowired
	private DraftSuratRuanganDao draftSuratRuanganDao;
	
	@Autowired
	private DraftSuratDao draftSuratDao;
	
	@Autowired
	private DokumenDao dokumenDao;
	
	@Autowired
	private LoginUserService loginUserService;
	
	@Autowired
	private DraftSuratConverter draftSuratConverter;
	
	@Autowired
	private BaseConverterImpl<SuratVO, Surat> suratConverter;
	
	@Autowired
	private BaseConverterImpl<JenisSuratVO, JenisSurat> jenisSuratConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<DraftSuratRuanganVO,DraftSuratRuangan> draftSuratRuanganConverter;
	
	@Override
	@Transactional
	public Map<String, Object> addDraftSurat(DraftSuratVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		// Save ke dokumen
		Dokumen dokumen = new Dokumen();
		JenisDokumen jenisDokumen = new JenisDokumen();
		jenisDokumen.setId(6); // Menyatakan bahwa jenis dokumennya adalah draft
		dokumen.setJenisDokumen(jenisDokumen);
		if(CommonUtil.isNotNullOrEmpty(loginUserService.getLoginUser())){
			dokumen.setPegawaiPembuat(loginUserService.getLoginUser().getPegawai());
			dokumen.setNamaPegawaiPembuat(loginUserService.getLoginUser().getPegawai().getNamaLengkap());
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getSurat())){
			dokumen.setSurat(suratConverter.transferVOToModel(vo.getSurat(), new Surat()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getKeterangan())){
			dokumen.setDeskripsiDokumen(vo.getKeterangan());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getJudul())){
			dokumen.setNamaJudulDokumen(vo.getJudul());
		}
		
		dokumen.setqDokumen(0);
		dokumen.setQtyLampiran((short) 1);
		dokumen.setTglDokumen(new Date());
		
		
		dokumen.setNoDokumen(getNoSurat());
		
		Dokumen resultDokumen = new Dokumen();
		if(CommonUtil.isNotNullOrEmpty(resultDokumen)){
			resultDokumen = dokumenDao.save(dokumen);
		}
		if(CommonUtil.isNotNullOrEmpty(resultDokumen)){
			result.put("dokumen", resultDokumen.getId());
		}
		
		// Save ke Draft Surat
		DraftSurat draftSurat = new DraftSurat();
		Dokumen dokumenDraft = dokumenDao.findOne(resultDokumen.getId());
		if(CommonUtil.isNotNullOrEmpty(dokumenDraft)){
			draftSurat.setDokumen(dokumenDraft);
		}
		if(CommonUtil.isNotNullOrEmpty(vo)){
			draftSurat = draftSuratConverter.transferVOToModel(vo, new DraftSurat());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getJenisSurat())){
			draftSurat.setJenisSurat(jenisSuratConverter.transferVOToModel(vo.getJenisSurat(), new JenisSurat()));
		}
		
		draftSurat.setNoSurat(getNoSurat());
	
		List<DraftSuratRuangan> draftSuratRuanganList=new ArrayList<DraftSuratRuangan>();
		if(CommonUtil.isNotNullOrEmpty(vo.getDraftSuratRuangan())){
			for(DraftSuratRuanganVO draftSuratRuanganVO : vo.getDraftSuratRuangan()){
				DraftSuratRuangan draftSuratRuangan = new DraftSuratRuangan();
				draftSuratRuangan.setIsVerifikasi(false);
				draftSuratRuangan.setStatus(false);
				draftSuratRuangan.setDraftSurat(draftSurat);
				if(CommonUtil.isNotNullOrEmpty(draftSuratRuanganVO.getRuangan())){
					draftSuratRuangan.setRuangan(ruanganConverter.transferVOToModel(draftSuratRuanganVO.getRuangan(), new Ruangan()));
				}
				draftSuratRuanganList.add(draftSuratRuangan);
			}
		}
		
		draftSurat.getDraftSuratRuangan().clear();
		draftSurat.getDraftSuratRuangan().addAll(draftSuratRuanganList);		

		DraftSurat resultModel = new DraftSurat();
		draftSurat.setDokumen(dokumenDraft);
		if(CommonUtil.isNotNullOrEmpty(draftSurat)){
			resultModel = draftSuratDao.save(draftSurat);
		}
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("draft", resultModel.getNoRec());
		}
		
		return result;
	}
	
	
	
	@Transactional
	@Override
	public String getNoSurat() {
		String currentDate = null;
		if (CommonUtil.isNotNullOrEmpty(Calendar.getInstance())) {
			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);
			String stringKdTerima = StringUtil.formatNumber(String.valueOf(draftSuratDao.getCount() + 1), 6);
			currentDate = "DRAFT" + year.toString() + monthString + dateString + stringKdTerima;
		}
		return currentDate;
		
	}



	@Override
	public Map<String, Object> gridDraftSurat(Integer page, Integer limit, String sort, String dir,Specification<DraftSuratRuangan> 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<DraftSuratRuangan> resultPage = draftSuratRuanganDao.findAll(spec, pageable);
		List<DraftSuratRuangan> draftSuratList = resultPage.getContent();
		List<GridDraftSuratVO>listGridDraftSurat=new ArrayList<GridDraftSuratVO>();
		for(DraftSuratRuangan model:draftSuratList){
			GridDraftSuratVO gridDraftSurat=new GridDraftSuratVO();
			if(CommonUtil.isNotNullOrEmpty(model.getDraftSurat())){
				gridDraftSurat.setNoDraftSurat(model.getDraftSurat().getNoSurat());
			}
			if(CommonUtil.isNotNullOrEmpty(model.getIsVerifikasi())){
				if(model.getIsVerifikasi()){
					gridDraftSurat.setIsVerifikasi(true);
				}else{
					gridDraftSurat.setIsVerifikasi(false);
				}
			}else{
				gridDraftSurat.setIsVerifikasi(false);
			}
			
			if(CommonUtil.isNotNullOrEmpty(model.getStatus())){
				if(model.getStatus()){
					gridDraftSurat.setStatus(true);
				}else{
					gridDraftSurat.setStatus(false);
				}
			}else{
				gridDraftSurat.setStatus(false);
			}
			
			if(CommonUtil.isNotNullOrEmpty(model.getFileName())){
				gridDraftSurat.setFileName(model.getFileName());
			}else{
				gridDraftSurat.setFileName("-");
			}
	
			gridDraftSurat.setNamaRuangan(model.getRuangan().getNamaRuangan());
			gridDraftSurat.setIdRuangan(model.getRuanganId());
			
			gridDraftSurat.setNoRec(model.getNoRec());
			if(CommonUtil.isNotNullOrEmpty(model.getDraftSurat())){
				if(CommonUtil.isNotNullOrEmpty(model.getDraftSurat().getJenisSurat())){
					gridDraftSurat.setJudulSurat(model.getDraftSurat().getJenisSurat().getName());
				}else{
					gridDraftSurat.setJudulSurat("-");
				}
			}else{
				gridDraftSurat.setJudulSurat("-");
			}
			listGridDraftSurat.add(gridDraftSurat);
		}
		
		return constructMapReturn(listGridDraftSurat, resultPage.getTotalElements(), resultPage.getTotalPages());
	}



	@Override
	@Transactional(readOnly=false)
	public DraftSuratVO findByNoRec(String noRec) {
		DraftSurat draftSurat=draftSuratDao.findById(noRec);
		DraftSuratVO draftSuratVO=new DraftSuratVO();
		if(CommonUtil.isNotNullOrEmpty(draftSurat)){
			
			draftSuratVO=draftSuratConverter.transferModelToVO(draftSurat, new DraftSuratVO());
			
			if(CommonUtil.isNotNullOrEmpty(draftSurat.getJenisSurat())){
				draftSuratVO.setJenisSurat(jenisSuratConverter.transferModelToVO(draftSurat.getJenisSurat(), new JenisSuratVO()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(draftSurat.getDokumen().getSurat())){
				draftSuratVO.setSurat(suratConverter.transferModelToVO(draftSurat.getDokumen().getSurat(), new SuratVO()));
			}
			for(DraftSuratRuangan draftSuratRuangan:draftSurat.getDraftSuratRuangan()){
				DraftSuratRuanganVO draftSuratRuanganVO=draftSuratRuanganConverter.transferModelToVO(draftSuratRuangan, new DraftSuratRuanganVO());
				if(CommonUtil.isNotNullOrEmpty(draftSuratRuanganVO.getRuangan())){
					if(CommonUtil.isNotNullOrEmpty(draftSuratRuanganVO.getRuangan().getDepartemen())){
						draftSuratRuanganVO.getRuangan().setDepartemen(null);
					}
				}
				draftSuratVO.getDraftSuratRuangan().add(draftSuratRuanganVO);
			}
			
		}
		return draftSuratVO;
	}
	
	

}
