'use strict';

var es = require('event-stream'),
    gutil = require('gulp-util'),
    _ = require('lodash'),
    log = gutil.log,
    colors= gutil.colors,
    PluginError = gutil.PluginError;

function gulpWc() {

    var codeInUtf = "",
        rootDir = "",
        linesOfCode = 0,
        wholeLinesOfCode = 0,
        stringForTotal = "Total",
        colorIndex = 0,
        colorArray = ["green", "yellow", "blue", "magenta", "cyan"],
        printResult = printResultClosure();

    function countLines(file) {

        rootDir = rootDir || pathToRootDir(file);

        var path = file.path.split('/'),
            indexOfRootDir = _.findIndex(path, function(p) {return p == rootDir}),
            parentDir = path[indexOfRootDir+1],
            filename = path.slice(indexOfRootDir+1, path.length).join('/');

        if (file.isNull()) {
            throw new PluginError('gulp-wc', 'Missing file for gulp-wc');
        }

        codeInUtf = file.contents.toString('utf8');
        linesOfCode = codeInUtf.split('\n').length;
        wholeLinesOfCode += linesOfCode;

        printResult(filename, parentDir);

    }

    function printResultClosure() {
        var previousParentDir;

        return function(filename, parentDir) {
            colorIndex = parentDir == previousParentDir ? colorIndex : (colorIndex + 1) % 5;
            previousParentDir = parentDir;
            filename = filename || stringForTotal;
            var lines = filename !== stringForTotal ? linesOfCode : wholeLinesOfCode;
            log(colors[colorArray[colorIndex]](filename + " :" + lines + " lines"));
        }
    }

    function pathToRootDir(file) {
        var path = file.cwd.split('/');
        return path[path.length - 1];
    }

    return es.through(countLines, printResult);
}

module.exports = gulpWc;

@@ -414,12 +414,12 @@
 %22, %22
-blue
+cyan
 %22, %22
@@ -429,20 +429,20 @@
 enta%22, %22
-cyan
+blue
 %22%5D,%0A    
@@ -1191,16 +1191,90 @@
 rentDir;
+%0A        log(colors.green(%22===========gulp-wc output start============%22));
 %0A%0A      

add the code to indicate the gulp-wc begins