package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

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

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PenyusunanEPlanningDao;
import com.jasamedika.medifirst2000.dao.custom.PenyusunanEPlanningDaoCustom;
import com.jasamedika.medifirst2000.entities.PenyusunanDetailEPlanning;
import com.jasamedika.medifirst2000.entities.PenyusunanEPlanning;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.UnitKerja;
import com.jasamedika.medifirst2000.service.PenyusunanEPlanningService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PenyusunanDetailEPlanningVO;
import com.jasamedika.medifirst2000.vo.PenyusunanEPlanningVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;
import com.jasamedika.medifirst2000.vo.UnitKerjaVO;

@Service("PenyusunanEPlanningService")
public class PenyusunanEPlanningServiceImpl implements PenyusunanEPlanningService {

	@Autowired
	private PenyusunanEPlanningDaoCustom daoCustom;

	@Autowired
	private PenyusunanEPlanningDao dao;

	@Autowired
	private BaseConverterImpl<PenyusunanEPlanningVO, PenyusunanEPlanning> penyusunanEPlanningConverter;

	@Autowired
	private BaseConverterImpl<PenyusunanDetailEPlanningVO, PenyusunanDetailEPlanning> penyusunanDetailEPlanningConverter;

	@Autowired
	private BaseConverterImpl<ProdukVO, Produk> produkConverter;

	@Autowired
	private BaseConverterImpl<UnitKerjaVO, UnitKerja> unitKerjaConverter;

	@Override
	public Map<String, Object> getPenyusunanEPlanning(Integer page, Integer limit, String sort, String dir,
			String isVerifikasi) {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("=======START GET PENYUSUNAN E-PLANNING=======");

			int totalRow = daoCustom.findCount(isVerifikasi);
			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;

			result.put("result", daoCustom.getPenyusunanEPlanning(rowStart, rowEnd, sort, dir, isVerifikasi));
			result.put("totalPages", totalPages);
			result.put("totalRow", totalRow);
			System.out.println("=======END GET PENYUSUNAN E-PLANNING=======");
		} catch (Exception e) {

		}
		return result;
	}

	@Override
	public Map<String, Object> getMataAnggaran(Integer id) {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("=======START GET MATA ANGGARAN=======");
			result.put("result", daoCustom.getMataAnggaran(id));
			System.out.println("=======END GET MATA ANGGARAN=======");
		} catch (Exception e) {

		}
		return result;
	}

	@Override
	public Map<String, Object> savePenyusunanEPlanning(PenyusunanEPlanningVO vo) {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("=======START SAVE PENYUSUNAN E-PLANNING=======");
			PenyusunanEPlanning ePlanning = penyusunanEPlanningConverter.transferVOToModel(vo,
					new PenyusunanEPlanning());
			ePlanning.setStatus(vo.getStatus());
			ePlanning.setTglUsulan(DateUtil.getShortDate());
			ePlanning.setPeriodeAnggaran(vo.getPeriodeAnggaran());
			if (CommonUtil.isNotNullOrEmpty(vo.getUnitKerja())) {
				ePlanning.setUnitKerja(unitKerjaConverter.transferVOToModel(vo.getUnitKerja(), new UnitKerja()));
			}
			// set detail penyusunan e-planning
			if (CommonUtil.isNotNullOrEmpty(vo.getPenyusunanDetailEPlanningSet())) {
				Set<PenyusunanDetailEPlanning> listPenyusunanDetailEPlanning = new HashSet<PenyusunanDetailEPlanning>();
				for (PenyusunanDetailEPlanningVO modelMap : vo.getPenyusunanDetailEPlanningSet()) {
					PenyusunanDetailEPlanning detail = penyusunanDetailEPlanningConverter.transferVOToModel(modelMap,
							new PenyusunanDetailEPlanning());
					if (CommonUtil.isNotNullOrEmpty(modelMap.getProduk())) {
						detail.setProduk(produkConverter.transferVOToModel(modelMap.getProduk(), new Produk()));
					}
					detail.setHarga(modelMap.getHarga());
					detail.setQty(modelMap.getQty());
					detail.setKeterangan(modelMap.getKeterangan());
					detail.setTotal(modelMap.getHarga() * modelMap.getQty());
					detail.setPenyusunanEPlanning(ePlanning);
					listPenyusunanDetailEPlanning.add(detail);
				}
				ePlanning.setPenyusunanDetailEPlanningSet(listPenyusunanDetailEPlanning);
			}
			PenyusunanEPlanning resultModel = dao.save(ePlanning);
			result.put("noRec", resultModel.getNoRec());
			System.out.println("=======END SAVE PENYUSUNAN E-PLANNING=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
