/**
 * Utils
 * @param prop
 * @param data
 * @returns {*}
 */
ko.observableArray.fn.find = function(prop, data) {
    var valueToMatch = data[prop];
    return ko.utils.arrayFirst(this(), function(item) {
        return item[prop] === valueToMatch;
    });
};

//group
function group(id, name, roles){
    var self = this;

    self.id = ko.observable(id);
    self.name = name;
    self.roles = ko.observableArray();
}
function role(role, name){
    var self = this;
    self.role = role;
    self.name = name;
};

var viewModel = {
    groups: ko.observableArray(new Array()),
    roles: ko.observableArray(new Array()),

    get_all_data: function(){
        loadMask("active_notices_list", "Loading active notifications");
        $.ajax({
            type: 'post',
            async: false,
            dataType: 'json',
            url: getURLGetAllData(),
            data: {},
            success: function(response) {
                viewModel.roles.removeAll();
                viewModel.groups.removeAll();
                if(response.groups.length > 0){
                    var roles = new Array();
                    $.each( response.groups, function( i, group ) {
                        $.each( group.roles, function( i, role ) {
                            var role2add = $.grep(viewModel.roles(), function(r){
                                return (r.role == role); });
                            if(role2add.length == 1)
                                roles.push(role2add[0]);
                        });
                        viewModel.groups.push(new group(group.id, group.name, roles))
                    });
                }
                /*$.each( viewModel.active_notices(), function( i, val ) {
                    $('#' + val.own_id).on('show.bs.collapse', function () {
                        val.getMyLoops();
                    })
                });*/
            },
            error:function() {
                alert("Error");
            }
        });
        hideMask("active_notices_list");
    }
    /*get_active_notices: function(){
        $.ajax({
            type: 'post',
            async: false,
            dataType: 'json',
            url: getURLActiveNotifications(),
            data: {},
            success: function(response) {
                viewModel.active_notices.removeAll();
                if(response.notifications.length > 0){
                    var notifications = response.notifications;
                    viewModel.active_notices.push(new notice(notifications[0].own_id, notifications[0].worker_id,
                        notifications[0].worker, notifications[0].loop_id, notifications[0].generated,
                        notifications[0].loop_date, null, notifications[0].alert_type, notifications[0].time,
                        notifications[0].text, notifications[0].hour_cost))
                    for (i = 1; i < notifications.length; i++){
                        //alert(getFirstWeekDay(notifications[0].generated) + ' - ' + getFirstWeekDay(notifications[1].generated));
                        var added = $.grep(viewModel.active_notices(), function(s){
                            return (s.worker_id == response.notifications[i].worker_id
                                && s.alert_type ==  notifications[i].alert_type)
                                && ((s.alert_type == 2
                                && getFirstWeekDay(s.loop_date).getTime() == getFirstWeekDay(notifications[i].loop_date).getTime())
                                || (s.alert_type == 1
                                && s.loop_date == notifications[i].loop_date)); });
                        if(added.length == 1){
                            viewModel.active_notices()[viewModel.active_notices.indexOf(added[0])].time(
                                viewModel.active_notices()[viewModel.active_notices.indexOf(added[0])].time()
                                    + notifications[i].time);
                        }
                        else
                            viewModel.active_notices.push(new notice(notifications[i].own_id,
                                notifications[i].worker_id, notifications[i].worker, notifications[i].loop_id,
                                notifications[i].generated, notifications[i].loop_date, null,
                                notifications[i].alert_type, notifications[i].time, notifications[i].text,
                                notifications[i].hour_cost));
                    }
                }
                $.each( viewModel.active_notices(), function( i, val ) {
                    $('#' + val.own_id).on('show.bs.collapse', function () {
                        val.getMyLoops();
                    })
                });
            },
            error:function() {
                alert("Error");
            }
        });
    },
    get_dismissed_notices: function(){
        $.ajax({
            type: 'post',
            async: true,
            dataType: 'json',
            url: getURLDismissedNotifications(),
            data: {},
            success: function(response) {
                viewModel.dismissed_notices.removeAll();
                if(response.notifications.length > 0){
                    var notifications = response.notifications;
                    viewModel.dismissed_notices.push(new notice(notifications[0].own_id, notifications[0].worker_id,
                        notifications[0].worker, notifications[0].loop_id, notifications[0].generated,
                        notifications[0].loop_date, notifications[0].dismissed, notifications[0].alert_type,
                        notifications[0].time, notifications[0].text, notifications[0].hour_cost))
                    for (i = 1; i < notifications.length; i++){
                        //alert(getFirstWeekDay(notifications[0].generated) + ' - ' + getFirstWeekDay(notifications[1].generated));
                        var added = $.grep(viewModel.dismissed_notices(), function(s){
                            return (s.worker_id == response.notifications[i].worker_id
                                && s.alert_type ==  notifications[i].alert_type)
                                && ((s.alert_type == 2
                                && getFirstWeekDay(s.loop_date).getTime() == getFirstWeekDay(notifications[i].loop_date).getTime())
                                || (s.alert_type == 1
                                && s.loop_date == notifications[i].loop_date)); });
                        if(added.length == 1){
                            viewModel.dismissed_notices()[viewModel.dismissed_notices.indexOf(added[0])].time(
                                viewModel.dismissed_notices()[viewModel.dismissed_notices.indexOf(added[0])].time()
                                    + notifications[i].time);
                        }
                        else
                            viewModel.dismissed_notices.push(new notice(notifications[i].own_id,
                                notifications[i].worker_id, notifications[i].worker, notifications[i].loop_id,
                                notifications[i].generated, notifications[0].loop_date, notifications[i].dismissed,
                                notifications[i].alert_type, notifications[i].time, notifications[i].text,
                                notifications[i].hour_cost));
                    }
                }
                $.each( viewModel.dismissed_notices(), function( i, val ) {
                    $('#' + val.own_id).on('show.bs.collapse', function () {
                        val.getMyLoops();
                    })
                });
            },
            error:function() {
                alert("Error");
            }
        });
    }*/
};

function loadMask(area_id, message){
    /*$('#' + area_id).waitMe({
        effect: 'ios',
        text: message + '...',
        bg: 'rgba(255,255,255,0.7)',
        color:'#014576'
    });*/
}
function hideMask(area_id){
    /*$('#' + area_id).waitMe('hide');*/
}