package info.bukova.isspst.ui.tripbill;

import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.TripBill;
import info.bukova.isspst.data.TripBillApproval;
import info.bukova.isspst.data.TripBillItem;
import info.bukova.isspst.data.Vehicle;
import info.bukova.isspst.services.settings.GlobalSettingsService;
import info.bukova.isspst.services.tripbill.TripBillApprovalService;
import info.bukova.isspst.services.tripbill.TripBillService;
import info.bukova.isspst.ui.FormWithUpload;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

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

public class TripBillForm extends FormWithUpload<TripBill> {
	
	@WireVariable
	private TripBillService tripBillService;
	private List<Vehicle> vehicles;
	@WireVariable
	private GlobalSettingsService settingsService;
	@WireVariable
	private TripBillApprovalService tripBillApprovalService;

	@Init(superclass = true)
	public void init() {
		vehicles = new ArrayList<Vehicle>();
		vehicles.add(null);
		vehicles.addAll(settingsService.getSettings().getVehicles());
		TripBill bill = getDataBean();

		if (bill.getResultMessageDate() == null) {
			bill.setResultMessageDate(new Date());
		}
	}

	@Command
	@NotifyChange("dataBean")
	public void calculate() {
		tripBillService.calculate(getDataBean());
	}

	public List<Vehicle> getVehicles() {
		return vehicles;
	}

	public boolean isBillDisabled() {
		if (getDataBean().getApproval() != null) {
			return true;
		}

		return false;
	}

	@Command
	@NotifyChange("errMessages")
	public void saveForApproval(@BindingParam("window") Window win)
	{
		if (!canSaveForApproval()) {
			return;
		}

		final Window editWin = win;

		if (getDataBean().getApproval() == null && !isBillDisabled()) {
			Messagebox.show(StringUtils.localize("TripBillSaveApprove"), StringUtils.localize("TripBillSave"), Messagebox.YES
					| Messagebox.NO, Messagebox.QUESTION, new EventListener<Event>() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (((Integer) event.getData()).intValue() == Messagebox.YES) {
						TripBillApproval approval = tripBillApprovalService.createApproval(getDataBean());
						tripBillApprovalService.add(approval);
						TripBillForm.super.save(editWin);
						BindUtils.postGlobalCommand(null, null, "refresh", null);
					} else {
						TripBillForm.super.save(editWin);
					}
				}
			});
		} else {
			super.save(win);
		}

	}

	private boolean canSaveForApproval() {
		if (StringUtils.isNullOrTrimmedEmpty(this.getDataBean().getResultMessage())) {
			Messagebox.show(StringUtils.localize("ErrFillTripBillResultMessageText"), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR);
			return false;
		}

		if (getDataBean().getBillItems() != null && !getDataBean().getBillItems().isEmpty()) {
			TripBillItem first = getDataBean().getBillItems().get(0);
			TripBillItem last = getDataBean().getBillItems().get(getDataBean().getBillItems().size() - 1);

			if (first.getToArrival() == null || first.getToDeparture() == null
					|| last.getBackArrival() == null || last.getBackDeparture() == null) {
				Messagebox.show(StringUtils.localize("ErrFillTripBillResultTimes"), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR);
				return false;
			}
		}

		return true;
	}

	@Override
	protected void doSave() {
		maintainAttachment();
		tripBillService.updateOwned(getDataBean());
	}
}
