Ejemplo n.º 1
0
        private void Save()
        {
            if (!HasNotes)
            {
                if (player == null)
                {
                    CloseAction?.Invoke();
                    return;
                }

                // check if notes exist because notes might be added on another table
                var notes = dataService.GetPlayerNotes(player.PlayerId);

                var manualNote = notes.FirstOrDefault(x => !x.IsAutoNote);

                if (manualNote == null)
                {
                    manualNote = new Playernotes
                    {
                        PlayerId    = player.PlayerId,
                        PokersiteId = player.PokersiteId,
                        Timestamp   = DateTime.UtcNow
                    };

                    playerNoteEntities.Add(manualNote);
                }

                manualNote.Note = Note;

                dataService.Store(manualNote);

                CloseAction?.Invoke();

                return;
            }

            // no auto
            if (playerNoteEntities.All(x => !x.IsAutoNote))
            {
                if (string.IsNullOrEmpty(Note))
                {
                    dataService.DeletePlayerNotes(playerNoteEntities);
                    playerNoteEntities.Clear();
                }
                else
                {
                    var manualNote = playerNoteEntities.FirstOrDefault();
                    manualNote.Note = Note;

                    dataService.Store(manualNote);
                }

                CloseAction?.Invoke();

                return;
            }

            var newNotes       = NoteBuilder.ParseNotes(Note);
            var manualNoteText = newNotes.Item1;

            // no auto
            if (string.IsNullOrEmpty(newNotes.Item2))
            {
                // delete auto notes if exists
                var notesToDelete = playerNoteEntities.Where(x => x.IsAutoNote).ToArray();
                dataService.DeletePlayerNotes(notesToDelete);

                notesToDelete.ForEach(x => playerNoteEntities.Remove(x));
            }
            else
            {
                // check if any auto notes
                if (playerNoteEntities.Any(x => x.IsAutoNote))
                {
                    var oldNotes = NoteBuilder.ParseNotes(initialNotes);

                    // if auto note were changed, then make them all manual
                    if (newNotes.Item2 != oldNotes.Item2)
                    {
                        // delete all auto notes because we move them to manual
                        var notesToDelete = playerNoteEntities.Where(x => x.IsAutoNote).ToArray();
                        dataService.DeletePlayerNotes(notesToDelete);
                        notesToDelete.ForEach(x => playerNoteEntities.Remove(x));

                        manualNoteText = Note;
                    }
                }
                else
                {
                    manualNoteText = Note;
                }
            }

            // no manual
            if (string.IsNullOrEmpty(manualNoteText))
            {
                // delete manual notes if exists
                var notesToDelete = playerNoteEntities.Where(x => !x.IsAutoNote).ToArray();
                dataService.DeletePlayerNotes(notesToDelete);
                notesToDelete.ForEach(x => playerNoteEntities.Remove(x));
            }
            else
            {
                var manualNote = playerNoteEntities.FirstOrDefault(x => !x.IsAutoNote);

                if (manualNote == null)
                {
                    if (player == null)
                    {
                        CloseAction?.Invoke();
                        return;
                    }

                    manualNote = new Playernotes
                    {
                        PlayerId    = player.PlayerId,
                        PokersiteId = player.PokersiteId,
                        Timestamp   = DateTime.UtcNow
                    };

                    playerNoteEntities.Add(manualNote);
                }

                manualNote.Note = manualNoteText;

                dataService.Store(manualNote);
            }


            CloseAction?.Invoke();
        }