package apksigner

import (
	"fmt"

	"notabug.org/Umnik/GoAndroidSDK/v2/components/executor"
)

func (a Apksigner) BinPath() string {
	return a.binPath
}

// SignApk signs apk file
func (a Apksigner) SignApk(in, out string, verity, allowDebug, verbose bool, signer SignerDN) ([]byte, error) {
	var args []string
	if verbose {
		args = append(args, "-v")
	}
	args = append(args, "--in", in, "--out", out)
	if verity {
		args = append(args, "--verity-enabled", "true")
	}
	if !allowDebug {
		args = append(args, "--debuggable-apk-permitted", "false")
	}
	for i := 1; i <= 4; i++ {
		args = append(args, fmt.Sprintf("--v%d-signing-enabled", i), "true")
	}
	args = append(args, "--v1-signer-name", signer.SignerName, "--key", signer.KeyPath, "--cert", signer.CertPath)
	return executor.RunSync(a, "sign", args...)
}

// DumpSignatureInfo returns signature info. See ParseDumpSignatureInfoFromBytes
func (a Apksigner) DumpSignatureInfo(apkFile string, noV1 bool) ([]byte, error) {
	args := []string{"--verbose", "--print-certs"}
	if noV1 {
		args = append(args, "--min-sdk-version", "24")
	}
	args = append(args, apkFile)
	return executor.RunSync(a, "verify", args...)
}
