/**
 * Created by Macr0s on 18/12/16.
 */

var stream = require('linestream');
var BaseParser = require("./BaseParser");

var relations = {};

var stage_two = function (file, cb){
    var line = stream.create(file, {bufferSize: 300})

    line.on('data', function(line, isEnd) {
        var elements = line.split("\t");

        if (typeof relations[elements[0]] != "undefined"){
            relations[elements[0]].SubjectType = BaseParser.simplify_name(elements[1]);
            relations[elements[0]].ObjectType = BaseParser.simplify_name(elements[2])
        }
    })

    line.on('end', function() { // emitted at the end of file

        var rs = [];

        Object.keys(relations).forEach(function (e) {
            rs.push(relations[e]);
        })

        cb(true, rs);
    });

    line.on('error', function(e) { // emitted when an error occurred
        cb(false);
    });
}

var stage_one = function (file, cb){
    var line = stream.create(file, {bufferSize: 300})

    line.on('data', function(line, isEnd) {
        var elements = line.split("\t");

        var name = BaseParser.simplify_name(elements[0])

        relations[name] = {
            RepresentativePhrase: elements[1].trim(),
            Name: name
        }
    })

    line.on('end', function() { // emitted at the end of file
        cb(true);
    });

    line.on('error', function(e) { // emitted when an error occurred
        cb(false);
    });

}

var simplify_access = function (elements){
    var relations = {}

    elements.forEach(function (e){
        relations[e.Name] = e;
    })

    return relations
}

module.exports =  {
    createRelations: function (file_pharses_rappresentative, file_relation_schema, cb) {
        var RelationModel = require("../models/RelationModel");

        stage_one(file_pharses_rappresentative, function (status) {
            if (!status)
                return cb(false)

            stage_two(file_relation_schema, function (status, rs) {
                if (!status)
                    return cb(false)

                RelationModel.collection.insert(rs, function(err, list) {
                    if (err) return cb(false);
                    else return cb(true, simplify_access(list.ops));

                });


            });
        })
    }
}
@@ -306,32 +306,91 @@
 e.split(%22%5Ct%22);%0A%0A
+        var name = BaseParser.simplify_name(elements%5B0%5D);%0A%0A
         if (type
@@ -402,27 +402,20 @@
 lations%5B
-elements%5B0%5D
+name
 %5D != %22un
@@ -447,27 +447,20 @@
 lations%5B
-elements%5B0%5D
+name
 %5D.Subjec
@@ -528,27 +528,20 @@
 lations%5B
-elements%5B0%5D
+name
 %5D.Object
@@ -1088,33 +1088,32 @@
 ne.split(%22%5Ct%22);%0A
-%0A
         var name
@@ -1152,16 +1152,17 @@
 ents%5B0%5D)
+;
 %0A%0A      

fix objecttype and subjecttype