package info.bukova.isspst.services.tripbill;

import info.bukova.isspst.Constants;
import info.bukova.isspst.Module;
import info.bukova.isspst.ModuleUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.data.RequirementState;
import info.bukova.isspst.data.TripBill;
import info.bukova.isspst.data.TripBillApproval;
import info.bukova.isspst.services.IsspstException;
import info.bukova.isspst.services.requirement.RequirementBaseServiceImpl;
import info.bukova.isspst.services.requirement.RequirementTypeService;
import info.bukova.isspst.services.signeddocs.SignedDocumentService;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Pepa Rokos
 */
public class TripBillApprovalServiceImpl extends RequirementBaseServiceImpl<TripBillApproval> implements TripBillApprovalService {

	@Autowired
	private RequirementTypeService reqTypeService;
	@Autowired
	private TripBillService tripBillService;
	@Autowired
	private SignedDocumentService signedDocumentService;

	@Override
	public TripBillApproval createApproval(TripBill bill) {
		if (bill.getApproval() != null) {
			throw new IsspstException("Approval already exists");
		}

		TripBillApproval approval = new TripBillApproval();
		approval.setCentre(bill.getRequirement().getCentre());
		approval.setWorkgroup(bill.getRequirement().getWorkgroup());
		approval.setSeason(bill.getSeason());
		approval.setReqDate(new Date());
		approval.setType(reqTypeService.getTypeById(Constants.REQTYPE_BUSINESSTRIP));
		approval.setState(RequirementState.NEW);
		// approval.setNumser(bill.getRequirement().getNumser());
		approval.setDescription(StringUtils.localize("TravelOrdersFormTitle") + " \"" + bill.getOwnedBy() + "\" - " + bill.getRequirement().getDescription());
		approval.setBill(bill);
		bill.setApproval(approval);
		return approval;
	}

	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_ADD')")
	public void add(TripBillApproval entity) {
		entity.setCreated(new Date());
		entity.setOwnedBy(getLoggedInUser());
		addWorkflow(entity);
		dao.add(entity);

		postAdd(entity);
	}

	@Override
	protected DataModel entityForSignReport(TripBillApproval entity) {
		return entity.getBill();
	}

	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_DELETE') or this.canApprove(#bill.approval)")
	public void cancelApproval(TripBill bill) {
		bill.setApproval(null);
		signedDocumentService.deleteForEntity(bill);
		tripBillService.update(bill);
	}

	@Override
	public Module getModule() {
		return ModuleUtils.getModule(Constants.MOD_TRIPREQUIREMENTS);
	}

	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_DELETE') or hasPermission(#entity, this.getDeleteEntityPermission())")
	public void delete(TripBillApproval entity) {
		TripBill bill = entity.getBill();

		if (bill == null) {
			Query q = queryDao.getQuery("FROM TripBill WHERE APPROVAL_ID = :appId");
			q.setInteger("appId", entity.getId());
			bill = (TripBill) q.uniqueResult();
		}

		if (bill != null) {
			dao.delete(entity);
			cancelApproval(bill);
		} else {
			super.delete(entity);
		}
	}
}
