import random


def new_episode(client, carla_settings, position, vehicle_pair, pedestrian_pair, set_of_weathers):
    """
    Start a CARLA new episode and generate a target to be pursued on this episode

    Args:
        client: the client connected to CARLA now
        carla_settings: a carla settings object to be used
        position: start position in the town
        vehicle_pair: range of permissible number of vehicles
        pedestrian_pair: range of permissible number of pedestrians
        set_of_weathers: permissible set of weathers

    Returns:
        Returns the configuration of the current episode to be used for data collection
    """

    # Every time the seeds for the episode are different
    number_of_vehicles = random.randint(vehicle_pair[0], vehicle_pair[1])
    number_of_pedestrians = random.randint(pedestrian_pair[0], pedestrian_pair[1])
    weather = random.choice(set_of_weathers)
    carla_settings.set(
        NumberOfVehicles=number_of_vehicles,
        NumberOfPedestrians=number_of_pedestrians,
        WeatherId=weather
    )
    scene = client.load_settings(carla_settings)
    client.start_episode(position)

    return scene.map_name, scene.player_start_spots, weather, number_of_vehicles, number_of_pedestrians, \
           carla_settings.SeedVehicles, carla_settings.SeedPedestrians