########## 
##########
# Create series of networks to run through simulations
##########
##########
# AUTHOR: Cole B. Brookson
# DATE OF CREATION: 2021-05-29
##########
##########

# set up =======================================================================
using DrWatson
@quickactivate "trait-based-rewiring"

include(srcdir("00_species_list_creation.jl"))

using DelimitedFiles

# set all positional values 
organism_id_pos = 1
organism_type_pos = 2
body_size_pos = 3
lower_prey_limit_pos = 4
upper_prey_limit_pos = 5
habitat_midpoint_pos = 6
habitat_lower_limit_pos = 7
habitat_upper_limit_pos = 8
nutritional_value_pos = 9
biomass_pos = 10
network_id_pos = 11
alive_pos = 12

# create different combinations of networks ====================================

# the different values that have different options are:
#   richness shape
#   biomass shape

all_network_data = Array{Any}(undef, (450000, 6))
all_networks = Array{Int}(undef, (450000, 51))
all_species_lists = Array{Any}(undef, (450000, 11))

"""
    create_networks(j, 
        network_id, 
        all_network_data, 
        all_networks, 
        all_species_lists
    )

For simplicity, put the process of making all of the 9000 networks in a function
"""
function create_networks(
    reps::Int,
    j::Int, 
    all_network_data::Array,
    all_networks::Array,
    all_species_lists::Array
    )

    # make 1000 networks for each of the 9 combinations (3x3 of richness and
    # biomass) to have multiple starting points
    network_id = 1
    for i in 1:reps
        for richness in ["top-heavy", "bottom-heavy", "uniform"]
            for biomass in ["pyramid", "uniform", "inverted"]
                temp = Array{Any}(undef, (50, 6))
                # get species list and network
                species_list, network = network_draw(50, richness)
                # save network 
                all_networks[j:j+49, 1] = repeat([network_id], 50)
                all_networks[j:j+49, 2:51] = network
                # get biomass 
                species_list = initialize_biomass(species_list, biomass)
                # append the network id to each species list item
                species_list = hcat(species_list, fill(network_id, 50))
                # put the species list in the all_species_lists 
                all_species_lists[j:j+49, :] = species_list
                # put each species with the network data in all_network_data 
                for k in 1:size(species_list,1)
                    temp[k, 1] = network_id
                    temp[k, 2] = richness
                    temp[k, 3] = biomass 
                    temp[k, 4] = species_list[k, body_size_pos]
                    temp[k, 5] = species_list[k, biomass_pos]
                    temp[k, 6] = species_list[k, organism_type_pos]
                end 
                all_network_data[(j:j+49), :] = temp
                # increment
                j = j + 50
                network_id = network_id + 1
            end
        end 
        if (i % 5 == 0) print(i, " out of 1000 --") end 
    end 
    return all_network_data, all_networks, all_species_lists
end

# run function =================================================================
all_network_data_done, all_networks_done, all_species_lists_done = create_networks(
    1000, 1, all_network_data, all_networks, all_species_lists)
# add alive/dead 
all_species_lists_done = hcat(all_species_lists_done, ones(size(all_species_lists_done, 1))) 

# save files ===================================================================

writedlm(datadir("./intermediate-files/all_networks_for_simulations.csv"), 
        all_networks_done, ",")
writedlm(datadir("./intermediate-files/species_lists_for_simulations.csv"), 
        all_species_lists_done, ",")
writedlm(datadir("./intermediate-files/all_network_data_for_simulations.csv"),
        all_network_data_done, ",")

# test that files are equivalent when read back in 
networks_test = readdlm(datadir("./intermediate-files/all_networks_for_simulations.csv"), ',', Any)
if networks_test == all_networks_done
    print("networks are equivalent")
else 
    ArgumentError("networks are NOT equivalent")
end 

network_data_test = readdlm(datadir("./intermediate-files/all_network_data_for_simulations.csv"), ',', Any)
if network_data_test == all_network_data_done
    print("network data are equivalent")
else 
    ArgumentError("network data are NOT equivalent")
end 

species_list_test = readdlm(datadir("./intermediate-files/species_lists_for_simulations.csv"), ',', Any)
i = 1
while i <= size(all_species_lists_done, 1)
    if species_list_test[i,:] === all_species_lists_done[i,:]
        error("species lists are NOT equivalent at ", i)
    end  
    i = i + 1
end 


