import com.dySweetFishPlugin.tool.lang.Holder
import org.apache.logging.log4j.Logger

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Stream

class Ignore_ReadUTF8File {

    static String execute(Path scriptFile, boolean trimed, Logger logger) {
        final StringBuilder buf = new StringBuilder()

        try {
            Integer iIndex = 0
            Holder<Integer> prefix = Holder.of(iIndex)
            try (Stream<String> lines = Files.lines(scriptFile, StandardCharsets.UTF_8)) {
                lines.each { line ->
                    line = (trimed) ? line.trim() : line
                    if (!line.isEmpty()) {
                        if (prefix.get() == 0) {
                            buf.append((trimed) ? line.trim() : line)
                        } else {
                            buf.append("\n").append((trimed) ? line.trim() : line)
                        }

                        prefix.set(1)
                    } else {
                        if (!trimed) buf.append("\n")
                    }
                }
            }

        } catch (IOException e) {
            logger.error(e.getMessage(), e)
        }

        return buf.toString()
    }
}
