package io.freefair.gradle.plugins.compress.tasks;

import lombok.Getter;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipParameters;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.workers.WorkerExecutor;

import javax.inject.Inject;
import java.io.BufferedOutputStream;
import java.io.IOException;

@SuppressWarnings("UnstableApiUsage")
@Getter
public class GZip extends CompressorTask<GZip.GZipWorkParameters> {

    @Input
    @Optional
    private final Property<Integer> compressionLevel = getProject().getObjects().property(Integer.class);

    @Inject
    public GZip(WorkerExecutor workerExecutor) {
        super(workerExecutor);
        getFileExtension().convention("gz");
    }

    @Override
    protected Class<GZipWorkAction> getWorkAction() {
        return GZipWorkAction.class;
    }

    @Override
    protected void fillParameters(GZipWorkParameters parameters) {
        parameters.getCompressionLevel().set(getCompressionLevel());
    }

    public interface GZipWorkParameters extends CompressorWorkParameters {
        Property<Integer> getCompressionLevel();
    }

    public static abstract class GZipWorkAction extends CompressorWorkAction<GzipCompressorOutputStream, GZipWorkParameters> {

        @Override
        protected GzipCompressorOutputStream createOutputStream(BufferedOutputStream outputStream) throws IOException {
            GzipParameters parameters = new GzipParameters();
            if (getParameters().getCompressionLevel().isPresent()) {
                parameters.setCompressionLevel(getParameters().getCompressionLevel().get());
            }
            parameters.setFilename(getParameters().getSourceFile().getAsFile().get().getName());
            return new GzipCompressorOutputStream(outputStream, parameters);
        }
    }
}

@@ -713,16 +713,268 @@
 lass);%0A%0A
+    @Input%0A    @Optional%0A    private final Property%3CString%3E comment = getProject().getObjects().property(String.class);%0A%0A    @Input%0A    private final Property%3CBoolean%3E addFilename = getProject().getObjects().property(Boolean.class).convention(false);%0A%0A
     @Inj
@@ -977,16 +977,16 @@
 @Inject%0A
-
     publ
@@ -1367,24 +1367,134 @@
 onLevel());%0A
+        parameters.getComment().set(getComment());%0A        parameters.getAddFilename().set(getAddFilename());%0A
     %7D%0A%0A    p
@@ -1611,16 +1611,101 @@
 Level();
+%0A%0A        Property%3CString%3E getComment();%0A%0A        Property%3CBoolean%3E getAddFilename();
 %0A    %7D%0A%0A
@@ -2028,24 +2028,25 @@
 rameters();%0A
+%0A
             
@@ -2205,24 +2205,248 @@
           %7D%0A
+%0A            if (getParameters().getComment().isPresent()) %7B%0A                parameters.setComment(getParameters().getComment().get());%0A            %7D%0A%0A            if (getParameters().getAddFilename().getOrElse(false)) %7B%0A    
             
@@ -2526,24 +2526,39 @@
 getName());%0A
+            %7D%0A%0A
             

Improve GZip task