package com.jasamedika.medifirst2000.service.impl;

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 com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PlanningDiklatHumasMarketDao;
import com.jasamedika.medifirst2000.dao.StrukPlanningDao;
import com.jasamedika.medifirst2000.entities.Dokumen;
import com.jasamedika.medifirst2000.entities.JenisAnggaran;
import com.jasamedika.medifirst2000.entities.JenisTempat;
import com.jasamedika.medifirst2000.entities.Kelas;
import com.jasamedika.medifirst2000.entities.PlanningDiklatHumasMarket;
import com.jasamedika.medifirst2000.entities.PlanningPegawaiMuSaRe;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.entities.RencanaKinerjaOperasional;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukPlanning;
import com.jasamedika.medifirst2000.service.PlanningDiklatHumasMarketService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.DokumenVO;
import com.jasamedika.medifirst2000.vo.JenisAnggaranVO;
import com.jasamedika.medifirst2000.vo.JenisTempatVO;
import com.jasamedika.medifirst2000.vo.KelasVO;
import com.jasamedika.medifirst2000.vo.PlanningDiklatHumasMarketVO;
import com.jasamedika.medifirst2000.vo.PlanningPegawaiMuSaReVO;
import com.jasamedika.medifirst2000.vo.RekananVO;
import com.jasamedika.medifirst2000.vo.RencanaKinerjaOperasionalVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StrukPlanningVO;

@Service("PlanningDiklatHumasMarketService")
public class PlanningDiklatHumasMarketServiceImpl implements PlanningDiklatHumasMarketService {
	@Autowired
	private PlanningDiklatHumasMarketDao planningDiklatHumasMarketDao;
	
	@Autowired
	private StrukPlanningDao strukPlanningDao;
	
	@Autowired
	private BaseConverterImpl<StrukPlanningVO, StrukPlanning> strukPlanningConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<DokumenVO, Dokumen> dokumenConverter;
	
	@Autowired
	private BaseConverterImpl<JenisAnggaranVO, JenisAnggaran> jenisAnggaranConverter;
	
	@Autowired
	private BaseConverterImpl<PlanningDiklatHumasMarketVO, PlanningDiklatHumasMarket> planningDHMConverter;
	
	@Autowired
	private BaseConverterImpl<KelasVO, Kelas> kelasConverter;
	
	@Autowired
	private BaseConverterImpl<RekananVO, Rekanan> rekananConverter;
	
	@Autowired
	private BaseConverterImpl<JenisTempatVO, JenisTempat> jenisTempatConverter;

	@Override
	public String noPlanning() {
		Long count = this.planningDiklatHumasMarketDao.countNoPlanningHistori("HM");
		
		String monthNow = String.valueOf(DateUtil.getMonthFromDate(new Date()));
		String yearNow = String.valueOf(DateUtil.getYear(new Date())).substring(2, 4);
		String digit = null;
		
		count++;
		
		if (count.toString().length() == 1) {
			digit = "000";
		}
		else if (count.toString().length() == 2) {
			digit = "00";
		}
		else if (count.toString().length() == 3) {
			digit = "0";
		}
		if (monthNow.length() == 1)
			monthNow = "0" + monthNow;
		
		return "HM" + yearNow + monthNow + digit + count;
	}

	@Override
	public Map<String, Object> noDefault() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<String> list = this.planningDiklatHumasMarketDao.noPlanning("HM");
		
		if (CommonUtil.isNotNullOrEmpty(list)) {
			Integer data = Integer.parseInt(list.get(list.size() - 1).substring(2)) + 1;
			result.put("noPlanning", noPlanning());
		}
		else {
			result.put("noPlanning", noPlanning());
		}
		return result;
	}

	@Override
	public Map<String, Object> saveStrukPlanning(StrukPlanningVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		StrukPlanning strukPlanning = new StrukPlanning();
		
		String noPlanning = noPlanning();
		Date dateNow = DateUtil.now();
		
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			strukPlanning = strukPlanningConverter.transferVOToModel(vo, new StrukPlanning());
			strukPlanning.setNoplanning(noPlanning);
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getTglplanning())) {
			strukPlanning.setTglplanning(vo.getTglplanning());
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKdruangan())) {
			strukPlanning.setKdruangan(ruanganConverter.transferVOToModel(vo.getKdruangan(), new Ruangan()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKdruanganasal())) {
			strukPlanning.setKdruanganasal(ruanganConverter.transferVOToModel(vo.getKdruangan(), new Ruangan()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKddokumensk())) {
			strukPlanning.setKddokumensk(dokumenConverter.transferVOToModel(vo.getKddokumensk(), new Dokumen()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKdjenisanggaran())) {
			strukPlanning.setKdjenisanggaran(jenisAnggaranConverter.transferVOToModel(vo.getKdjenisanggaran(), new JenisAnggaran()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(strukPlanning)) {
			StrukPlanning strukPlanningResult = this.strukPlanningDao.save(strukPlanning);
			result.put("noRec", strukPlanningResult.getNoRec());
			
			//define array to save planningdiklathumasmarket
			for (PlanningDiklatHumasMarketVO pdhmvo : vo.getPlanningDHM()) {
				PlanningDiklatHumasMarket modPDHM = planningDHMConverter.transferVOToModel(pdhmvo, new PlanningDiklatHumasMarket());
				
				modPDHM.setNoPlanning(noPlanning);
				modPDHM.setKdProdukDHM(pdhmvo.getKdProdukDHM());
				modPDHM.setTglPlanning(pdhmvo.getTglPlanning());
				modPDHM.setKelasPlanDHM(kelasConverter.transferVOToModel(pdhmvo.getKelasPlanDHM(), new Kelas()));
				modPDHM.setRekananPlanDHM(rekananConverter.transferVOToModel(pdhmvo.getRekananPlanDHM(), new Rekanan()));
				modPDHM.setJenisTempatPlanDHM(jenisTempatConverter.transferVOToModel(pdhmvo.getJenisTempatPlanDHM(), new JenisTempat()));
				modPDHM.setQtyPeserta(pdhmvo.getQtyPeserta());
				modPDHM.setDeskripsiDetail(pdhmvo.getDeskripsiDetail());
				
				PlanningDiklatHumasMarket planning = this.planningDiklatHumasMarketDao.save(modPDHM);
				result.put("norecplanning", planning.getNoRec());
				
			}
		}
		
		return result;
		
	}

	@Override
	public Map<String, Object> getListPlanningDHM(String noPlanning) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListPlannDHMByNoPlanning(noPlanning);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListNoPlanningDHM() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListNoPlanningDHM();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getLoadData() {
		Map<String, Object> result = new HashMap<>();

		Map<String, Object> noPlanning = noDefault();
		if (CommonUtil.isNotNullOrEmpty(noPlanning)) {
			result.put("noPlanning", noPlanning.get("noPlanning"));
		}
		return result;
	}
	
	
	@Override
	public Map<String, Object> getListDokumen() {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListDokumenSk();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	
	@Override
	public Map<String, Object> getListJenisAnggaran() {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListJenisAnggaran();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListJenisTempat() {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListJenisTempat();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListRekanan() {
		Map<String, Object> result = new HashMap<>();

		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketDao.getListJenisRekanan();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public String getNoSurat() {
		 
		return noPlanning() ;
	}
	
	@Override
	public Map<String, Object> saveDHMPelaksana(PlanningDiklatHumasMarketVO vo) {
		Map<String, Object> result = new HashMap<>();
		
		PlanningDiklatHumasMarket pd = planningDiklatHumasMarketDao.findOne(vo.getNoRec());
		
		if (CommonUtil.isNotNullOrEmpty(pd)) {
			pd.setTglPlanningExecAwal(vo.getTglPlanningExecAwal());
			pd.setTglPlanningExecAkhir(vo.getTglPlanningExecAkhir());
			pd.setQtyPesertaExec(vo.getQtyPesertaExec());
			pd.setKeteranganExecLainnya(vo.getKeteranganExecLainnya());
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getJenisTempatExec())) {
			pd.setJenisTempatExec(jenisTempatConverter.transferVOToModel(vo.getJenisTempatExec(), new JenisTempat()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(vo.getRuanganExec())) {
			pd.setRuanganExec(ruanganConverter.transferVOToModel(vo.getRuanganExec(), new Ruangan()));
		}
		
		PlanningDiklatHumasMarket pdResult = planningDiklatHumasMarketDao.save(pd);
		result.put("norec", pdResult.getNoRec());


		return result;
	}
	


}
