'use strict';

var qusion = window.qusion = angular.module('qusion', [
    'qusion.filters',
    'qusion.common',
    'qusion.data',
    'qusion.ui',
    'qusion.charts'
]);

qusion.list = { };

qusion.list.single = function(list, value, key) {
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (!item[key]) return null;
        if (item[key] == value)
            return item;
    }
    return null;
};

qusion.list.put = function(list, item, key) {
    var existing = qusion.list.single(list, item[key], key);
    if (existing)
        angular.extend(existing, item);
    else {
        list.push(item);
        existing = item;
    }
    return existing;
};

qusion.parseSource = function (input) {
    var parts = input;
    if (parts.indexOf('|') > -1)
        parts = parts.split('|')[0];

    parts = parts.split(' ');
    var source = parts[parts.length - 1];

    return source;
};

qusion.list.hierarchy = function(list, key, parentKey, childrenKey) {
    key = key || "ID";
    parentKey = parentKey || "ParentID";
    childrenKey = childrenKey || "Children";

    var ids = { };
    angular.forEach(list, function(v, k) {
        ids[v.ID] = v;
    });

    angular.forEach(list, function (v, i) {
        if (v.ParentID != null) {
            var ParentID = v.ParentID;

            if (!ids[ParentID].Children) {
                ids[ParentID].Children = [];
            }
            ids[ParentID].Children[ids[ParentID].Children.length] = ids[v.ID];
        }
    });

    

    var looper = function(arr, end, allowRecurse) {
        angular.forEach(arr, function (v, k) {
            
            if (v.ParentID == null || allowRecurse) {
                end.push(v);
                if (v.Children) {
                    looper(v.Children, end, true);
                }
            }
        });
    };
    var end = [];
    looper(ids, end);
    return end;
};

qusion.list.jsTree = function(data, key, title) {
    key = key || "ID";
    title = title || "Title";
    
    var list = [];
   
    var looper = function(array, exit) {
        angular.forEach(array, function (v, k) {
            
            var element = {
                "title": v[title],
                "key": v[key],
                isFolder: true,
                "children": [],
                "ParentID": v.ParentID,
                
            };
            
            if(v.Select) {
                element.select = v.Selected;
            }
           
            if (v.Children != null) {
                looper(v.Children, element.children);
            }
            

            exit.push(element);
        });
        
    };

    looper(data, list);

    return list;
};

qusion.list.jsTreeOrdered = function (list) {
    var data = [];

    var looper = function (array, exit) {
        angular.forEach(array, function (v, k) {
            var element = {
                "ID": v.key,
                "Children": []
            };

            if (v.children != null) {
                looper(v.children, element.Children);
            }
            exit.push(element);
        });

    };

    looper(list, data);
    return data;
};

qusion.generateAlias = function(string) {

    var alias;
    alias = string;
    return alias;
};


Array.prototype.diff = function(a) {
    return this.filter(function(i) { return !(a.indexOf(i) > -1); });
};