package com.jasamedika.medifirst2000.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.KajianEvaluasiConverter;
import com.jasamedika.medifirst2000.dao.DetailKajianEvaluasiDao;
import com.jasamedika.medifirst2000.dao.KajianEvaluasiDao;
import com.jasamedika.medifirst2000.entities.DetailKajianEvaluasi;
import com.jasamedika.medifirst2000.entities.KajianEvaluasi;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.paging.Comparison;
import com.jasamedika.medifirst2000.paging.Condition;
import com.jasamedika.medifirst2000.paging.Filter;
import com.jasamedika.medifirst2000.service.KajianEvaluasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.vo.KajianEvaluasiVO;

@Service("KajianEvaluasiService")
public class KajianEvaluasiServiceImpl extends BaseVoServiceImpl implements KajianEvaluasiService{

	@Autowired
	private KajianEvaluasiDao kajianDao;
	
	@Autowired
	private DetailKajianEvaluasiDao detailDao;
	
	@Autowired
	private KajianEvaluasiConverter converter;
	
	public Map<String, Object> findAll(Integer page, Integer take, String sort, String dir, Specification<KajianEvaluasi> spec){
		Direction direction=null;
		
		if(sort.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<KajianEvaluasi> resultPage = kajianDao.findAll(spec, pageable);
		List<KajianEvaluasi> kajianList = resultPage.getContent();
		
		for(KajianEvaluasi kajianEvaluasi : kajianList){
			for(DetailKajianEvaluasi detail : kajianEvaluasi.getDetailKajianSet()){
				detail.setKajianEvaluasi(null);
			}
		}
		
		return constructMapReturn(kajianList, resultPage.getTotalElements(), resultPage.getTotalPages());
	}
	
	public Map<String, Object> saveKajian(KajianEvaluasiVO vo){
		Map<String,Object> result = new HashMap<String, Object>();
		KajianEvaluasi model = converter.transferVOToModel(vo, new KajianEvaluasi());
		
		List<DetailKajianEvaluasi> listDetail = new LinkedList<DetailKajianEvaluasi>();
		for(DetailKajianEvaluasi detail : model.getDetailKajianSet())
		{
			listDetail.add(detail);
		}
		model.setDetailKajianSet(null);
		kajianDao.save(model);
		
		for (DetailKajianEvaluasi detail : listDetail) {
			detail.setKajianEvaluasi(model);
			detailDao.save(detail);
		}
		
		kajianDao.save(model);
		
		
		return result;
	}
	
	@Override
	public Map<String,Object> generateNoUsulan(Integer bulan, Integer tahun) {
		Map<String,Object> result = new HashMap<String,Object>();
		
		int count = kajianDao.CountbyMonthYear(bulan, tahun);
		
		String noUsulan =tahun+"/"+String.format("%02d", bulan)+"/EK/"+String.format("%02d",count+1)+"/00"; // result = "year/month/EK/noCount"
		
		result.put("noUsulan", noUsulan);
		return result;
	}
	
	@Override
	public KajianEvaluasiVO add(KajianEvaluasiVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public KajianEvaluasiVO update(KajianEvaluasiVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public KajianEvaluasiVO findById(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<KajianEvaluasiVO> findAll() throws JpaSystemException {
		return null;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

	@Override
	public Map<String, Object> deleteKajian(List<KajianEvaluasiVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		for(KajianEvaluasiVO vo:vos){
			KajianEvaluasi model = kajianDao.findOne(vo.getId());
			kajianDao.delete(model);
		}
		return result;
	}

}
