export data_functions, save_path, getValues
import Pkg
Pkg.activate(".")

using DataFrames
using DelimitedFiles



# Dict of the functions used for evaluation
# QUESTION Calculate Variance for all of these?
data_type_functions = Dict(
    :served_percentage => RP.served_percentage,
    :driven_distance => RP.driven_distance,
    :requested_distance => RP.requested_distance,
    :mean_relative_delay => RP.mean_relative_delay,
    :mean_occupancy => RP.mean_occupancy,
)


save_path = "" #TODO What savepath?

"""
 Converts a given index to a 2D Matrix Index with the given Matrix size
"""
function getValue(index, xmin, xmax, xlen, ymin, ymax, ylen)
    xstep = (xmax-xmin)/xlen
    ystep = (ymax-ymin)/ylen
    xs = xmin:xstep:xmax
    ys = ymin:ystep:ymax

    x = xs[index%xlen + 1]
    y = ys[index - index%xlen + 1]
    return x,y
end

"""
 Function for Running a RidePooling simulation with the normalized Frequency x
"""
function simulate_rp(paths::Dict, N::Int64, x, y, t0::Float64, specs; served = 10*N, requested=10*N)

    #Make Model
    #TODO Make Random gens
    model=RP.get_model(;N_bus=N,ν=x/t0,specs...);
    RP.run!(model;requested=requested, served=served)
    data = Dict()
    data[:frequency] = x
    data[:dt_earliest_pickup] = y
    for (name, func) in data_type_functions
        data[name] = func(model)
    end

    # Save the calculated Data
    data = DataFrame(data)

    #CSV.write(paths[:data]*"$(x)_$(y).csv", data) #QUESTION what savepath
    open(paths[:data]*"$index.csv", "w") do io
        writedlm(io, Iterators.flatten(([names(data)], eachrow(data))), ',')
    end
    #Save the model for possible later reference
    RP.savemodel(paths[:model]*"$index.model",model;route_matrix=false) #QUESTION What savepath
                                    # QUESTION How do I use the saving
end