package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
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.PermintaanBarangDariRuanganDao;
import com.jasamedika.medifirst2000.entities.PermintaanBarangDariRuangan;
import com.jasamedika.medifirst2000.entities.PermintaanBarangDariRuanganSet;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StatusBarang;
import com.jasamedika.medifirst2000.service.PermintaanBarangDariRuanganService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PermintaanBarangDariRuanganSetVO;
import com.jasamedika.medifirst2000.vo.PermintaanBarangDariRuanganVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StatusBarangVO;

@Service("permintaanBarangDariRuanganService")
public class PermintaanBarangDariRuanganServiceImpl implements PermintaanBarangDariRuanganService {

	@Autowired
	private PermintaanBarangDariRuanganDao permintaanBarangDariRuanganDao;

	@Autowired
	private BaseConverterImpl<PermintaanBarangDariRuanganVO, PermintaanBarangDariRuangan> permintaanBarangDariRuanganConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Autowired
	private BaseConverterImpl<StatusBarangVO, StatusBarang> statusBarangConverter;

	@Autowired
	private BaseConverterImpl<PermintaanBarangDariRuanganSetVO, PermintaanBarangDariRuanganSet> permintaanBarangDariRuanganSetConverter;

	@Override
	public Map<String, Object> savePermintaanBarangDariRuangan(PermintaanBarangDariRuanganVO vo) {

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

		try {
			System.out.println("=======START SAVE PERMINTAAN BARANG DARI RUANGAN=======");
			PermintaanBarangDariRuangan permintaanBarangDariRuangan = permintaanBarangDariRuanganConverter
					.transferVOToModel(vo, new PermintaanBarangDariRuangan());
			permintaanBarangDariRuangan.setNoOrder(generateNoOrderByDayMonthAndYear());
			System.out.println("NO ORDER = " + permintaanBarangDariRuangan.getNoOrder());

			// get ruangan tujuan pesanan
			if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
				permintaanBarangDariRuangan
						.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
			}

			// get status barang (linen)
			if (CommonUtil.isNotNullOrEmpty(vo.getStatusBarang())) {
				permintaanBarangDariRuangan.setStatusBarang(
						statusBarangConverter.transferVOToModel(vo.getStatusBarang(), new StatusBarang()));
			}

			// input permintaan barang
			List<PermintaanBarangDariRuanganSet> listPermintaanBarangDariRuangan = new ArrayList<PermintaanBarangDariRuanganSet>();
			for (PermintaanBarangDariRuanganSetVO permintaanBarangDariRuanganSetVO : vo
					.getPermintaanBarangDariRuanganSet()) {
				PermintaanBarangDariRuanganSet permintaanBarangDariRuanganSet = permintaanBarangDariRuanganSetConverter
						.transferVOToModel(permintaanBarangDariRuanganSetVO, new PermintaanBarangDariRuanganSet());
				permintaanBarangDariRuanganSet.setPermintaanBarangDariRuangan(permintaanBarangDariRuangan);
				listPermintaanBarangDariRuangan.add(permintaanBarangDariRuanganSet);
			}
			permintaanBarangDariRuangan.getPermintaanBarangDariRuanganSet().clear();
			permintaanBarangDariRuangan.getPermintaanBarangDariRuanganSet().addAll(listPermintaanBarangDariRuangan);

			PermintaanBarangDariRuangan resultModel = permintaanBarangDariRuanganDao.save(permintaanBarangDariRuangan);
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("noRec", resultModel.getNoRec());
			}
			System.out.println("=======END SAVE PERMINTAAN BARANG DARI RUANGAN=======");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> updatePermintaanBarangDariRuangan(PermintaanBarangDariRuanganVO vo) {

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

		try {
			System.out.println("=======START UPDATE PERMINTAAN BARANG DARI RUANGAN=======");
			PermintaanBarangDariRuangan permintaanBarangDariRuangan = permintaanBarangDariRuanganConverter
					.transferVOToModel(vo, new PermintaanBarangDariRuangan());
			permintaanBarangDariRuangan.setNoOrder(generateNoOrderByDayMonthAndYear());
			System.out.println("NO ORDER = " + permintaanBarangDariRuangan.getNoOrder());

			// get ruangan tujuan pesanan
			if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
				permintaanBarangDariRuangan
						.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
			}

			// get status barang (linen)
			if (CommonUtil.isNotNullOrEmpty(vo.getStatusBarang())) {
				permintaanBarangDariRuangan.setStatusBarang(
						statusBarangConverter.transferVOToModel(vo.getStatusBarang(), new StatusBarang()));
			}

			// input permintaan barang
			List<PermintaanBarangDariRuanganSet> listPermintaanBarangDariRuangan = new ArrayList<PermintaanBarangDariRuanganSet>();
			for (PermintaanBarangDariRuanganSetVO permintaanBarangDariRuanganSetVO : vo
					.getPermintaanBarangDariRuanganSet()) {
				PermintaanBarangDariRuanganSet permintaanBarangDariRuanganSet = permintaanBarangDariRuanganSetConverter
						.transferVOToModel(permintaanBarangDariRuanganSetVO, new PermintaanBarangDariRuanganSet());
				permintaanBarangDariRuanganSet.setPermintaanBarangDariRuangan(permintaanBarangDariRuangan);
				listPermintaanBarangDariRuangan.add(permintaanBarangDariRuanganSet);
			}
			permintaanBarangDariRuangan.getPermintaanBarangDariRuanganSet().clear();
			permintaanBarangDariRuangan.getPermintaanBarangDariRuanganSet().addAll(listPermintaanBarangDariRuangan);

			PermintaanBarangDariRuangan resultModel = permintaanBarangDariRuanganDao.save(permintaanBarangDariRuangan);
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("noRec", resultModel.getNoRec());
			}
			System.out.println("=======END UPDATE PERMINTAAN BARANG DARI RUANGAN=======");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String generateNoOrderByDayMonthAndYear() {

		Date dateNow = new Date();
		Integer day = getDayFromDate(dateNow);
		Integer month = DateUtil.getMonthFromDate(dateNow);
		Integer year = DateUtil.getYearFromDate(dateNow);
		Integer countByDayMonthAndYear = 0;

		countByDayMonthAndYear = permintaanBarangDariRuanganDao.getCountByDayMonthAndYear(day, month, year);

		Integer dayNow = getDayFromDate(dateNow);
		Integer monthNow = DateUtil.getMonthFromDate(dateNow);
		Integer yearNow = DateUtil.getYearFromDate(dateNow);
		String dayNowString = dayNow.toString();
		String monthNowString = monthNow.toString();
		String yearNowString = yearNow.toString().substring(2, 4);
		String countString = null;
		countByDayMonthAndYear++;

		if (CommonUtil.isNotNullOrEmpty(countByDayMonthAndYear)) {
			if (countByDayMonthAndYear.toString().length() == 1) {
				countString = "000" + countByDayMonthAndYear.toString();
			} else if (countByDayMonthAndYear.toString().length() == 2) {
				countString = "000" + countByDayMonthAndYear.toString();
			} else if (countByDayMonthAndYear.toString().length() == 3) {
				countString = "000" + countByDayMonthAndYear.toString();
			} else if (countByDayMonthAndYear.toString().length() == 4) {
				countString = "000" + countByDayMonthAndYear.toString();
			} else if (countByDayMonthAndYear.toString().length() == 5) {
				countString = "000" + countByDayMonthAndYear.toString();
			}
		}

		if ((monthNowString.length()) == 1) {
			monthNowString = "0" + monthNow;
		} else if ((monthNowString.length()) == 2) {
			monthNowString = "0" + monthNow;
		} else if ((monthNowString.length()) == 3) {
			monthNowString = "0" + monthNow;
		} else if ((monthNowString.length()) == 4) {
			monthNowString = "0" + monthNow;
		} else if ((monthNowString.length()) == 5) {
			monthNowString = "0" + monthNow;
		}

		return dayNowString + "" + monthNowString + "" + yearNowString + "" + countString;
	}

	private Integer getDayFromDate(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return day;
	}
}
