/*
 * Copyright (C) 2014-2020 CZ.NIC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include "src/datovka_shared/isds/message_interface.h"
#include "src/datovka_shared/log/log.h"
#include "src/records_management/content/automatic_upload_target.h"
#include "src/records_management/content/automatic_upload_target_interaction.h"

QString RecMgmt::uniqueUploadReceived(const Isds::Envelope &env,
	    const RecMgmt::AutomaticUploadTarget &rmTgts)
{
	QString identUpload, refNumUpload;
	QString caseUpload;
	if ((!env.dmRecipientIdent().isEmpty()) &&
	    rmTgts.haveCaseId(env.dmRecipientIdent())) {
		identUpload = rmTgts.hierarchyForCaseId(env.dmRecipientIdent());
	}
	if ((!env.dmRecipientRefNumber().isEmpty()) &&
	    rmTgts.haveCaseId(env.dmRecipientRefNumber())) {
		refNumUpload = rmTgts.hierarchyForCaseId(env.dmRecipientRefNumber());
	}
	if (identUpload == refNumUpload) {
		caseUpload = identUpload;
	} else if ((!identUpload.isEmpty()) && refNumUpload.isEmpty()) {
		caseUpload = identUpload;
	} else if (identUpload.isEmpty() && (!refNumUpload.isEmpty())) {
		caseUpload = refNumUpload;
	} else if ((!identUpload.isEmpty()) && (!refNumUpload.isEmpty())) {
		logWarningNL("%s",
		    "Recipient reference number and file mark point to different upload hierarchy items.");
		return QString();
	}

	identUpload.clear();
	refNumUpload.clear();

	QString courtCaseUpload;
	if ((!env.dmSenderIdent().isEmpty()) &&
	    rmTgts.haveCourtCaseId(env.dmSenderIdent())) {
		identUpload = rmTgts.hierarchyForCourtCaseId(env.dmSenderIdent());
	}
	if ((!env.dmSenderRefNumber().isEmpty()) &&
	    rmTgts.haveCourtCaseId(env.dmSenderRefNumber())) {
		refNumUpload = rmTgts.hierarchyForCourtCaseId(env.dmSenderRefNumber());
	}
	if (identUpload == refNumUpload) {
		courtCaseUpload = identUpload;
	} else if ((!identUpload.isEmpty()) && refNumUpload.isEmpty()) {
		courtCaseUpload = identUpload;
	} else if (identUpload.isEmpty() && (!refNumUpload.isEmpty())) {
		courtCaseUpload = refNumUpload;
	} else if ((!identUpload.isEmpty()) && (!refNumUpload.isEmpty())) {
		logWarningNL("%s",
		    "Sender reference number and file mark point to different upload hierarchy items.");
		return QString();
	}

	if (caseUpload.isEmpty() && courtCaseUpload.isEmpty()) {
		return QString();
	} if ((!caseUpload.isEmpty()) && (!courtCaseUpload.isEmpty())) {
		if (caseUpload == courtCaseUpload) {
			return caseUpload;
		} else {
			logWarningNL("%s",
			    "Recipient and sender identifiers point to different upload hierarchy items.");
			return QString();
		}
	} if (!caseUpload.isEmpty()) {
		return caseUpload;
	} else {
		return courtCaseUpload;
	}
}
