package sevenzip

import (
	"bytes"
	"firefly-launcher/pkg/constant"
	"fmt"
	"os"
	"os/exec"
	"strings"
)

func IsFileIn7z(archivePath, fileInside string) (bool, error) {
	cmd := exec.Command(constant.Tool7zaExe.String(), "l", archivePath)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &out

	if err := cmd.Run(); err != nil {
		return false, fmt.Errorf("7za list failed: %v\nOutput: %s", err, out.String())
	}

	lines := strings.Split(out.String(), "\n")
	for _, line := range lines {
		if strings.Contains(line, fileInside) {
			return true, nil
		}
	}

	return false, fmt.Errorf("%s not found in %s", fileInside, archivePath)
}

func ExtractAFileFromZip(archivePath, fileInside, outDir string) error {
	cmd := exec.Command(constant.Tool7zaExe.String(), "e", archivePath, fileInside, "-o"+outDir, "-y")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func ExtractAllFilesFromZip(archivePath, outDir string) error {
	cmd := exec.Command(constant.Tool7zaExe.String(), "x", archivePath, "-o"+outDir, "-y")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}
