package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.IteratorUtils;
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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.AgamaConverter;
import com.jasamedika.medifirst2000.dao.AgamaDao;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.entities.Agama;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.AgamaService;
import com.jasamedika.medifirst2000.service.GenerateNumberService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.AgamaVO;

@Service("generateNumberServiceImpl")
public class GenerateNumberServiceImpl extends BaseVoServiceImpl implements GenerateNumberService {

	@Autowired
	private PasienDao pasienDao;
	@Override
	public AgamaVO add(AgamaVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

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

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

	@Override
	@Transactional(readOnly = false)
	public String generatePasienCM() {
		List<String> nocmList =pasienDao.MaxNoCm(8);// IteratorUtils.toList(pasienDao.findAllNoCm().iterator());
		String noCM =null;
		if(nocmList.size()!=0)
			noCM=nocmList.get(nocmList.size()-1);
		// generate cari no yang hilang
		String missNoCm ="" ;//getCM(nocmList, 8);
		if(noCM != null)
		{
			Integer maxNoCm=Integer.parseInt(noCM)+1;
			missNoCm = StringUtil.formatNumber(maxNoCm.toString(), 8); 
		}
		else
			{
			Integer maxNoCm=Integer.parseInt("0")+1;
			missNoCm = StringUtil.formatNumber(maxNoCm.toString(), 8); 
			
			}
		return missNoCm;
	}
	private String getCM(List<String> nocmList, int lengthDigit) {
		int d = lengthDigit;
		StringBuffer buf = new StringBuffer();
		for (int i = 1; i <= 10; i++) {
			buf.append("9");
		}
		Long end = Long.valueOf(buf.toString());
		List<Long> nocms = new ArrayList<Long>();
		nocms.add(0L);
		nocms.add(end);
		for (String pas : nocmList) {
			if (pas != null) {
				nocms.add(Long.valueOf(pas));
			}
		}
		Collections.sort(nocms);

		Long[] nocmGen = nocms.toArray(new Long[nocms.size()]);
		long missNoCm = missingNumberFromSortedArray(nocmGen);
		String missNoCmS = String.format("%0" + d + "d", missNoCm);
		return missNoCmS;
	}
	private long missingNumberFromSortedArray(Long[] numbers) {
		if (numbers == null || numbers.length <= 0) {
			throw new IllegalArgumentException("Null or Empty array not permitted");
		}
		long left = 0;
		long right = numbers.length - 1;
		while (left <= right) {
			long middle = (right + left) >> 1;
			if (numbers[(int) middle] != middle) {
				if (middle == 0 || numbers[(int) middle - 1] == middle - 1) {
					return middle;
				}
				right = middle - 1;
			} else {
				left = middle + 1;
			}
		}
		throw new RuntimeException("No missing number");
	}
	
}
