'use strict';
var _ = require( 'lodash' );
var logRegex = require( './log-regex' );
var dateUtil = require( './time' );
var moment = require( 'moment' );

var IrcFile = function( rawData ) {
    if ( !_.isString( rawData ) ) {
        throw new Error( 'Bad file data' );
    }

    this.data = rawData.split( /(?:\n|\r)+/ );

    var logOpenedText = _.find( this.data, function( logLine ) {
        return logRegex.logOpened.test( logLine );
    } );

    //If there is a line in the log that tells us when it was opened, then we can get the date that this file
    //represents. If it isn't present, just bomb out for now
    if ( _.isString( logOpenedText ) ) {
        var regexMatch = logOpenedText.match( logRegex.logOpened );

        //TODO: Make a class for each regex that can test for a match, and if it matches then
        //It can return a well structured data object
        var month = regexMatch[2],
            day = parseInt( regexMatch[3] ),
            year = parseInt( regexMatch[5] );

        if ( ! logRegex.month.test( month ) ||
             ! _.isInteger( day ) ||
             ! _.isInteger( year ) ) {
                 throw new Error( 'Bad date!' );
        }

        this.date = new Date( Date.UTC( year, dateUtil.monthToInt[month], day, 0, 0, 0, 0 ) );

        if( !_.isDate( this.date ) ) {
            throw new Error( 'Bad date, regex pass!' );
        }
    } else {
        throw new Error( 'No log date!' );
    }

    this.iteratorIndex = 0;
};

IrcFile.prototype.getNextUserEvent = function() {
    if ( this.iteratorIndex === -1 ) {
        return undefined;
    }

    this.iteratorIndex = _.findIndex( this.data, function( logLine ) {
        return  logRegex.userJoin.exec( logLine ) || logRegex.userLeave.exec( logLine )
    }, this.iteratorIndex );

    if ( this.iteratorIndex === -1 ) {
        return undefined;
    }
    var logLineText = this.data[ this.iteratorIndex ];

    this.iteratorIndex++;
    this.iteratorIndex = this.iteratorIndex >= this.data.length ? -1 : this.iteratorIndex;

    var regexMatch, userLeave;
    if ( regexMatch = logRegex.userJoin.exec( logLineText ) ) {
        userLeave = false;
    } else if( regexMatch = logRegex.userLeave.exec( logLineText ) ) {
        userLeave = true;
    } else {
        throw new Error( "I just don't know what went wrong!" );
    }

    var hour = parseInt( regexMatch[1] ),
        minute = parseInt( regexMatch[2] ),
        second = parseInt( regexMatch[3] );

    if ( !_.isInteger( hour ) ||
         !_.isInteger( minute ) ||
         !_.isInteger( second ) ) {
             throw new Error( 'Bad hour minute or second' );
         }

    var timestamp = new Date( this.date.getTime() );
    timestamp.setUTCHours( hour -1 );
    timestamp.setUTCMinutes( minute );
    timestamp.setUTCSeconds( second )

    //var timestampMoment = moment.utc( timestamp );

    return {
        timestamp: timestamp,// timestampMoment.toDate(),
        username: regexMatch[5],
        isLogin: !userLeave
    }
}

module.exports = IrcFile;