########## 
##########
# Definition of functions used to look at species rewiring in relevant paper
##########
##########
# AUTHOR: Cole B. Brookson
# DATE OF CREATION: 2021-04-29
##########
##########

# set up =======================================================================

using LightGraphs
using PyCall

run(`$(PyCall.python) -m pip install python-igraph`)
run(`$(PyCall.python) -m pip install partition_igraph`)

ig = pyimport("igraph");
pyimport("partition_igraph");
# main function ================================================================

"""
    network_metrics(species_list, network)

Get all metrics of interest from the focal network and put them in a vector
"""
function network_metrics(species_list::Array, network::Array)
    # simple metrics 

    # initialize vector of metrics for each species
    species_network_metrics = Vector{Any}(undef, length(species_list[:,1]))

    # order
    S = length(species_list[:,1])
    # interactions 
    L = sum(network)
    # linkage density 
    SL = L/S
    # Connectance
    C = L/(S*(S-1))
    # degree
    all_in_deg = Vector{Any}(undef, length(species_list[:,1]))
    all_out_deg = Vector{Any}(undef, length(species_list[:,1]))
    all_total_deg = Vector{Any}(undef, length(species_list[:,1]))
    for i in 1:length(species_list[:,1])
        in_deg = sum(network[i,:])
        out_deg = sum(network[:,i])
        total_deg = in_deg + out_deg
        species_network_metrics[i] = [Float64(i), 
                                      Float64(in_deg), 
                                      Float64(out_deg),
                                      Float64(total_deg)]
        all_in_deg[i] = in_deg
        all_out_deg[i] = out_deg
        all_total_deg[i] = total_deg
    end 
    unique_total_deg = unique(all_total_deg)
    # degree distributions 
    unique_in_deg = unique(all_in_deg)
    unique_out_deg = unique(all_out_deg)
    pk_in = Vector{Any}(undef, length(unique_in_deg))
    pk_out = Vector{Any}(undef, length(unique_out_deg))
    j = 1
    for i in unique_in_deg # assuming each i is each k 
        pk_in_temp = (length(all_in_deg[all_in_deg .== i]))/S # P(k) = N(k)/S
        pk_in[j] = [i, pk_in_temp]
        j += 1
    end
    j = 1
    for i in unique_out_deg # assuming each i is each k 
        pk_out_temp = (length(all_out_deg[all_out_deg .== i]))/S # P(k) = N(k)/S
        pk_out[j] = [i, pk_out_temp]
        j += 1
    end
    # diameter 
    interaction_matrix = convert(Array{Int64}, network)
    graph = DiGraph(interaction_matrix)
    diameter = LightGraphs.diameter(SimpleGraph(graph))
    # local clustering coefficient 
    for i in 1:length(species_network_metrics)
        cc_i = local_clustering_coefficient(graph, i)
        append!(species_network_metrics[i], cc_i)
    end 
    # local (by degree) clustering coefficient 
    cc_k = Vector{Any}(undef, length(unique_total_deg))
    for k in 1:length(unique_total_deg)
        cc_1 = []
        for i in 1:length(species_network_metrics)
            if species_network_metrics[i][4] == unique_total_deg[k] # position 4 is total deg 
                append!(cc_1, species_network_metrics[i][5])
            end
        end 
        cc_k[k] = [unique_total_deg[k], (sum(cc_1)/S)]
    end 
    # global clustering coefficient
    global_cc = global_clustering_coefficient(graph)
    # modularity 
    #simple_graph = SimpleGraph(graph)
    #modularity(simple_graph)
    # nestedness

    # web metrics 
    web_metrics = [S, L, SL, C, diameter, global_cc]
    
    return web_metrics, all_in_deg, all_out_deg, all_total_deg

end

## practice with the example:
# https://www.juliabloggers.com/a-tutorial-on-igraph-for-julia/