package com.jasamedika.medifirst2000.service.impl;

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

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.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PenerimaanLinenDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.dao.SatuanStandarDao;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.MappingCycle;
import com.jasamedika.medifirst2000.entities.MappingPenerimaanLinen;
import com.jasamedika.medifirst2000.entities.PenerimaanLinen;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PenerimaanLinenService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.MappingPenerimaanLinenVO;
import com.jasamedika.medifirst2000.vo.PenerimaanLinenVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;

@Service("penerimaanLinenService")
public class PenerimaanLinenServiceImpl extends BaseVoServiceImpl implements PenerimaanLinenService {

	@Autowired
	private PenerimaanLinenDao penerimaanLinenDao;

	@Autowired
	private SatuanStandarDao satuanStandarDao;

	@Autowired
	private RuanganDao ruanganDao;

	@Autowired
	private BaseConverterImpl<PenerimaanLinenVO, PenerimaanLinen> penerimaanLinenConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Autowired
	private BaseConverterImpl<LoginUserVO, LoginUser> petugasConverter;

	@Autowired
	private BaseConverterImpl<MappingPenerimaanLinenVO, MappingPenerimaanLinen> mappingPenerimaanLinenConverter;

	@Autowired
	private BaseConverterImpl<SatuanStandarVO, SatuanStandar> satuanStandarConverter;

	@Override
	public Map<String, Object> savePenerimaanLinen(PenerimaanLinenVO vo) {

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

		try {
			System.out.println("==================START SAVE PENERIMAAN LINEN======================");

			PenerimaanLinen penerimaanLinen = penerimaanLinenConverter.transferVOToModel(vo, new PenerimaanLinen());

			if (CommonUtil.isNotNullOrEmpty(vo.getPetugas())) {
				penerimaanLinen.setPetugas(petugasConverter.transferVOToModel(vo.getPetugas(), new LoginUser()));
			}

			// set mapping penerimaan linen
			if (CommonUtil.isNotNullOrEmpty(vo.getMapPenerimaanLinen())) {
				Set<MappingPenerimaanLinen> listMapPenerimaanLinen = new HashSet<MappingPenerimaanLinen>();
				for (MappingPenerimaanLinenVO modelMapPenerimaanLinen : vo.getMapPenerimaanLinen()) {
					MappingPenerimaanLinen mappingPenerimaanLinen = mappingPenerimaanLinenConverter
							.transferVOToModel(modelMapPenerimaanLinen, new MappingPenerimaanLinen());
					// set tgl penerimaan linen
					mappingPenerimaanLinen.setTgl(DateUtil.getShortDate());
					// get ruangan
					if (CommonUtil.isNotNullOrEmpty(modelMapPenerimaanLinen.getRuangan())) {
						mappingPenerimaanLinen.setRuangan(ruanganConverter
								.transferVOToModel(modelMapPenerimaanLinen.getRuangan(), new Ruangan()));
					}
					// get satuan standar (kg)
					if (CommonUtil.isNotNullOrEmpty(modelMapPenerimaanLinen.getSatuan())) {
						mappingPenerimaanLinen.setSatuan(satuanStandarConverter
								.transferVOToModel(modelMapPenerimaanLinen.getSatuan(), new SatuanStandar()));
					}
					mappingPenerimaanLinen.setPenerimaanLinen(penerimaanLinen);
					listMapPenerimaanLinen.add(mappingPenerimaanLinen);
				}
				penerimaanLinen.setMapPenerimaanLinen(listMapPenerimaanLinen);
			}

			PenerimaanLinen resultModel = penerimaanLinenDao.save(penerimaanLinen);
			result.put("noRec Penerimaan Linen", resultModel.noRec);
			System.out.println("==================END SAVE PENERIMAAN LINEN======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> findSatuanStandar() {

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

		try {
			System.out.println("==================START FIND SATUAN STANDAR======================");
			List<SatuanStandar> resultModel = satuanStandarDao.findSatuanStandarKg();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("Satuan Standar Penerimaan Linen Ada", true);
			} else {
				result.put("Satuan Standar Penerimaan Linen Tidak Ada", false);
			}
			System.out.println("==================END FIND SATUAN STANDAR======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> findRuangLoundry() {

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

		try {
			System.out.println("==================START FIND RUANG LOUNDRY======================");
			List<Ruangan> resultModel = ruanganDao.findRuangLoundry();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("Ruang Loundry Ada", true);
			} else {
				result.put("Ruang Loundry Tidak Ada", false);
			}
			System.out.println("==================END FIND RUANG LOUNDRY======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> findAll(Integer page, Integer take, String sort, String dir,
			Specification<PenerimaanLinen> 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<PenerimaanLinen> resultPage = penerimaanLinenDao.findAll(spec, pageable);
		List<PenerimaanLinen> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public PenerimaanLinenVO add(PenerimaanLinenVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

	@Override
	public PenerimaanLinenVO findById(String key) throws JpaSystemException {
		return null;
	}

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

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

}
