package ru.otus.mobile.driver;

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;

public final class MobileDriverFactory {
    private final MobileConfig config;

    public MobileDriverFactory(MobileConfig config) {
        this.config = config;
    }

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