import networkx as nx
import numpy as np


def correctLastCharCR(inText):#
    """ if the last letter of the nick is '\\' replace it by 'CR'
        for example rohan\ becomes rohanCR
        to avoid complications in nx because of the special char '\\'

    Args:
        inText (str): input nick, checked for '\\' at last position

    Returns:
        str: updated string with '\\' replaced by CR (if it exists) 

    """
    if(len(inText) > 1 and inText[len(inText)-1]=='\\'):
        inText = inText[:-1]+'CR'
    return inText

def correct_nick_for_(inText):
    """
        last letter of nick maybe _ and this produces error in nickmatching
    Args:
        inText (str): input nick, checked for '_' at last position

    Returns:
        str: updated string with '_'  removed 
    """
    
    if(inText and inText[len(inText)-1] == '_'):
        inText = inText[:-1]
    return inText

def to_graph(l):
    G = nx.Graph()
    for part in l:
        # each sublist is a bunch of nodes
        G.add_nodes_from(part)
        # it also imlies a number of edges:
        G.add_edges_from(to_edges(part))
    return G


def to_edges(l):
    """ A generator which
        takes a graph and returns it's edges | for
        example : to_edges(['a','b','c','d']) -> [(a,b), (b,c),(c,d)]

        Args:
            l (list): graph object to be converted to edge_list

        Returns:
            str: edge list of the inputted graph object
    """
    it = iter(l)
    last = next(it)
    for current in it:
        yield last, current
        last = current  


def exponential_curve_func(x, a, b, c):
    return a * np.exp(-b * x) + c

def get_year_month_day(day_content):
    """ A generator which
        takes a day_content and gives the associated year, month and date associated with it       

        Args:
        day_content(dictionary)=
                    {
                    "log_data": day_data, 
                    "auxiliary_data": {
                            "channel": channel_name,
                            "year": year_iterator,
                            "month": month_iterator,
                            "day": day_iterator
                            }
                    }

        Returns:
            str:year, str:month, str:day
    """
    year, month, day = str(day_content["auxiliary_data"]["year"]), str(day_content["auxiliary_data"]["month"]), str(day_content["auxiliary_data"]["day"])
    return year, month, day

def rec_list_splice(rec_list):
    rec_list[1] = rec_list[1][rec_list[1].find(">") + 1:len(rec_list[1])][1:]
    

def build_graphs(nick_sender, nick_receiver, time, year, month, day, day_graph, aggr_graph):
    """    
        Args:
            nick_sender(str): person who has sent the message
            nick_receiver(str): person who receives the message
            time(str): time when message is sent
            year(str): year  when message is sent
            month(str): month  when message is sent
            day(str): day when message is sent
            day_graph(networkx directed graph): a single days graph to which we add edges
            aggr_graph(networkx directed graph): a whole time spans aggregate graph to which we add edges

        Returns:
            None
    """
    day_graph.add_edge(nick_sender, nick_receiver, weight=time)
    aggr_graph.add_edge(nick_sender, nick_receiver, weight=year+"/" + month + "/" + day + " - " + time)
        

@@ -34,16 +34,29 @@
 y as np%0A
+import igraph
 %0A%0Adef co
@@ -2471,16 +2471,17 @@
 h, day%0A%0A
+%0A
 def rec_
@@ -2581,20 +2581,16 @@
 %5D)%5D%5B1:%5D%0A
-    
 %0A%0Adef bu
@@ -3434,16 +3434,212 @@
  time)%0A        %0A
+%0Adef HACK_convert_nx_igraph(nx_graph):%0A    nx.write_pajek(nx_graph, %22/tmp/rohan.net%22)%0A    ig_graph = igraph.Graph()%0A    ig_graph = igraph.read(%22/tmp/rohan.net%22, format=%22pajek%22)%0A    return ig_graph

add hack to convert nx -> igraph