package cokr.xit.fims.crdn.receive.eqpmnt;

import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

public class LayoutDiscriminator {

	public static LayoutDescriptor choice(MultipartFile[] fileList, List<LayoutDescriptor> candidates) {

		BiFunction<String, String, Integer> filter = (validType, thisValue) -> {
			for(Iterator<LayoutDescriptor> it = candidates.iterator(); it.hasNext();){
				LayoutDescriptor descriptor = it.next();

				switch (validType) {
					case "fileGroupIsThis": {
						if(!descriptor.getFileGroup().equals(thisValue)){
							it.remove();
						}
						break;
					}
					case "notUseThisFileNameSeperator": {
						if(descriptor.getFileNameSeperator().equals(thisValue)){
							it.remove();
						}
						break;
					}
					case "fileNameLengthIsThis": {
						if(descriptor.getFileNameLength() != null) {
							if(!descriptor.getFileNameLength().equals(thisValue)){
								it.remove();
							}
						}
						break;
					}
					case "fileNameLengthIsNotFixed": {
						if(descriptor.getFileNameLength() != null){
							it.remove();
						}
						break;
					}
				}
			}

			return candidates.size();
		};

		boolean hasTxt = false; //txt파일 포함 여부
		boolean hasJpg = false; //jpg파일 포함 여부
		boolean hasUnderbar = false; //파일명에 언더바 포함 여부
		boolean hasComma = false; //파일명에 콤마 포함 여부
		boolean sameAllFileNameLength = true; //모든 파일명 길이 동일 여부

		int fileNameLengthTemp = 0;
		for(int i=0;i < fileList.length; i++) {

			String fileName = fileList[i].getOriginalFilename();
			String extension = FilenameUtils.getExtension(fileName);
			String noExtensionName= FilenameUtils.removeExtension(fileName);

			if(fileNameLengthTemp == 0) {
				fileNameLengthTemp = fileName.length();
			}

			int fileNameLength = fileName.length();
			if(fileNameLength != fileNameLengthTemp) {
				sameAllFileNameLength = false;
			}

			if(extension.toUpperCase().equals("TXT")) {
				hasTxt = true;
			}
			if(extension.toUpperCase().equals("JPG")) {
				hasJpg = true;
			}

			if(noExtensionName.contains("_")) {
				hasUnderbar = true;
			}
			if(noExtensionName.contains(",")) {
				hasComma = true;
			}
		}

		//
		String fileGroup = "";
		if(hasTxt) {
			fileGroup = "TXT";
		} else if(hasJpg) {
			fileGroup = "JPG";
		} else {
			fileGroup = "BIN";
		}

		if(filter.apply("fileGroupIsThis", fileGroup) == 1) {
			return candidates.get(0);
		}

		if(!hasUnderbar) {
			if(filter.apply("notUseThisSeperator", "_") == 1) {
				return candidates.get(0);
			}
		}

		if(!hasComma) {
			if(filter.apply("notUseThisFileNameSeperator", ",") == 1) {
				return candidates.get(0);
			}
		}

		if(sameAllFileNameLength) {
			MultipartFile sample = fileList[0];
			int nameLength = sample.getOriginalFilename().length();
			if(filter.apply("fileNameLengthIsThis", Integer.toString(nameLength)) == 1) {
				return candidates.get(0);
			}
		} else {
			if(filter.apply("fileNameLengthIsNotFixed", "") == 1) {
				return candidates.get(0);
			}
		}




		if(candidates.size() != 1) {
			throw new RuntimeException("파일의 레이아웃 서식을 구분할 수 없습니다.");
		}

		return candidates.get(0);
	}

}
