package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.AsalProdukDao;
import com.jasamedika.medifirst2000.dao.DetailAnggaranDao;
import com.jasamedika.medifirst2000.dao.DetailRupDao;
import com.jasamedika.medifirst2000.dao.HeadRupDao;
import com.jasamedika.medifirst2000.dao.MataAnggaranDao;
import com.jasamedika.medifirst2000.dao.custom.DetailRupDaoCustom;
import com.jasamedika.medifirst2000.dto.DetailRupDto;
import com.jasamedika.medifirst2000.entities.DetailAnggaran;
import com.jasamedika.medifirst2000.entities.DetailPengadaan;
import com.jasamedika.medifirst2000.entities.DetailRup;
import com.jasamedika.medifirst2000.entities.DetailSpekAnggaran;
import com.jasamedika.medifirst2000.entities.HeadRup;
import com.jasamedika.medifirst2000.entities.Pengadaan;
import com.jasamedika.medifirst2000.entities.Pengendali;
import com.jasamedika.medifirst2000.service.DetailRupService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.DetailAnggaranVO;
import com.jasamedika.medifirst2000.vo.DetailPengadaanVO;
import com.jasamedika.medifirst2000.vo.DetailRupCustomVO;
import com.jasamedika.medifirst2000.vo.DetailSpekAnggaranVO;
import com.jasamedika.medifirst2000.vo.GridDetailAnggaran;
import com.jasamedika.medifirst2000.vo.PengadaanVO;

@Service("detailRupService")
public class DetailRupServiceImpl extends BaseAngaranServiceImpl  implements DetailRupService{
	
	@Autowired
	private DetailAnggaranDao detailAnggaranDao;
	
	@Autowired 
	private DetailRupDao detailRupDao;
	
	@Autowired 
	private AsalProdukDao asalProdukDao;
	
	@Autowired 
	private MataAnggaranDao mataAnggaranDao;
	
	@Autowired 
	private HeadRupDao headRupDao;
	
	@Autowired 
	private DetailRupDaoCustom detailRupDaoCustom;
	
	@Autowired
	private BaseConverterImpl<DetailSpekAnggaranVO,DetailSpekAnggaran> detailSpekAnggaranConverter;
	
	@Autowired
	private BaseConverterImpl<DetailAnggaranVO ,DetailAnggaran> detailAnggaranConverter;
	
	@Autowired
	private BaseConverterImpl<PengadaanVO ,Pengadaan> pengadaaanConverter;
	
	
	@Override
	public Map<String, Object> saveDetailLRup(DetailRupCustomVO vo) {
		//save head Rup
		HeadRup headRup=new HeadRup();
		headRup.setStatusEnabled(true);
		headRup.setKdHeadRup(GenerateKdHead());
		headRup.setTanggal(new Date());
		headRupDao.save(headRup);
		
		//Save Detail Rup
		Map<String, Object> result = new HashMap<String, Object>();
		ArrayList<String> resultArray=new ArrayList<String>();
		if(CommonUtil.isNotNullOrEmpty(vo.getDetailSpekAnggaran())){
			
			
			for(DetailSpekAnggaranVO modelVo : vo.getDetailSpekAnggaran()){
				DetailSpekAnggaran detailSpekAnggaran=new DetailSpekAnggaran();
				if(CommonUtil.isNotNullOrEmpty(modelVo)){
					detailSpekAnggaran=detailSpekAnggaranConverter.transferVOToModel(modelVo, new DetailSpekAnggaran());
				}
				
				DetailRup detailRup=new DetailRup(); 
				detailRup.setHeadRup(headRup);
				detailRup.setKeterangan(vo.getKeterangan());
				detailRup.setPelaksanaanPekerjaanAhir(vo.getPelaksanaanPekerjaanAhir());
				detailRup.setPelaksanaanPekerjaanAwal(vo.getPelaksanaanPekerjaanAwal());
				detailRup.setPelaksanaanPemilihanAhir(vo.getPelaksanaanPemilihanAhir());
				detailRup.setPelaksanaanPemilihanAwal(vo.getPelaksanaanPemilihanAwal());
				detailRup.setDetailSpekAnggaran(detailSpekAnggaran);
				
				if(CommonUtil.isNotNullOrEmpty(vo.getDetailPengadaan())){
					for(DetailPengadaanVO detailPengadaanVO:vo.getDetailPengadaan()){
						DetailPengadaan detailPengadaan=new DetailPengadaan();
						detailPengadaan.setDetailRup(detailRup);
						detailPengadaan.setPengadaan(pengadaaanConverter.transferVOToModel(detailPengadaanVO.getPengadaan(), new Pengadaan()));
						detailRup.getDetailPengadaan().add(detailPengadaan);
					}
				}
				
				detailRup=detailRupDao.save(detailRup);
				resultArray.add(detailRup.getNoRec());
			}
			
		}
		
		result.put("resultArray", resultArray);
		return result;
	}


	@Override
	public Map<String, Object> detailRupPaging(Integer page, Integer limit, String sort, String dir, String dateStart, String dateEnd) {
		List<Integer>pengedaliIdList=getPengendaliByUserLoginList();
		int totalRow = detailRupDaoCustom.findPreAddRupCount(sort, dateStart, dateEnd,pengedaliIdList);
		int totalPages = 0;

		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("data", detailRupDaoCustom.findPreAddRupList(rowStart, rowEnd, sort, dir, dateStart, dateEnd,pengedaliIdList));
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
	}


	@Override
	public Map<String, Object> preAddKartuPengendali(Integer page, Integer limit, String sort, String dir,String dateStart, String dateEnd, Integer kegiatanId, Integer akunId, Integer asalProdukId) {
		List<Integer>pengedaliIdList=getPengendaliByUserLoginList();
		int totalRow = detailRupDaoCustom.findCountPreAddKartuPengendali(sort, dateStart, dateEnd,pengedaliIdList,kegiatanId,akunId,asalProdukId);
		int totalPages = 0;

		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("data", detailRupDaoCustom.findListPreAddKartuPengendali(rowStart, rowEnd, sort, dir, dateStart, dateEnd,pengedaliIdList,kegiatanId,akunId,asalProdukId));
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
	}
	
	@Override
	public Map<String, Object> findRupList(Integer page, Integer limit, String sort, String dir,String dateStart, String dateEnd) {
		List<Integer>pengedaliIdList=getPengendaliByUserLoginList();
		int totalRow = detailRupDaoCustom.findRupCount(sort, dateStart, dateEnd, pengedaliIdList);
		int totalPages = 0;

		int pageRequested = page;

		if (totalRow > 0) {
			totalPages = (int) Math.ceil((double) totalRow / (double) limit);
		} else {
			totalPages = 0;
		}

		if (pageRequested > totalPages)
			pageRequested = totalPages;
		int rowStart = pageRequested * limit - limit;
		if (rowStart < 0) {
			rowStart = 0;
		}
		int rowEnd = limit;
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("data", detailRupDaoCustom.findRupList(rowStart, rowEnd, sort, dir, dateStart, dateEnd, pengedaliIdList));
		result.put("totalPages", totalPages);
		result.put("totalRow", totalRow);
		return result;
	}
	
	/*/
	Generate No KdHead
	**/
	public String GenerateKdHead() {
		Date dateNow=new Date();
		Integer month=DateUtil.getMonthFromDate(dateNow);
		Integer year=DateUtil.getYearFromDate(dateNow);
		Long count=headRupDao.count();
		
		String monthNow=month.toString();
		String yearNow=year.toString().substring(2, 4);
		String noOrder=null;
		
		count++;
		if(count.toString().length()==1){
			noOrder="00000";}
		else if(count.toString().length()==2){
			noOrder="0000";}
		else if(count.toString().length()==3){
			noOrder="000";}
		else if(count.toString().length()==4){
			noOrder="00";}
		else if(count.toString().length()==5){
			noOrder="0";}
		if(monthNow.length()==1)
			monthNow = "0"+monthNow;

		return yearNow+monthNow+noOrder+count;
	}


	@Override
	public Map<String, Object> getDetailRupHeaderByNoRec(String noRec) {
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("detailRup",detailRupDaoCustom.findDetailRupByParent(noRec));
		return result;
	}


	@Override
	public Map<String, Object> getAsalProduk(Integer kegiatanId) {
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("asalProduk",asalProdukDao.getAsalProduk(kegiatanId));
		return result;
	}


	@Override
	public Map<String, Object> getMataAnggaran(Integer asalProdukId) {
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("mataAnggaran",mataAnggaranDao.getMataAnggaran(asalProdukId));
		return result;
	}
	
}
