package info.bukova.isspst.services.requirement;

import info.bukova.isspst.Constants;
import info.bukova.isspst.data.*;
import info.bukova.isspst.services.LazyLoader;
import info.bukova.isspst.services.tripbill.TripBillService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import org.hibernate.Hibernate;
import org.hibernate.LazyInitializationException;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public class TripRequirementServiceImpl extends RequirementBaseServiceImpl<TripRequirement>
		implements TripRequirementService, RequirementBaseService<TripRequirement> {
	
	@Autowired
	private RequirementTypeService reqTypeService;
	@Autowired
	private WorkgroupService workgroupService;
	@Autowired
	private TripBillService tripBillService;

	@Override
	protected TripRequirement createEntity() {
		TripRequirement tr = new TripRequirement();
		tr.setReqDate(new Date());
		tr.setType(reqTypeService.getTypeById(Constants.REQTYPE_BUSINESSTRIP));
		tr.setState(RequirementState.NEW);

		return tr;
	}

	@Override
	@Transactional
	@LazyLoader("form")
	public void loadPassangers(TripRequirement entity) {
		try {
			if (entity.getPassengers() == null) {
				throw new LazyInitializationException("");
			}
			
			entity.getPassengers().size();
		} catch (LazyInitializationException ex) {
			TripRequirement e = dao.getById(entity.getId());
			e.getPassengers().size();
			entity.setPassengers(e.getPassengers());	
		}
	}

	@Override
	@Transactional
	public TripBill getTripBill(TripRequirement requirement) {
		Query q = dao.getQuery("from TripBill tb where tb.requirement = :req");
		q.setParameter("req", requirement);
		List<TripBill> result = q.list();

		if (result.size() > 0) {
			return result.get(0);
		}

		return null;
	}

	@Transactional
	@LazyLoader("form")
	public void loadAttachments(TripRequirement entity) {
		TripRequirement e = dao.getById(entity.getId());
		Hibernate.initialize(e.getAttachedFiles());
		entity.setAttachedFiles(e.getAttachedFiles());
	}

	@Override
	protected void postApprove(TripRequirement entity) {
		if (entity.getState() == RequirementState.APPROVED) {
			TripBill bill = tripBillService.createTripBill(entity);
			tripBillService.add(bill);
			bill.setOwnedBy(entity.getOwnedBy());
			tripBillService.update(bill);
			
			for (User u : entity.getPassengers()) {
				if (!u.equals(entity.getOwnedBy())) {
					TripBill passBill = tripBillService.createTripBill(entity);
					tripBillService.add(passBill);
					passBill.setOwnedBy(u);
					tripBillService.update(passBill);
				}
			}
		}
	}

	@Override
	protected String getNumberSerie() {
		String currentNumber = "";
		
		if (getNumberSeriesService() != null)
		{
			NumberSeries ns = getNumberSeriesService().getNumberSerie(Constants.MOD_REQUIREMENTS);
			
			if (ns != null)
			{
				currentNumber = ns.getCurrentNumber();
				getNumberSeriesService().increase(ns);
			}
		}
		
		return currentNumber;
	}

}
