Files
BeWoPlaner/BeWo/ownChat/ChatView.xaml.cs
2020-04-14 11:49:16 +02:00

317 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using BeWo.Core.Service;
using BeWo.ServiceProxy;
using BeWo.View;
using BeWo.ViewModel;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BS.Shared.Translation;
using ChatController.ChatKlassen;
using ChatController.LoginKlassen;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;
using Panel = System.Windows.Controls.Panel;
namespace BeWo.ownChat
{
public partial class ChatView
{
public ChatEmojiView EmojiView { get; set; }
public ChatView(ChatDatenUebergabe chatDaten)
{
InitializeComponent();
CloseWindow();
ChatMainControl.InitMitChatdaten(chatDaten);
ChatMainControl.AddContextMenu("In Dokumentation übernehmen", ChatMainControl_OnClickContextMenuItem);
LadeMessageInfoAusDatenBank();
}
private void RootGroupBox_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
DragMove();
}
catch (Exception)
{
}
}
private void CloseWindow()
{
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) =>
{
HideWindowsAndSaveMessageInfo();
}));
}
private void HideWindowsAndSaveMessageInfo()
{
Visibility = Visibility.Hidden;
EmojiView?.Close();
SpeichereMessageInfosInDatenBank();
}
private void ChatMainControl_OnOnEmoji(Button emojiButton)
{
if (EmojiView == null)
{
EmojiView = new ChatEmojiView(ChatMainControl);
var x = emojiButton.PointToScreen(new Point(0, 0));
var source = PresentationSource.FromVisual(emojiButton);
EmojiView.Top = x.Y / source.CompositionTarget.TransformToDevice.M22 - EmojiView.Height - 5;
EmojiView.Left = x.X / source.CompositionTarget.TransformToDevice.M11;
EmojiView.Show();
}
else
{
EmojiView.Close();
EmojiView = null;
EmojiView = new ChatEmojiView(ChatMainControl);
var x = emojiButton.PointToScreen(new Point(0, 0));
var source = PresentationSource.FromVisual(emojiButton);
EmojiView.Top = x.Y / source.CompositionTarget.TransformToDevice.M22 - EmojiView.Height - 5;
EmojiView.Left = x.X / source.CompositionTarget.TransformToDevice.M11;
EmojiView.Show();
}
}
private void ChatMainControl_OnClickContextMenuItem(string menuItem)
{
var selectedContact = ChatMainControl.GetCurrentContact();
if (selectedContact != null)
{
var itemListe = ChatMainControl.GetSelectedMessages();
if (itemListe != null)
{
// TODO: Mike fragen
// Z.B. "C5" -> alles nach dem C bei Klienten
if(!string.IsNullOrEmpty(selectedContact.UserIdManage))
{
var rawCustomerOid = selectedContact.UserIdManage.Substring(1);
var customerOid = long.Parse(rawCustomerOid);
ServiceFacade.DoCustomerServiceAsync(s => s.GetCompactCustomerDC(customerOid), compactCustomer =>
{
if(compactCustomer == null)
{
MessageBox.Show("Der/die ausgewählte Klient/in wurde nicht gefunden.", "Dokumentieren nicht möglich", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
this.Dispatch(delegate
{
OpenServiceRecordEditView(itemListe, compactCustomer);
});
});
}
}
else
{
MessageBox.Show("Bitte Nachricht auswählen", "Nachricht auswählen", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else
{
MessageBox.Show("Es können nur Nachrichten von KlientInnen dokumentiert werden.", "Dokumentieren nicht möglich", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private void OpenServiceRecordEditView(List<ChatMessage> messages, CompactCustomerDC compactCustomer)
{
messages.Sort((a, b) => a.SendTime.CompareTo(b.SendTime));
var stringBuilder = new StringBuilder();
foreach (var m in messages)
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append(Environment.NewLine);
}
stringBuilder.Append($"{m.SendTime:dd.MM.yyyy HH:mm} - {m.Username}: {m.UserMessage}");
}
var utf8 = Encoding.UTF8;
var encodedBytes = utf8.GetBytes(stringBuilder.ToString());
var convertedBytes = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes);
var ascii = Encoding.ASCII;
var finalString = ascii.GetString(convertedBytes);
var firstTime = messages.Min(r =>
{
var dt = r.SendTime;
dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
return dt;
});
var lastTime = messages.Max(r =>
{
var dt = r.SendTime;
dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
return dt;
});
Cache.GetInstance().GetActiveSupportConceptCostbearer(scList =>
VMFactory.CreateServiceRecordVMAsync(
BeWoApp.LoggedOnUser.Employee.EmployeeOid,
vm => this.Dispatch(
delegate
{
vm.DispatcherObject = this;
var dc = SupportConceptService.ErstelleServiceRecordDC(firstTime, lastTime, compactCustomer, scList);
if (vm.PrototypeVM.SortedCategories != null && vm.PrototypeVM.SortedCategories.Count > 0)
{
var cat1 = vm.PrototypeVM.SortedCategories[0];
dc.ServiceDescription = vm.PrototypeVM.Category2Services[cat1][0];
}
var serviceRecord = new ServiceRecordVM(dc, vm.PrototypeVM.Category2Services, vm.PrototypeVM.AllGoalCategories, vm.PrototypeVM.AllGoals)
{
Notice = finalString
};
if (serviceRecord.SupportConcept == null)
{
MessageBox.Show(
Translator.Translate("Es wurde kein passender Hilfeplan gefunden."),
"ownChat Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation);
return;
}
var newView = new ChatDokumentationsView(vm, serviceRecord);
newView.ShowDialog();
})));
}
#region Fenstergrößenverstellung
internal void Normal()
{
WindowState = (WindowState) FormWindowState.Normal;
}
internal void Maximize()
{
WindowState = (WindowState)FormWindowState.Maximized;
}
internal void Minimize()
{
WindowState = (WindowState)FormWindowState.Minimized;
}
#endregion
#region Speichere/Lade MessageSync In DB
private void SpeichereMessageInfosInDatenBank()
{
var liste = ChatMainControl.GetContactsWithoutUnreadMessages();
if (liste.Count != 0)
{
var text = liste.Aggregate(string.Empty, (current, item) => current + item.GroupId + ";" + item.TimeStamp + ";" + "&");
ServiceFacade.DoEmployeeServiceSync(r => r.CreateOrUpdateChatBewoMessageSync(BeWoApp.LoggedOnUser.Employee.EmployeeOid,text));
}
}
private void LadeMessageInfoAusDatenBank()
{
var item = ServiceFacade.DoEmployeeServiceSync(r => r.GetChatBewoMessageSync(BeWoApp.LoggedOnUser.Employee.EmployeeOid));
if (item != null)
{
var messageSynctext = item.MessageSyncText.Split('&');
var aa = new Dictionary<long, DateTime>();
foreach (var text in messageSynctext)
{
if (!string.IsNullOrEmpty(text)) {
var splitten = text.Split(';');
aa.Add(Convert.ToInt64(splitten[0]), DateTime.Parse(splitten[1]));
}
}
ChatMainControl.ResetNumberOfUnreadMessages(aa);
}
}
#endregion
#region Waitlayer
private WaitLayer2 _WaitLayer;
public void StartWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)StartWaitingImmediately);
}
public void StartWaitingImmediately()
{
if (_WaitLayer == null)
{
_WaitLayer = new WaitLayer2();
ChatViewGrid.Children.Add(_WaitLayer);
Panel.SetZIndex(_WaitLayer, int.MaxValue);
_WaitLayer.RefreshChatUI();
}
}
public void EndWaiting()
{
Dispatcher.BeginInvoke(
DispatcherPriority.Background,
(Action)delegate
{
if (_WaitLayer != null)
{
ChatViewGrid.Children.Remove(_WaitLayer);
_WaitLayer = null;
}
});
}
#endregion
private void ChatView_OnClosing(object sender, CancelEventArgs e)
{
HideWindowsAndSaveMessageInfo();
e.Cancel = true;
}
}
}