package ru.otus.mobile.config;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public final class MobileConfig {
    private final String appiumUrl;
    private final String appPath;
    private final String deviceName;
    private final String platformVersion;
    private final String udid;
    private final boolean noReset;
    private final int newCommandTimeoutSeconds;
    private final String appPackage;
    private final String appActivity;

    private MobileConfig(
            String appiumUrl,
            String appPath,
            String deviceName,
            String platformVersion,
            String udid,
            boolean noReset,
            int newCommandTimeoutSeconds,
            String appPackage,
            String appActivity
    ) {
        this.appiumUrl = appiumUrl;
        this.appPath = appPath;
        this.deviceName = deviceName;
        this.platformVersion = platformVersion;
        this.udid = udid;
        this.noReset = noReset;
        this.newCommandTimeoutSeconds = newCommandTimeoutSeconds;
        this.appPackage = appPackage;
        this.appActivity = appActivity;
    }

    public String appiumUrl() {
        return appiumUrl;
    }

    public String appPath() {
        return appPath;
    }

    public String deviceName() {
        return deviceName;
    }

    public String platformVersion() {
        return platformVersion;
    }

    public String udid() {
        return udid;
    }

    public boolean noReset() {
        return noReset;
    }

    public int newCommandTimeoutSeconds() {
        return newCommandTimeoutSeconds;
    }

    public String appPackage() {
        return appPackage;
    }

    public String appActivity() {
        return appActivity;
    }

    public static MobileConfig load() {
        String appiumUrl = System.getProperty("appium.url", "http://localhost:4723");
        String appPath = System.getProperty("app.path", "/apk/wishlist.apk");
        String deviceName = System.getProperty("device.name", "Android Emulator");
        String platformVersion = System.getProperty("platform.version");
        String udid = System.getProperty("udid");
        boolean noReset = Boolean.parseBoolean(System.getProperty("noReset", "false"));
        int newCommandTimeoutSeconds = Integer.parseInt(System.getProperty("newCommandTimeout", "120"));

        String appPackage = System.getProperty("app.package");
        String appActivity = System.getProperty("app.activity");

        Path apkPath = Paths.get(appPath);
        if ((appPackage == null || appActivity == null) && Files.exists(apkPath)) {
            ApkInfo apkInfo = ApkInfo.fromApk(apkPath);
            if (appPackage == null) {
                appPackage = apkInfo.packageName();
            }
            if (appActivity == null && apkInfo.mainActivity() != null) {
                appActivity = apkInfo.mainActivity();
            }
        }

        if (appPackage == null) {
            appPackage = "ru.otus.wishlist";
        }
        if (appActivity == null) {
            appActivity = "ru.otus.wishlist.MainActivity";
        }

        return new MobileConfig(
                appiumUrl,
                appPath,
                deviceName,
                platformVersion,
                udid,
                noReset,
                newCommandTimeoutSeconds,
                appPackage,
                appActivity
        );
    }
}
