package info.bukova.isspst.reporting;

import com.mysql.jdbc.StringUtils;
import info.bukova.isspst.data.Address;
import info.bukova.isspst.data.AuthItem;
import info.bukova.isspst.data.Order;
import info.bukova.isspst.data.TripBill;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.UserSettingsData;
import info.bukova.isspst.services.requirement.TripRequirementService;
import info.bukova.isspst.services.settings.GlobalSettingsService;
import info.bukova.isspst.services.tripbill.TripBillApprovalService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.storage.FileStorage;
import net.sf.jasperreports.engine.JRParameter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class ParamFiller {
	
	@Autowired
	private ReportDefinition definition;
	@Autowired
	private UserService userService;
	@Autowired
	private FileStorage storage;
	@Autowired
	private TripRequirementService tripReqService;
	@Autowired
	private GlobalSettingsService settingService;
	@Autowired
	private TripBillApprovalService tripBillApprovalService;
	@Autowired
	private HttpServletRequest req;
	
	public void fill() {
		if (definition.getDataSet() == null || definition.getDataSet().isEmpty()) {
			return;
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		for (String paramKey : definition.getParams().keySet()) {
			if (paramKey.startsWith("SET_")) {
				params.put(paramKey, definition.getParam(paramKey));
			}
		}
		
		definition.getParams().clear();
		definition.getParams().putAll(params);
		
		if ((definition.getDataSet().get(0) instanceof TripBill) 
				&& definition.getReport().isSingleRecord()) {

			if (userService.getUserSettings().getSignatureFile() != null 
					&& !userService.getUserSettings().getSignatureFile().isEmpty()) {
				definition.setParam("P_USER_SIGNATURE", storage.serverPath(userService.getUserSettings().getSignatureFile()));
			}
			
			TripBill tb = (TripBill)definition.getDataSet().get(0);
			tripReqService.loadAuthItems(tb.getRequirement());

			if (tb.getApproval() != null) {
				tripBillApprovalService.loadAuthItems(tb.getApproval());
			}

			if (tb.getApproval() != null && tb.getApproval().getAuthorization() != null && !tb.getApproval().getAuthorization().isEmpty()) {

				AuthItem lastButOneAuth = tb.getApproval().getAuthorization().get(0);
				definition.setParam("P_PREV_APPROVE_DATE", lastButOneAuth.getAuthDate());

				User lastButOneUser = lastButOneAuth.getApprover();
				UserSettingsData prevApproverSettings = userService.getUserSettings(lastButOneUser);

				if (prevApproverSettings != null && !StringUtils.isNullOrEmpty(prevApproverSettings.getSignatureFile())) {
					definition.setParam("P_PREV_APPROVER_SIGNATURE", storage.serverPath(prevApproverSettings.getSignatureFile()));
				}

				AuthItem lastAuth = tb.getApproval().getAuthorization().get(tb.getApproval().getAuthorization().size() - 1);
				definition.setParam("P_APPROVE_DATE", lastAuth.getAuthDate());

				User u = lastAuth.getApprover();
				UserSettingsData approverSettings = userService.getUserSettings(u);

				if (approverSettings != null && !StringUtils.isNullOrEmpty(approverSettings.getSignatureFile())) {
					definition.setParam("P_APPROVER_SIGNATURE", storage.serverPath(approverSettings.getSignatureFile()));
				}
			}
		}
		
		if ((definition.getDataSet().get(0) instanceof Order)
				&& definition.getReport().isSingleRecord()) {
			if (settingService.getSettings().getStampFile() != null
					&& !settingService.getSettings().getStampFile().isEmpty()) {
				definition.setParam("P_STAMP", storage.serverPath(settingService.getSettings().getStampFile()));
			}
		}

		definition.setParam("P_LOGO", storage.serverPath(settingService.getSettings().getLogoFile()));
		definition.setParam(JRParameter.REPORT_LOCALE, req.getLocale());

		Address mainAddress = settingService.getSettings().getMainAddress();

		if (mainAddress != null) {
			String addr = (StringUtils.isNullOrEmpty(mainAddress.getCompany()) ? "" : mainAddress.getCompany());
			definition.setParam("P_MAIN_ADDRESS", addr);
		}
	}

}
