package ru.otus.mobile.driver;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import ru.otus.mobile.config.MobileConfig;

import java.net.MalformedURLException;
import java.net.URI;

@Singleton
public final class MobileDriverFactory {
    @Inject
    public MobileDriverFactory() {
    }

    public AndroidDriver create(MobileConfig.Emulator emulator) {
        UiAutomator2Options options = new UiAutomator2Options()
                .setPlatformName("Android")
                .setAutomationName("UiAutomator2")
                .setDeviceName(emulator.deviceName())
                .setApp(emulator.appUrl())
                .setSkipDeviceInitialization(true);
        try {
            return new AndroidDriver(URI.create(emulator.appiumUrl()).toURL(), options);
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Invalid Appium URL: " + emulator.appiumUrl(), e);
        }
    }
}
