package org.hzhub.factory;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hzhub.constant.FileTypeConstants;
import org.hzhub.service.knowledge.ResourceLoader;
import org.hzhub.service.knowledge.impl.loader.*;
import org.hzhub.service.knowledge.impl.split.*;
import org.springframework.stereotype.Component;

@AllArgsConstructor
@Component
public class ResourceLoaderFactory {
    private final CharacterTextSplitter characterTextSplitter;
    private final CodeTextSplitter codeTextSplitter;
    private final MarkdownTextSplitter markdownTextSplitter;
    private final ExcelTextSplitter excelTextSplitter;

    public ResourceLoader getLoaderByFileType(String fileType) {
        fileType = StringUtils.removeStart(fileType, ".");
        if (FileTypeConstants.isTextFile(fileType)) {
            return new TextFileLoader(characterTextSplitter);
        } else if (FileTypeConstants.isWord(fileType)) {
            return new WordLoader(characterTextSplitter);
        } else if (FileTypeConstants.isPdf(fileType)) {
            return new PdfFileLoader(characterTextSplitter);
        } else if (FileTypeConstants.isMdFile(fileType)) {
            return new MarkDownFileLoader(markdownTextSplitter);
        } else if (FileTypeConstants.isCodeFile(fileType)) {
            return new CodeFileLoader(codeTextSplitter);
        } else if (FileTypeConstants.isExcel(fileType)) {
            return new ExcelFileLoader(excelTextSplitter);
        } else {
            return new TextFileLoader(characterTextSplitter);
        }
    }
}
