package connection

import (
	"os/exec"

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

type Connection struct {
	adb    adb.Adb
	device adb.Device
}

func (c Connection) Adb() adb.Adb {
	return c.adb
}

func (c Connection) Device() adb.Device {
	return c.device
}

// Install installs apk
func (c Connection) Install(apkFile string, flags ...string) ([]byte, error) {
	fc := append(flags, apkFile)
	return c.RunSync("install", fc...)
}

// Uninstall uninstalls package
func (c Connection) Uninstall(packageName string) ([]byte, error) {
	return c.RunSync("uninstall", packageName)
}

// Pull pulls files from device to host
func (c Connection) Pull(onDevice, onHost string) ([]byte, error) {
	return c.RunSync("pull", onDevice, onHost)
}

// Push pushes or syncs file
func (c Connection) Push(onDevice, onHost string, sync bool) ([]byte, error) {
	sFlag := ""
	if sync {
		sFlag = "--sync"
	}
	return c.RunSync("push", sFlag, onHost, onDevice)
}

// LogcatClear clears logcat buffer
func (c Connection) LogcatClear() ([]byte, error) {
	return c.RunSync("logcat", "-c")
}

// LogcatDump dumps logcat buffer
func (c Connection) LogcatDump() ([]byte, error) {
	return c.RunSync("logcat", "-d", "-v", "threadtime")
}

// LogcatStartToFileAsync starts saving logcat to filePath
func (c Connection) LogcatStartToFileAsync(filePath string) (*exec.Cmd, error) {
	return c.StartAsync("logcat", "-v", "threadtime", "-f", filePath)
}

// LogcatStop interrupts logcat process with timeSleep timeout in seconds
func (c Connection) LogcatStop(process *exec.Cmd) error {
	return c.StopAsync(process)
}
