package org.yeastrc.proxl.import_xml_to_db.pre_validate_xml;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;import org.slf4j.Logger;
import org.yeastrc.proxl.import_xml_to_db.exceptions.ProxlImporterDataException;
import org.yeastrc.proxl.import_xml_to_db.objects.ScanFileFileContainer;
import org.yeastrc.proxl_import.api.xml_dto.ProxlInput;
import org.yeastrc.proxl_import.api.xml_dto.Psm;
import org.yeastrc.proxl_import.api.xml_dto.Psms;
import org.yeastrc.proxl_import.api.xml_dto.ReportedPeptide;
import org.yeastrc.proxl_import.api.xml_dto.ReportedPeptides;
import org.yeastrc.xlink.base.file_import_proxl_xml_scans.dto.ProxlXMLFileImportTrackingSingleFileDTO;

/**
 * 
 *
 */
public class ValidateScanFilenamesInXMLAreOnCommandLine {

	private static final Logger log = LoggerFactory.getLogger(  ValidateScanFilenamesInXMLAreOnCommandLine.class );
	private ValidateScanFilenamesInXMLAreOnCommandLine() { }
	public static ValidateScanFilenamesInXMLAreOnCommandLine getInstance() {
		return new ValidateScanFilenamesInXMLAreOnCommandLine();
	}
	
	/**
	 * @param proxlInput
	 * @param scanFileList
	 * @throws ProxlImporterDataException for data errors
	 */
	public void validateScanFilenamesInXMLAreOnCommandLine( 
			ProxlInput proxlInput, List<ScanFileFileContainer> scanFileFileContainerList ) throws ProxlImporterDataException {
		
		if ( scanFileFileContainerList == null || scanFileFileContainerList.isEmpty() ) {
			//  Exit if no scan files provided, which means no scans were chosen
			return;  //  EARLY RETURN
		}
		Set<String> scanFilenamesSet = new HashSet<>();
		for ( ScanFileFileContainer scanFileFileContainer : scanFileFileContainerList ) {
			File scanFile = scanFileFileContainer.getScanFile();
			String scanFilename = scanFile.getName();
			if ( scanFileFileContainer.getScanFileDBRecord() != null ) {
				ProxlXMLFileImportTrackingSingleFileDTO scanFileDBRecord = scanFileFileContainer.getScanFileDBRecord();
				scanFilename = scanFileDBRecord.getFilenameInUpload();
			}
			scanFilenamesSet.add( scanFilename );
		}
		ReportedPeptides reportedPeptides = proxlInput.getReportedPeptides();
		if ( reportedPeptides != null ) {
			List<ReportedPeptide> reportedPeptideList =
					reportedPeptides.getReportedPeptide();
			if ( reportedPeptideList != null && ( ! reportedPeptideList.isEmpty() ) ) {
				for ( ReportedPeptide reportedPeptide : reportedPeptideList ) {
					Psms psms =	reportedPeptide.getPsms();
					if ( psms != null ) {
						List<Psm> psmList = psms.getPsm();
						if ( psmList != null ) {
							for ( Psm psm : psmList ) {
								String scanFileName = psm.getScanFileName();
								if ( StringUtils.isEmpty( scanFileName ) ) {
									if (  scanFileFileContainerList == null || scanFileFileContainerList.isEmpty() ) {
										//   Valid condition
									} else if ( scanFileFileContainerList != null && scanFileFileContainerList.size() != 1 ) {
										String msg = "ERROR: Scan Filename on PSM is empty when there is more than one scan file on the command line.";
										log.error(msg);
										throw new ProxlImporterDataException(msg);
									}
								} else {
									if ( ! scanFilenamesSet.contains( scanFileName ) ) {
										String msg = "Scan Filename on PSM is not in Scan File List on Command Line."
												+ "  Scan Filename on PSM: " + scanFileName;
										log.error(msg);
										throw new ProxlImporterDataException(msg);
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
@@ -3330,30 +3330,36 @@
  msg = %22
-S
+Found s
 can 
-F
+f
 ilename 
 on PSM i
@@ -3354,99 +3354,226 @@
 ame 
-on PSM is not in S
+in Proxl XML file that is different than the s
 can 
-File List on Command Line.%22%0A%09%09%09%09%09%09%09%09%09%09%09%09+ %22  Scan Filename on PSM: %22 
+file(s) uploaded.  Uploaded filename(s): %22 %0A%09%09%09%09%09%09%09%09%09%09%09%09+ StringUtils.join( scanFilenamesSet, %22, %22 )%0A%09%09%09%09%09%09%09%09%09%09%09%09+ %22.  Filename found in Proxl XML file:  %22%0A%09%09%09%09%09%09%09%09%09%09%09%09
 + sc

Improve error message for Scan Filename on PSM not found