public DepartmentNotification Save(DepartmentNotification notification)
        {
            _departmentNotificationRepository.SaveOrUpdate(notification);

            return(notification);
        }
        public bool ValidateNotificationForProcessing(ProcessedNotification notification, DepartmentNotification setting)
        {
            //dynamic dynamicData = JsonConvert.DeserializeObject(notification.Data);
            NotificationItem dynamicData = ObjectSerialization.Deserialize <NotificationItem>(notification.Data);

            switch (notification.Type)
            {
            case EventTypes.UnitStatusChanged:
                if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData))
                {
                    if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1"))
                    {
                        return(true);
                    }

                    bool beforeAny  = setting.BeforeData.Contains("-1");
                    bool currentAny = setting.CurrentData.Contains("-1");

                    UnitState beforeState  = null;
                    UnitState currentState = null;

                    currentState = _unitsService.GetUnitStateById((int)dynamicData.StateId);

                    if (!beforeAny)
                    {
                        beforeState = _unitsService.GetLastUnitStateBeforeId(currentState.UnitId, currentState.UnitStateId);
                    }

                    if ((currentAny || currentState.State == int.Parse(setting.CurrentData)) &&
                        (beforeAny || beforeState.State == int.Parse(setting.BeforeData)))
                    {
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.PersonnelStaffingChanged:
                if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData))
                {
                    if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1"))
                    {
                        return(true);
                    }

                    bool beforeAny  = setting.BeforeData.Contains("-1");
                    bool currentAny = setting.CurrentData.Contains("-1");

                    UserState beforeState  = null;
                    UserState currentState = null;

                    currentState = _userStateService.GetUserStateById((int)dynamicData.StateId);

                    if (!beforeAny)
                    {
                        beforeState = _userStateService.GetPerviousUserState(currentState.UserId, currentState.UserStateId);
                    }

                    if ((currentAny || currentState.State == int.Parse(setting.CurrentData)) &&
                        (beforeAny || beforeState.State == int.Parse(setting.BeforeData)))
                    {
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.PersonnelStatusChanged:
                if (!String.IsNullOrWhiteSpace(setting.BeforeData) && !String.IsNullOrWhiteSpace(setting.CurrentData))
                {
                    if (setting.BeforeData.Contains("-1") && setting.CurrentData.Contains("-1"))
                    {
                        return(true);
                    }

                    bool beforeAny  = setting.BeforeData.Contains("-1");
                    bool currentAny = setting.CurrentData.Contains("-1");

                    ActionLog beforeState  = null;
                    ActionLog currentState = null;

                    currentState = _actionLogsService.GetActionlogById((int)dynamicData.StateId);

                    if (!beforeAny)
                    {
                        beforeState = _actionLogsService.GetPreviousActionLog(currentState.UserId, currentState.ActionLogId);
                    }

                    if ((currentAny || currentState.ActionTypeId == int.Parse(setting.CurrentData)) &&
                        (beforeAny || beforeState.ActionTypeId == int.Parse(setting.BeforeData)))
                    {
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.RolesInGroupAvailabilityAlert:
                if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data))
                {
                    int count            = 0;
                    var userStateChanged = _userStateService.GetUserStateById((int)dynamicData.StateId);
                    var usersInRole      = _personnelRolesService.GetAllMembersOfRole(int.Parse(setting.Data));
                    var group            = _departmentGroupsService.GetGroupForUser(userStateChanged.UserId, notification.DepartmentId);

                    if (group == null || group.Members == null || !group.Members.Any())
                    {
                        return(false);
                    }

                    var acceptableStaffingLevels = setting.CurrentData.Split(char.Parse(","));

                    if (usersInRole != null && !usersInRole.Any())
                    {
                        return(false);
                    }

                    var staffingLevels = _userStateService.GetLatestStatesForDepartment(setting.DepartmentId);

                    if (staffingLevels != null && staffingLevels.Any())
                    {
                        foreach (var user in usersInRole)
                        {
                            var currentState = staffingLevels.FirstOrDefault(x => x.UserId == user.UserId);

                            if (currentState != null && acceptableStaffingLevels.Any(x => x == currentState.State.ToString()) &&
                                group.Members.Any(x => x.UserId == user.UserId))
                            {
                                count++;
                            }
                        }

                        if (count <= setting.LowerLimit)
                        {
                            notification.PersonnelRoleTargeted = int.Parse(setting.Data);
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.RolesInDepartmentAvailabilityAlert:
                if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data))
                {
                    int count       = 0;
                    var usersInRole = _personnelRolesService.GetAllMembersOfRole(int.Parse(setting.Data));
                    var acceptableStaffingLevels = setting.CurrentData.Split(char.Parse(","));

                    if (usersInRole != null && !usersInRole.Any())
                    {
                        return(false);
                    }

                    var staffingLevels = _userStateService.GetLatestStatesForDepartment(setting.DepartmentId);

                    if (staffingLevels != null && staffingLevels.Any())
                    {
                        foreach (var user in usersInRole)
                        {
                            var currentState = staffingLevels.FirstOrDefault(x => x.UserId == user.UserId);

                            if (currentState != null && acceptableStaffingLevels.Any(x => x == currentState.State.ToString()))
                            {
                                count++;
                            }
                        }

                        if (count <= setting.LowerLimit)
                        {
                            notification.PersonnelRoleTargeted = int.Parse(setting.Data);
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.UnitTypesInGroupAvailabilityAlert:
                if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data))
                {
                    int count            = 0;
                    var currentUnitState = _unitsService.GetUnitStateById((int)dynamicData.StateId);
                    var unitsForType     = _unitsService.GetAllUnitsForType(setting.DepartmentId, setting.Data);
                    var unitForEvent     = _unitsService.GetUnitById(currentUnitState.UnitId);

                    if (unitForEvent?.StationGroupId == null)
                    {
                        return(false);
                    }

                    var acceptableUnitStates = setting.CurrentData.Split(char.Parse(","));
                    var unitsInGroup         = _unitsService.GetAllUnitsForGroup(unitForEvent.StationGroupId.Value);

                    if (unitsForType != null && !unitsForType.Any())
                    {
                        return(false);
                    }

                    var staffingLevels = _unitsService.GetAllLatestStatusForUnitsByDepartmentId(setting.DepartmentId);

                    foreach (var unit in unitsForType)
                    {
                        var currentState = staffingLevels.FirstOrDefault(x => x.UnitId == unit.UnitId);

                        if (currentState != null && acceptableUnitStates.Any(x => x == currentState.State.ToString()) && unitsInGroup.Any(x => x.UnitId == unit.UnitId))
                        {
                            count++;
                        }
                    }

                    if (count <= setting.LowerLimit)
                    {
                        notification.UnitTypeTargeted = setting.Data;
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            case EventTypes.UnitTypesInDepartmentAvailabilityAlert:
                if (!String.IsNullOrWhiteSpace(setting.CurrentData) && !String.IsNullOrWhiteSpace(setting.Data))
                {
                    int count                = 0;
                    var unitsForType         = _unitsService.GetAllUnitsForType(setting.DepartmentId, setting.Data);
                    var acceptableUnitStates = setting.CurrentData.Split(char.Parse(","));

                    if (unitsForType != null && !unitsForType.Any())
                    {
                        return(false);
                    }

                    var staffingLevels = _unitsService.GetAllLatestStatusForUnitsByDepartmentId(setting.DepartmentId);

                    foreach (var unit in unitsForType)
                    {
                        var currentState = staffingLevels.FirstOrDefault(x => x.UnitId == unit.UnitId);

                        if (currentState != null && acceptableUnitStates.Any(x => x == currentState.State.ToString()))
                        {
                            count++;
                        }
                    }

                    if (count <= setting.LowerLimit)
                    {
                        notification.UnitTypeTargeted = setting.Data;
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
                break;

            default:
                return(true);
            }

            return(false);
        }