import numpy as np
import random

def service_time_from_arrivals_and_departures(ARRIVALS_AND_DEPARTURES):
    """

    :return:
    """
    service_times = []
    for arrival, departure in ARRIVALS_AND_DEPARTURES.iteritems():
        if departure is not np.infty:
            service_time = departure - arrival
            if service_time > 0.:
                service_times.append(service_time)
    return service_times

def infinitesimal_propagator(numberOfStates=3, alpha=10.):
    """
    A random walker is created per arrivals, and the departure happens whens the walker
    reaches the last state. The more states the longer the service

    alpha the bigger the longest
    """
    Q = np.random.dirichlet(alpha*np.random.random(numberOfStates), size=numberOfStates - 1).tolist()
    stationary = [np.zeros(numberOfStates).tolist()]
    Q = np.concatenate((Q, stationary), axis=0)
    QL = [q.tolist() for q in Q]
    return QL

def arrivals_and_departures(ARRIVALS, DEPARTURES):
    ARRIVALS_AND_DEPARTURES = {}
    for i, departure_time in DEPARTURES.iteritems():
        ARRIVALS_AND_DEPARTURES[ARRIVALS[i]] = departure_time

    return ARRIVALS_AND_DEPARTURES

def sample_from_rate(rate):
    if rate == 0:
        return np.infty
    return random.expovariate(rate)

def service_time_function(service_time):
    # np.random.exponential(service_rate)
    return service_time