package com.foomoo.awf.processors;

import com.foomoo.awf.onedrive.OneDriveService;
import com.foomoo.awf.pojo.Referral;
import com.foomoo.awf.render.PdfRenderer;
import com.foomoo.awf.render.XmlReferralRenderer;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;

@Service
public class OneDriveHandler {
    @Autowired
    OneDriveService oneDriveService;

    public void handleSubmission(final Referral referral, final Collection<MultipartFile> multipartFiles) {

        final XmlReferralRenderer xmlReferralRenderer = new XmlReferralRenderer();
        final String referralXml = xmlReferralRenderer.render(referral);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PdfRenderer pdfRenderer = new PdfRenderer();
        pdfRenderer.render(referralXml, baos);

        final ZonedDateTime submissionDateTime = referral.getSubmissionDateTime();
        final String formattedSubmissionDateTime = submissionDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        final String folderName = referral.getApplicantName() + " - " + formattedSubmissionDateTime;
        final String fileName = referral.getApplicantName() + ".pdf";
        writeFileToFolder(folderName, fileName, baos.toByteArray());

        for (final MultipartFile mf : multipartFiles) {
            try {
                writeFileToFolder(folderName, mf.getOriginalFilename(), mf.getBytes());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void writeFileToFolder(final String folderName, final String fileName, final byte[] content) {
        oneDriveService.writeFile(stripFilename(folderName) + "/" + stripFilename(fileName), content);
    }

    private String stripFilename(String filename) {
        final String[] invalidSequences = {"~", "\"", "#", "%", "&", "*", ":", "<", ">", "?", "/", "\\", "{", "|", "}"};
        for (String invalidSequence : invalidSequences) {
            filename = StringUtils.remove(filename, invalidSequence);
        }
        return filename;
    }
}

@@ -505,16 +505,79 @@
 eption;%0A
+import java.net.URI;%0Aimport java.nio.charset.StandardCharsets;%0A
 import j
@@ -660,26 +660,100 @@
 va.util.
-Collection
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.UUID
 ;%0A%0A@Serv
@@ -1993,24 +1993,739 @@
  %7D%0A        %7D
+%0A%0A        // Write any url files.%0A%0A        final List%3CURI%3E links = new ArrayList%3C%3E();%0A        if (StringUtils.isNotBlank(referral.getMusicLink1().getPath())) %7B%0A            final URI link = referral.getMusicLink1();%0A            final String content = %22%5BInternetShortcut%5D%5CnURL=%22 + link + %22%5Cn%22;%0A            writeFileToFolder(folderName,  %22link1.url%22, content.getBytes(StandardCharsets.UTF_8));%0A        %7D%0A        if (StringUtils.isNotBlank(referral.getMusicLink2().getPath())) %7B%0A            final URI link = referral.getMusicLink2();%0A            final String content = %22%5BInternetShortcut%5D%5CnURL=%22 + link + %22%5Cn%22;%0A            writeFileToFolder(folderName,  %22link2.url%22, content.getBytes(StandardCharsets.UTF_8));%0A        %7D
 %0A    %7D%0A%0A    

Write .url files to OneDrive for links provided in the Referral.