package tests

import (
	"archive/zip"
	"io"
	"os"
	"testing"

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

func newSdk(t *testing.T, fnName string) *sdk.SDK {
	s, err := sdk.NewSDK()
	if !testErr(t, fnName, err) {
		return nil
	}
	return s
}

func closeOsFile(t *testing.T, fnName string, closer *os.File) {
	t.Helper()
	testErr(t, fnName, closer.Close())
}

func closeZipFile(t *testing.T, fnName string, closer *zip.ReadCloser) {
	t.Helper()
	testErr(t, fnName, closer.Close())
}

func removeOsFile(t *testing.T, fnName string, fileName string) {
	testErr(t, fnName, os.Remove(fileName))
}

func makeCopy(t *testing.T, fnName string, dest io.Writer, source string) bool {
	t.Helper()
	src, err := os.Open(source)
	if !testErr(t, fnName, err) {
		return false
	}
	defer closeOsFile(t, fnName, src)
	_, err = io.Copy(dest, src)
	return testErr(t, fnName, err)
}

func testErr(t *testing.T, funcName string, err error) bool {
	if err != nil {
		t.Errorf("%v failed with: %q", funcName, err)
		return false
	}
	return true
}

func goodApkFile() string {
	return "saveto.apk"
}
