#include <strclass.h>

#include "infoarea.h"
#include "infonode.h"
#include "../include/list_tpl.h"

class areas_c: public list<infoarea> {
public:
    areas_c() : list<infoarea>() {};
/*    areas_c(int num) {
        list<infoarea>(num);
    };*/
    list_el<infoarea>* addarea(string echotag);
    list_el<infoarea>* addarea(infoarea &inf);
    list_el<infoarea>* addareainfo(string echotag, int recd_m, int recd_s/*,
                                  int sent_m*/);
    list_el<infoarea>* area(string echotag);
};

list_el<infoarea>* areas_c::addarea(string echotag)
{
    list_el<infoarea>* c_el;

    if(head!=NULL)
    {
        c_el=head;
        while(c_el->next!=NULL)
        {
            //S if(strcmp(echotag,c_el->data.echotag)<0)
            if(c_el->data.echotag>echotag)
            {
                c_el=new list_el<infoarea>(c_el->prev,c_el);
                c_el->next->prev=c_el;
                if(c_el->prev==NULL)
                    head=c_el;
                else
                    c_el->prev->next=c_el;
                //S strcpy(c_el->data.echotag,echotag);
                c_el->data.echotag=echotag;
                return c_el;
            }
            else
                c_el=c_el->next;
        }
        //S if(strcmp(echotag,c_el->data.echotag)<0)
        if(c_el->data.echotag>echotag)
        {
            /*if(c_el->prev==NULL)
                printf("Adding %s before %s\n",inf.echotag,
                       c_el->data.echotag);
            else
                printf("Adding %s between %s and %s\n",inf.echotag,
                       c_el->prev->data.echotag,c_el->data.echotag);*/
            c_el=new list_el<infoarea>(c_el->prev,c_el);
            c_el->next->prev=c_el;
            if(c_el->prev==NULL)
                head=c_el;
            else
                c_el->prev->next=c_el;
            //S strcpy(c_el->data.echotag,echotag);
            c_el->data.echotag=echotag;
            return c_el;
        }
        else
        {
            c_el->next=new list_el<infoarea>(c_el);
            c_el=c_el->next;
            //S strcpy(c_el->data.echotag,echotag);
            c_el->data.echotag=echotag;
            return c_el;
        }
    }
    else
    {
        head=new list_el<infoarea>;
        current=head;
        c_el=head;
    }
    //S strcpy(c_el->data.echotag,echotag);
    c_el->data.echotag=echotag;
    return c_el;
}

list_el<infoarea>* areas_c::addarea(infoarea &inf)
{
    add_el(inf);
}
/*{
    list_el<infoarea>* c_el;

    //cout<<"Adding new area: "<<inf<<endl;
    if(head!=NULL)
    {
        c_el=head;
        while(c_el->next!=NULL)
        {
            if(inf<c_el->data)
            {
                c_el=new list_el<infoarea>(c_el->prev,c_el);
                c_el->next->prev=c_el;
                if(c_el->prev==NULL)
                    head=c_el;
                else
                    c_el->prev->next=c_el;
                c_el->data=inf;
                return c_el;
            }
            else
                c_el=c_el->next;
        }
        if(inf<c_el->data)
        {
            c_el=new list_el<infoarea>(c_el->prev,c_el);
            c_el->next->prev=c_el;
            if(c_el->prev==NULL)
                head=c_el;
            else
                c_el->prev->next=c_el;
            c_el->data=inf;
            return c_el;
        }
        else
        {
            c_el->next=new list_el<infoarea>(c_el);
            c_el=c_el->next;
            c_el->data=inf;
            return c_el;
        }
    }
    else
    {
        //printf("Adding %s at the head\n",inf.echotag);
        head=new list_el<infoarea>;
        current=head;
        c_el=head;
    }
    c_el->data=inf;
    return c_el;
}*/

list_el<infoarea>* areas_c::area(string echotag)
{
    list_el<infoarea>* c_el;

    if(head==NULL)
        return NULL;

    c_el=head;
    while(c_el!=NULL)
    {
        if(c_el->data.echotag==echotag)
            return c_el;
        else
            c_el=c_el->next;
    }
    return NULL;
}

list_el<infoarea>* areas_c::addareainfo(string echotag, int recd_m,
                                        int recd_s/*, int sent_m=0*/)
{
    list_el<infoarea>* c_el;

    c_el=area(echotag);
    if (c_el==NULL)
    {
        /*infoarea a(echotag);
        c_el=addarea(a);*/
        c_el=addarea(echotag);
    }
    c_el->data.days[0].msgs+=recd_m;
    c_el->data.days[0].size+=recd_s;
    c_el->data.lastdate=c_el->data.lasttime=time(NULL);
}

class nodes_c: public list<infonode> {
public:
    nodes_c() {
        list<infonode>();
    };
    list_el<infonode>* addnode(string addr);
    list_el<infonode>* addnode(infonode &inf);
    list_el<infonode>* addnodeinfo(string node, int recd_s, int sent_s);
    list_el<infonode>* node(string node);
};

list_el<infonode>* nodes_c::addnode(infonode &inf)
{
    add_el(inf);
}

list_el<infonode>* nodes_c::addnodeinfo(string addr, int recd_s, int sent_s)
{
    list_el<infonode>* c_el;

    c_el=node(addr);
    if (c_el==NULL)
    {
        /*infonode *a=new infonode(addr);
         c_el=addnode(*a);*/
        c_el=addnode(addr);
    }
    c_el->data.recd+=recd_s;
    c_el->data.sent+=sent_s;
    //c_el->data.lastdate=c_el->data.lasttime=time(NULL);
}

list_el<infonode>* nodes_c::node(string node)
{
    list_el<infonode>* c_el;

    if(head==NULL)
        return NULL;

    c_el=head;
    while(c_el!=NULL)
    {
        if(c_el->data._addr==node)
            return c_el;
        else
            c_el=c_el->next;
    }
    return NULL;
}

list_el<infonode>* nodes_c::addnode(string saddr)
{
    list_el<infonode>* c_el;
    fidoaddr addr(saddr);

    if(head!=NULL)
    {
        c_el=head;
        while(c_el->next!=NULL)
        {
            //S if(strcmp(echotag,c_el->data.echotag)<0)
            if(c_el->data.addr>addr)
            {
                c_el=new list_el<infonode>(c_el->prev,c_el);
                c_el->next->prev=c_el;
                if(c_el->prev==NULL)
                    head=c_el;
                else
                    c_el->prev->next=c_el;
                //S strcpy(c_el->data.echotag,echotag);
                c_el->data.addr=addr;
                c_el->data._addr=saddr;
                return c_el;
            }
            else
                c_el=c_el->next;
        }
        //S if(strcmp(echotag,c_el->data.echotag)<0)
        if(c_el->data.addr>addr)
        {
            /*if(c_el->prev==NULL)
                printf("Adding %s before %s\n",inf.echotag,
                       c_el->data.echotag);
            else
                printf("Adding %s between %s and %s\n",inf.echotag,
                       c_el->prev->data.echotag,c_el->data.echotag);*/
            c_el=new list_el<infonode>(c_el->prev,c_el);
            c_el->next->prev=c_el;
            if(c_el->prev==NULL)
                head=c_el;
            else
                c_el->prev->next=c_el;
            //S strcpy(c_el->data.echotag,echotag);
            c_el->data.addr=addr;
            c_el->data._addr=saddr;
            return c_el;
        }
        else
        {
            c_el->next=new list_el<infonode>(c_el);
            c_el=c_el->next;
            //S strcpy(c_el->data.echotag,echotag);
            c_el->data.addr=addr;
            c_el->data._addr=saddr;
            return c_el;
        }
    }
    else
    {
        head=new list_el<infonode>;
        current=head;
        c_el=head;
    }
    //S strcpy(c_el->data.echotag,echotag);
    c_el->data.addr=addr;
    c_el->data._addr=saddr;
    return c_el;
}
