#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <locale.h>
#include <fstream.h>
#include <iomanip.h>

char sepSymbol;
//time_t lasttime;

#include "../include/sep1000.h"
#include "parstat.h"

int loadbase(areas_c&areas,fstream&base);
int savebase(areas_c&areas,fstream&base);
int scanlog(areas_c&areas,nodes_c&nodes,ifstream&log);
int report(areas_c&areas,nodes_c&nodes,ostream&rep);

int main(int argc, char**argv)
{
    sepSymbol='\'';
    int rc;

    tzset();
    /*setlocale(LC_ALL,"ru_ru");*/
    cout<< "Parma Tosser's Statistics v 2.02 lite (c) 1998-99 by Sergey Fadeyev\n"
        << endl;
    if(argc<2)
    {
        cout<< "Usage:\n\n"
            << argv[0] << " <logfile>\n\n";
            //<< "As base and expbase will be used "<< cbase
            //<< " and " << cexpbase << " respectively.\n";
        return 10;
    }

    //fstream base;
    //ifstream expbase(cexpbase, ios::in|ios::nocreate);
    ifstream log(argv[1], ios::in|ios::nocreate);
    areas_c areas;
    nodes_c nodes;

    /* Reading base */
    /*cerr<< "Reading base...";
    base.open(cbase,ios::in|ios::nocreate);
    rc=loadbase(areas,base);
    base.close();
    if(rc)
        cerr<<"\nBase readed Ok."<<endl;
    else
    {
        cerr<< "Error opening " << cbase << " for read: ";
        perror("");
    }*/

    /* Scanning log */
    cerr<<"Scanning log...";
    rc=scanlog(areas,nodes,log);
    if(rc)
        cerr<<"\nLog scanned Ok."<<endl;
    else
    {
        cerr<< "Error opening " << argv[1] << " for read: ";
        perror("");
    }

    /* Saving base */
    /*base.open(cbase,ios::out|ios::trunc);
    if(!base)
    {
        cerr<< "Error opening " << cbase << " for write: ";
        perror("");
        return 2;
    }
    cerr<< "Writing base...";
    savebase(areas,base);
    cerr<<"\nBase written Ok."<<endl;*/

    /* Reporting */
    cerr<< "Reporting...";
    report(areas,nodes,cout);
    cerr<<"\nReported Ok."<<endl;
}

/*int loadbase(areas_c&areas,fstream&base)
{
    if(!base)
    {
        return 0;
    }
    while(base)
    {
        infoarea area;
        base>>area;
        if(area.ok)
            areas.addarea(area);
    }
    return 1;
}

int savebase(areas_c&areas,fstream&base)
{
    areas.goto_head();
    do {
        base<<areas.curr()->data<<endl;
    } while(areas.next()!=NULL);
}*/

int scanlog(areas_c&areas,nodes_c&nodes,ifstream&log)
{
    if(!log)
    {
        return 0;
    }
    while(log)
    {
        string date, date2;
        string buf;
        Regex raddr("[0-9]+:[0-9]+/[0-9]+.[0-9]+");
        //Regex rdate("[0-9][0-9] \\(Jan\\)\\|\\(Feb\\)\\|\\(Mar\\)\\|\\(Apr\\)\\|\\(May\\)\\|\\(Jun\\)\\|\\(Jul\\)\\|\\(Aug\\)\\|\\(Sep\\)\\|\\(Oct\\)\\|\\(Nov\\)\\|\\(Dec\\)  [0-9][0-9]  [0-9][0-9]:[0-9][0-9]:[0-9][0-9]");
        //struct tm t, today;
        //time_t ttime;

        readline(log,buf);
        //cout<<buf<<endl;
        if(buf=="")
            continue;
        date=buf.before(20);
        buf=buf.after(date);
        if(buf.index("]")==3)
            buf=buf.after(4);
        if(buf.index("Sent to")==0)
        {
            string addr=buf.at(raddr);
            buf=buf.after("-");
            buf=buf.before("bytes");
            //cout<<date<<"To "<<addr<<" sent "<<atoi(buf)<<" bytes"<<endl;
            nodes.addnodeinfo(addr,0,atoi(buf));
        }
        else if (buf.contains(": sent -"))
        {
            string echo=buf.before(": sent -");
            if(echo==upcase(echo) & echo!="NETMAIL")
            {
                buf=buf.after(": sent -");
                //cout<<date<<"In "<<echo<<" sent "<<atoi(buf)<<" msgs"<<endl;
            }
        }
        else if (buf.contains(": toss -"))
        {
            string echo=buf.before(": toss -");
            if(echo==upcase(echo) & echo!="NETMAIL")
            {
                buf=buf.after(": toss -");
                int msgs=atoi(buf);
                buf=buf.after("(");
                int bytes=atoi(buf);
                //cout<<date<<"In "<<echo<<" tossed "<<bytes<<" bytes in "
                //    <<msgs<<" msgs"<<endl;
                areas.addareainfo(echo,msgs,bytes);
            }
        }
        else if (buf.index("[Inbound]")==0)
        {
            buf=buf.after("(");
            string addr=buf.at(raddr);
            //cout<<date<<"From "<<addr<<" recd "<<atoi(buf)<<" bytes"<<endl;
            nodes.addnodeinfo(addr,atoi(buf),0);
        }
        //else
        //    cout<<date<<"\t"<<buf<<endl;

        //date=buf.through(rdate);
        //date2=buf.through(rdate);
        //buf=buf.after(rdate);
        //cout<<"strcmp passed"<<endl;
        //cout<<strptime(date,"%d %b %y %H:%M:%S",&t)<<endl;
        //cout<<"strptime passed"<<endl;
        //ttime=mktime(&t);
        //cout<<"mktime passed"<<endl;
        //cout<<lin<<endl;
        /*
         strptime(date,"%d/%m/%y %H:%M:%S",&t);
         ttime=mktime(&t);
         */
    }
    return 1;
}

int report(areas_c&areas,nodes_c&nodes,ostream&rep)
{
    rep << " Echo                                    Size                Msgs\n"
        << "哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪腬n";
    int totsize=0, totmsgs=0;
    areas.goto_head();
    do {
        infoarea &a=areas.curr()->data;
        if(a.days[0].msgs!=0)
        {
            totsize+=a.days[0].size;
            totmsgs+=a.days[0].msgs;
            rep << a.echotag << setw(45-a.echotag.length())
                << sep1000(a.days[0].size)
                << setw(20) << sep1000(a.days[0].msgs) <<endl;
        }
    } while(areas.next()!=NULL);
    rep << "哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪腬n";
    rep << " Total:" << setw(38) << sep1000(totsize) << setw(20)
        << sep1000(totmsgs) <<endl
        << "\n Node                                    Sent                Recd\n"
        << "哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪腬n";
    int totsent=0, totrecd=0;
    nodes.goto_head();
    do {
        infonode &a=nodes.curr()->data;
        totsent+=a.sent;
        totrecd+=a.recd;
        cout<< a._addr << setw(45-a._addr.length()) << sep1000(a.sent)
            << setw(20) << sep1000(a.recd) <<endl;
    } while(nodes.next()!=NULL);
    rep << "哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪腬n";
    rep << " Total:" << setw(38) << sep1000(totsent) << setw(20)
        << sep1000(totrecd) <<endl;
}
