2017-09-05 09:23:53 +02:00
using System ;
using System.Collections.Generic ;
2017-11-23 10:58:07 +01:00
using System.ComponentModel ;
2017-12-17 17:29:07 +01:00
using System.Linq ;
using System.Text ;
2017-09-05 09:23:53 +02:00
using System.Windows ;
2017-10-06 12:06:34 +02:00
using System.Windows.Forms ;
2017-09-05 09:23:53 +02:00
using System.Windows.Input ;
2017-12-17 17:29:07 +01:00
using BeWo.Core.Service ;
2017-09-05 09:23:53 +02:00
using BeWo.ServiceProxy ;
2017-12-17 17:29:07 +01:00
using BeWo.ViewModel ;
2017-09-05 09:23:53 +02:00
using BS.Shared.DataContracts.Compact ;
2017-12-17 17:29:07 +01:00
using BS.Shared.Extensions ;
using BS.Shared.Translation ;
2017-09-05 09:23:53 +02:00
using ChatController.ChatKlassen ;
2019-04-17 17:01:37 +02:00
using ChatController.LoginKlassen ;
2017-10-06 12:06:34 +02:00
using Button = System . Windows . Controls . Button ;
using MessageBox = System . Windows . MessageBox ;
2017-09-05 09:23:53 +02:00
2017-11-13 10:59:42 +01:00
namespace BeWo.ownChat
2017-09-05 09:23:53 +02:00
{
2017-12-12 17:20:23 -04:00
public partial class ChatView
2017-09-05 09:23:53 +02:00
{
2020-04-14 11:49:16 +02:00
public ChatEmojiView EmojiView { get ; set ; }
2017-09-05 09:23:53 +02:00
2019-03-29 16:56:49 +01:00
public ChatView ( ChatDatenUebergabe chatDaten )
2017-09-05 09:23:53 +02:00
{
InitializeComponent ( ) ;
CloseWindow ( ) ;
2019-03-29 16:56:49 +01:00
ChatMainControl . InitMitChatdaten ( chatDaten ) ;
2017-09-05 09:23:53 +02:00
ChatMainControl . AddContextMenu ( "In Dokumentation übernehmen" , ChatMainControl_OnClickContextMenuItem ) ;
2017-10-06 12:06:34 +02:00
2017-11-09 12:18:50 +01:00
LadeMessageInfoAusDatenBank ( ) ;
2017-09-05 09:23:53 +02:00
}
private void RootGroupBox_OnMouseLeftButtonDown ( object sender , MouseButtonEventArgs e )
{
try
{
DragMove ( ) ;
}
2020-04-14 11:49:16 +02:00
catch ( Exception )
{
}
2017-09-05 09:23:53 +02:00
}
private void CloseWindow ( )
{
CommandBindings . Add ( new CommandBinding ( ApplicationCommands . Close , ( s , e ) = >
2017-11-23 10:58:07 +01:00
{
HideWindowsAndSaveMessageInfo ( ) ;
} ) ) ;
}
2017-09-05 09:23:53 +02:00
2017-11-23 10:58:07 +01:00
private void HideWindowsAndSaveMessageInfo ( )
{
Visibility = Visibility . Hidden ;
2017-09-05 09:23:53 +02:00
2020-04-14 11:49:16 +02:00
EmojiView ? . Close ( ) ;
2017-11-23 10:58:07 +01:00
SpeichereMessageInfosInDatenBank ( ) ;
2017-09-05 09:23:53 +02:00
}
private void ChatMainControl_OnOnEmoji ( Button emojiButton )
{
2023-03-03 21:54:36 +01:00
if ( EmojiView is null )
2017-09-05 09:23:53 +02:00
{
2020-04-14 11:49:16 +02:00
EmojiView = new ChatEmojiView ( ChatMainControl ) ;
2017-09-05 09:23:53 +02:00
var x = emojiButton . PointToScreen ( new Point ( 0 , 0 ) ) ;
2017-12-12 17:20:23 -04:00
var source = PresentationSource . FromVisual ( emojiButton ) ;
2017-09-05 09:23:53 +02:00
2023-03-03 21:54:36 +01:00
if ( ! ( source is null ) )
{
EmojiView . Top = x . Y / source . CompositionTarget . TransformToDevice . M22 - EmojiView . Height - 5 ;
EmojiView . Left = x . X / source . CompositionTarget . TransformToDevice . M11 ;
}
2017-09-05 09:23:53 +02:00
2020-04-14 11:49:16 +02:00
EmojiView . Show ( ) ;
2017-09-05 09:23:53 +02:00
}
else
{
2020-04-14 11:49:16 +02:00
EmojiView . Close ( ) ;
EmojiView = null ;
2017-09-05 09:23:53 +02:00
2020-04-14 11:49:16 +02:00
EmojiView = new ChatEmojiView ( ChatMainControl ) ;
2017-12-12 17:20:23 -04:00
var x = emojiButton . PointToScreen ( new Point ( 0 , 0 ) ) ;
var source = PresentationSource . FromVisual ( emojiButton ) ;
2017-09-05 09:23:53 +02:00
2023-03-03 21:54:36 +01:00
if ( ! ( source is null ) )
{
EmojiView . Top = x . Y / source . CompositionTarget . TransformToDevice . M22 - EmojiView . Height - 5 ;
EmojiView . Left = x . X / source . CompositionTarget . TransformToDevice . M11 ;
}
2017-09-08 15:09:48 +02:00
2020-04-14 11:49:16 +02:00
EmojiView . Show ( ) ;
2017-09-05 09:23:53 +02:00
}
}
2021-03-17 18:44:40 +01:00
// TODO: Für Mitarbeiter implementieren
2017-12-12 17:20:23 -04:00
private void ChatMainControl_OnClickContextMenuItem ( string menuItem )
2017-09-05 09:23:53 +02:00
{
2021-04-06 08:06:51 +02:00
var selectedContact = ChatMainControl . GetCurrentContactWithUserIdManage ( ) ;
2017-09-05 09:23:53 +02:00
2019-07-01 15:11:19 +02:00
if ( selectedContact ! = null )
2017-09-05 09:23:53 +02:00
{
var itemListe = ChatMainControl . GetSelectedMessages ( ) ;
if ( itemListe ! = null )
2017-12-17 17:29:07 +01:00
{
2019-07-01 15:11:19 +02:00
// TODO: Mike fragen
// Z.B. "C5" -> alles nach dem C bei Klienten
2017-09-05 09:23:53 +02:00
2019-07-01 15:11:19 +02:00
if ( ! string . IsNullOrEmpty ( selectedContact . UserIdManage ) )
2017-09-05 09:23:53 +02:00
{
2019-07-01 15:11:19 +02:00
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 ) ;
} ) ;
} ) ;
2017-09-05 09:23:53 +02:00
}
}
else
{
2017-11-23 10:58:07 +01:00
MessageBox . Show ( "Bitte Nachricht auswählen" , "Nachricht auswählen" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
2017-09-05 09:23:53 +02:00
}
}
else
{
2017-12-15 10:47:43 +01:00
MessageBox . Show ( "Es können nur Nachrichten von KlientInnen dokumentiert werden." , "Dokumentieren nicht möglich" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ;
2017-12-17 17:29:07 +01:00
}
}
private void OpenServiceRecordEditView ( List < ChatMessage > messages , CompactCustomerDC compactCustomer )
{
messages . Sort ( ( a , b ) = > a . SendTime . CompareTo ( b . SendTime ) ) ;
2020-04-14 11:49:16 +02:00
var stringBuilder = new StringBuilder ( ) ;
2017-12-17 17:29:07 +01:00
foreach ( var m in messages )
{
2020-04-14 11:49:16 +02:00
if ( stringBuilder . Length > 0 )
2017-12-17 17:29:07 +01:00
{
2020-04-14 11:49:16 +02:00
stringBuilder . Append ( Environment . NewLine ) ;
2017-12-17 17:29:07 +01:00
}
2020-04-14 11:49:16 +02:00
stringBuilder . Append ( $"{m.SendTime:dd.MM.yyyy HH:mm} - {m.Username}: {m.UserMessage}" ) ;
2017-12-17 17:29:07 +01:00
}
2023-02-09 15:17:33 +01:00
var finalString = stringBuilder . ToString ( ) ;
finalString = finalString . Replace ( "ẞ" , "ß" ) ;
2017-12-17 17:29:07 +01:00
var firstTime = messages . Min ( r = >
{
2020-04-14 11:49:16 +02:00
var dt = r . SendTime ;
2017-12-17 17:29:07 +01:00
dt = new DateTime ( dt . Year , dt . Month , dt . Day , dt . Hour , dt . Minute , 0 ) ;
return dt ;
} ) ;
var lastTime = messages . Max ( r = >
{
2020-04-14 11:49:16 +02:00
var dt = r . SendTime ;
2017-12-17 17:29:07 +01:00
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 ;
}
2021-09-23 18:44:18 +02:00
serviceRecord . SetInsertedOn ( DateTime . Now ) ;
serviceRecord . EditAlwaysAllowed = true ;
2017-12-17 17:29:07 +01:00
var newView = new ChatDokumentationsView ( vm , serviceRecord ) ;
newView . ShowDialog ( ) ;
} ) ) ) ;
2017-09-05 09:23:53 +02:00
}
2017-10-06 12:06:34 +02:00
internal void Normal ( )
{
2020-04-14 11:49:16 +02:00
WindowState = ( WindowState ) FormWindowState . Normal ;
2017-10-06 12:06:34 +02:00
}
internal void Maximize ( )
{
2020-04-14 11:49:16 +02:00
WindowState = ( WindowState ) FormWindowState . Maximized ;
2017-10-06 12:06:34 +02:00
}
internal void Minimize ( )
{
2020-04-14 11:49:16 +02:00
WindowState = ( WindowState ) FormWindowState . Minimized ;
2017-10-06 12:06:34 +02:00
}
2017-11-09 12:18:50 +01:00
private void SpeichereMessageInfosInDatenBank ( )
{
2019-04-17 17:01:37 +02:00
var liste = ChatMainControl . GetContactsWithoutUnreadMessages ( ) ;
2017-11-09 12:18:50 +01:00
2023-03-03 21:54:36 +01:00
if ( liste . Count = = 0 )
2017-11-09 12:18:50 +01:00
{
2023-03-03 21:54:36 +01:00
return ;
2017-11-09 12:18:50 +01:00
}
2023-03-03 21:54:36 +01:00
var text = liste . Aggregate ( string . Empty , ( current , item ) = > current + item . GroupId + ";" + item . TimeStamp + ";" + "&" ) ;
ServiceFacade . DoEmployeeServiceSync ( r = > r . CreateOrUpdateChatBewoMessageSync ( BeWoApp . LoggedOnUser . Employee . EmployeeOid , text ) ) ;
2017-11-09 12:18:50 +01:00
}
private void LadeMessageInfoAusDatenBank ( )
{
var item = ServiceFacade . DoEmployeeServiceSync ( r = > r . GetChatBewoMessageSync ( BeWoApp . LoggedOnUser . Employee . EmployeeOid ) ) ;
2023-03-03 21:54:36 +01:00
if ( item is null )
{
return ;
}
var messageSynctext = item . MessageSyncText . Split ( '&' ) ;
2017-11-09 12:18:50 +01:00
2023-03-03 21:54:36 +01:00
var aa = new Dictionary < long , DateTime > ( ) ;
2017-11-09 12:18:50 +01:00
2023-03-03 21:54:36 +01:00
foreach ( var text in messageSynctext )
{
if ( string . IsNullOrEmpty ( text ) )
2017-11-09 12:18:50 +01:00
{
2023-03-03 21:54:36 +01:00
continue ;
2017-11-09 12:18:50 +01:00
}
2023-03-03 21:54:36 +01:00
var splitten = text . Split ( ';' ) ;
aa . Add ( Convert . ToInt64 ( splitten [ 0 ] ) , DateTime . Parse ( splitten [ 1 ] ) ) ;
2017-11-09 12:18:50 +01:00
}
2023-03-03 21:54:36 +01:00
ChatMainControl . ResetNumberOfUnreadMessages ( aa ) ;
2017-11-09 12:18:50 +01:00
}
2017-11-16 15:37:29 +01:00
2017-11-23 10:58:07 +01:00
private void ChatView_OnClosing ( object sender , CancelEventArgs e )
{
HideWindowsAndSaveMessageInfo ( ) ;
e . Cancel = true ;
}
2017-09-05 09:23:53 +02:00
}
}