package com.jasamedika.medifirst2000.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
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.KirimBarangDao;
import com.jasamedika.medifirst2000.entities.KirimBarang;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.PermintaanBarangDariRuangan;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.KirimBarangService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.KirimBarangVO;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.PermintaanBarangDariRuanganVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("kirimBarangService")
public class KirimBarangServiceImpl implements KirimBarangService {

	@Autowired
	private KirimBarangDao kirimBarangDao;

	@Autowired
	private BaseConverterImpl<KirimBarangVO, KirimBarang> kirimBarangConverter;

	@Autowired
	private BaseConverterImpl<PermintaanBarangDariRuanganVO, PermintaanBarangDariRuangan> permintaanBarangDariRuanganConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Autowired
	private BaseConverterImpl<LoginUserVO, LoginUser> loginUserConverter;

	@Override
	public Map<String, Object> saveKirimBarang(KirimBarangVO vo) {

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

		try {
			System.out.println("=======START SAVE KIRIM BARANG=======");

			KirimBarang kirimBarang = kirimBarangConverter.transferVOToModel(vo, new KirimBarang());
			kirimBarang.setKodeBarang(generateKodeBarang());

			// hitung total harga barang yang dikirim
			Double hargaSatuan = kirimBarang.getHargaSatuan();
			Integer kirim = kirimBarang.getKirim();
			Double totalHarga = hargaSatuan * kirim;
			kirimBarang.setTotalHarga(totalHarga);

			// get no order, tgl order, nama barang, stok
			if (CommonUtil.isNotNullOrEmpty(vo.getPermintaanBarangDariRuangan())) {
				kirimBarang.setPermintaanBarangDariRuangan(permintaanBarangDariRuanganConverter
						.transferVOToModel(vo.getPermintaanBarangDariRuangan(), new PermintaanBarangDariRuangan()));
			}

			// get ruangan pemesan & set ruangan penerima
			if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
				kirimBarang.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
			}

			// get & set nama pemesan
			if (CommonUtil.isNotNullOrEmpty(vo.getNamaPemesan())) {
				kirimBarang.setNamaPemesan(loginUserConverter.transferVOToModel(vo.getNamaPemesan(), new LoginUser()));
			}

			KirimBarang resultModel = kirimBarangDao.save(kirimBarang);
			result.put("noRec", resultModel.getNoRec());
			System.out.println("=======END SAVE KIRIM BARANG=======");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> saveKirimBarangBaru(KirimBarangVO vo) {

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

		try {
			System.out.println("=======START SAVE KIRIM BARANG BARU=======");

			KirimBarang kirimBarang = kirimBarangConverter.transferVOToModel(vo, new KirimBarang());
			kirimBarang.setKodeBarang(generateKodeBarang());
			kirimBarang.setNoOrder(generateNoOrderByDayMonthAndYear());

			// hitung total harga barang yang dikirim
			Double hargaSatuan = kirimBarang.getHargaSatuan();
			Integer kirim = kirimBarang.getKirim();
			Double totalHarga = hargaSatuan * kirim;
			kirimBarang.setTotalHarga(totalHarga);

			// get no order, tgl order, nama barang, stok
			if (CommonUtil.isNotNullOrEmpty(vo.getPermintaanBarangDariRuangan())) {
				kirimBarang.setPermintaanBarangDariRuangan(permintaanBarangDariRuanganConverter
						.transferVOToModel(vo.getPermintaanBarangDariRuangan(), new PermintaanBarangDariRuangan()));
			}

			// get ruangan pemesan & set ruangan penerima
			if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
				kirimBarang.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
			}

			// get & set nama pemesan
			if (CommonUtil.isNotNullOrEmpty(vo.getNamaPemesan())) {
				kirimBarang.setNamaPemesan(loginUserConverter.transferVOToModel(vo.getNamaPemesan(), new LoginUser()));
			}

			KirimBarang resultModel = kirimBarangDao.save(kirimBarang);
			result.put("noRec", resultModel.getNoRec());
			System.out.println("=======END SAVE KIRIM BARANG BARU=======");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String generateKodeBarang() {

		Integer countByKodeBarang = 0;
		String kodeBarang = null;

		countByKodeBarang = kirimBarangDao.getCountByKodeBarang();
		countByKodeBarang++;

		if (CommonUtil.isNotNullOrEmpty(countByKodeBarang)) {
			kodeBarang = "000" + countByKodeBarang.toString();

		}

		return kodeBarang;
	}

	@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 = kirimBarangDao.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;
	}

}
