package sdk

import (
	"fmt"
	"os"

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

// NewSDKFromPath creates SDK from path to dir
func NewSDKFromPath(dirPath string) (*SDK, error) {
	if err := misc.TestDir(dirPath); err != nil {
		return nil, err
	} else {
		return &SDK{dirPath: dirPath}, nil
	}
}

// NewSDKFromEnv creates SDK from environment variable. For example: ${ANDROID_SDK_ROOT}
func NewSDKFromEnv(env string) (*SDK, error) {
	return NewSDKFromPath(os.ExpandEnv(env))
}

// NewSDK creates SDK from ANDROID_SDK_ROOT and ANDROID_HOME environment variables if it exists
func NewSDK() (*SDK, error) {
	environments := []string{"ANDROID_HOME", "ANDROID_SDK_ROOT"}
	for _, val := range environments {
		if key, ok := os.LookupEnv(val); ok {
			return NewSDKFromEnv(key)
		}
	}
	return nil, fmt.Errorf("following environments was checked: %q. They are not set", environments)
}
