package ru.otus.mobile.driver;

import com.google.inject.Inject;
import com.google.inject.Injector;
import io.appium.java_client.android.AndroidDriver;
import ru.otus.mobile.config.MobileConfig;
import ru.otus.mobile.config.TestAccount;
import ru.otus.mobile.config.TestContext;
import ru.otus.mobile.guice.SessionModule;

public final class MobileSessionFactory {
    private final Injector rootInjector;
    private final EmulatorQueue emulatorQueue;
    private final MobileDriverFactory driverFactory;
    private final LogcatCollector logcatCollector;

    @Inject
    public MobileSessionFactory(
            Injector rootInjector,
            EmulatorQueue emulatorQueue,
            MobileDriverFactory driverFactory,
            LogcatCollector logcatCollector
    ) {
        this.rootInjector = rootInjector;
        this.emulatorQueue = emulatorQueue;
        this.driverFactory = driverFactory;
        this.logcatCollector = logcatCollector;
    }

    public MobileSession create(TestAccount account, String testName) {
        MobileConfig.Emulator emulator = emulatorQueue.acquire();
        AndroidDriver driver = driverFactory.create(emulator);
        TestContext context = new TestContext(account, emulator, driver, testName);
        Injector childInjector = rootInjector.createChildInjector(new SessionModule(context));
        return new MobileSession(context, childInjector, emulatorQueue, logcatCollector);
    }
}
