package com.jasamedika.medifirst2000.service.impl;

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.KamusIndikatorConverter;
import com.jasamedika.medifirst2000.dao.KamusIndikatorDao;
import com.jasamedika.medifirst2000.dao.KontrakKinerjaDao;
import com.jasamedika.medifirst2000.dao.TargetIndikatorDao;
import com.jasamedika.medifirst2000.dto.DetailKamusIndikatorDTO;
import com.jasamedika.medifirst2000.dto.MatriksIndikatorDTO;
import com.jasamedika.medifirst2000.entities.KamusIndikator;
import com.jasamedika.medifirst2000.entities.KontrakKinerja;
import com.jasamedika.medifirst2000.entities.TargetIndikator;
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.KamusIndikatorService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KamusIndikatorVO;

@Service("KamusIndikatorService")
public class KamusIndikatorServiceImpl extends BaseVoServiceImpl implements KamusIndikatorService{

	@Autowired
	private KamusIndikatorDao kamusIndikatorDao;
	
	@Autowired
	private KamusIndikatorConverter KamusIndikatorConverter;

	@Autowired
	private TargetIndikatorDao targetDao;
	
	@Override
	public KamusIndikatorVO add(KamusIndikatorVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<KamusIndikatorVO> 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> findAll(Integer page, Integer take, String sort, String dir,
			Specification<KamusIndikator> spec) {
		Direction direction=null;
		
		if(dir.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<KamusIndikator> resultPage = kamusIndikatorDao.findAll(spec, pageable);
		List<KamusIndikator> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(KamusIndikatorVO vo) {
		Map<String,Object> result = new HashMap<String,Object>();
		KamusIndikator model = KamusIndikatorConverter.transferVOToModel(vo, new KamusIndikator());
		
		kamusIndikatorDao.save(model);
		
		if(CommonUtil.isNotNullOrEmpty(model.getId())){
			for(int now = model.getAwalPeriode();now<=model.getAkhirPeriode();now++){
				TargetIndikator modelTarget = new TargetIndikator();
				modelTarget.setKamusIndikator(model);
				modelTarget.setTahun(now);
				modelTarget.setTarget("-");
				targetDao.save(modelTarget);
			}
		}
		
		return result;
	}

	@Override
	public Map<String, Object> findDetail(Integer page, Integer take, String sort, String dir,
			Specification<KamusIndikator> spec) {
		Direction direction=null;
		List<DetailKamusIndikatorDTO> dtos = new LinkedList<DetailKamusIndikatorDTO>();
		
		if(dir.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<KamusIndikator> resultPage = kamusIndikatorDao.findAll(spec, pageable);
		List<KamusIndikator> resultData = resultPage.getContent();
		
		for(KamusIndikator model : resultData){
			
			Filter filter = new Filter();
			if (CommonUtil.isNotNullOrEmpty(model.getId())) {
				filter.addCondition(new Condition.Builder().setComparison(Comparison.eq).setField("kamusIndikatorId")
						.setValue(model.getId()).build());
			}
			filter.addCondition(new Condition.Builder().setComparison(Comparison.inn).setField("id").build());
			
			Page<TargetIndikator> resultPageKontrak = targetDao.findAll(filter, pageable);
			List<TargetIndikator> resultDataKontrak = resultPageKontrak.getContent();
			
			DetailKamusIndikatorDTO resultObj = new DetailKamusIndikatorDTO();
			
			resultObj.setKamus(model);
			resultObj.setListTarget(resultDataKontrak);
			
			dtos.add(resultObj);
			}
		
		return constructMapReturn(dtos, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

}
