package com.xypower.frpandroid;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class ShellThread extends Thread {

    public interface Function1 {
        Object invoke(Object var1);
    }

    @NotNull
    private final String command;
    @NotNull
    private final String[] envp;
    @NotNull
    private final File dir;
    @NotNull
    private final Function1 outputCallback;


    public ShellThread(@NotNull String command, @NotNull String[] envp, @NotNull File dir, @NotNull Function1 outputCallback) {
        // Intrinsics.checkNotNullParameter(command, "command");
        // Intrinsics.checkNotNullParameter(envp, "envp");
        // Intrinsics.checkNotNullParameter(dir, "dir");
        // Intrinsics.checkNotNullParameter(outputCallback, "outputCallback");
        super();
        this.command = command;
        this.envp = envp;
        this.dir = dir;
        this.outputCallback = outputCallback;
    }

    public void run() {
        try {
            Process process = Runtime.getRuntime().exec(this.command, this.envp, this.dir);
            // Intrinsics.checkNotNullExpressionValue(process, "process");
            InputStream inputStream = process.getInputStream();
            BufferedReader reader = new BufferedReader((Reader)(new InputStreamReader(inputStream)));

            while(true) {
                String var5 = reader.readLine();
                if (var5 == null || this.isInterrupted()) {
                    reader.close();
                    process.destroy();
                    break;
                }

                if (var5 != null) {
                    this.outputCallback.invoke(var5);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    @NotNull
    public final String getCommand() {
        return this.command;
    }

    @NotNull
    public final String[] getEnvp() {
        return this.envp;
    }

    @NotNull
    public final File getDir() {
        return this.dir;
    }

    @NotNull
    public final Function1 getOutputCallback() {
        return this.outputCallback;
    }

}
