"""
Pytest plugin with commonly used fixtures
"""
import pytest
from tlib.base import LogHelper


# noinspection PyShadowingNames
@pytest.fixture(scope='session')
def tlib_logger():
    """
    Returns logger with name tlib.\n
    This logger should only be used by TLib modules. To log from a test or test module use test_logger\n
    Logger will get flushed at the end to ensure data is not lost if tests exist abnormally
    """
    return LogHelper.get_tlib_logger()


# noinspection PyShadowingNames
@pytest.fixture(scope='session')
def adb_logger():
    """
    Returns logger with name adb\n
    This logger should only be used by TLib modules interacting with ADB.\n
    To log from a test or test module use test_logger\n
    Logger will get flushed at the end to ensure data is not lost fi tests exist abnormally
    """
    return LogHelper.get_adb_logger()


# noinspection PyShadowingNames
@pytest.fixture(scope='session')
def test_logger():
    """
    Returns logger that can be used to log test information\n
    Logger will get flushed at the end to ensure data is not lost fi tests exist abnormally
    """
    return LogHelper.get_test_logger()


def pytest_addoption(parser):
    """
    Options supported by pytest's selenium plug-in
    """
    group = parser.getgroup("tlib", "General command line options provided by TLib")
    group.addoption(
        "--base_url",
        action="store",
        dest="base_url",
        help="base url of the server used for testing. This can be used for web services as well as web sites")


#noinspection PyUnresolvedReferences
# noinspection PyShadowingNames
@pytest.fixture(scope="session")
def base_url(request, tlib_logger):
    """
    Returns value of --base_url command line parameter
    """
    if request.config.getoption("base_url") is None:
        #noinspection PyUnresolvedReferences
        tlib_logger.warn("parameter --base_url was not specified")
        pytest.fail("parameter --base_url was not specified")
        return None
    else:
        return request.config.getoption("base_url")


