using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using InABox.Clients;
using InABox.Core;
using InABox.Mail;
using InABox.Wpf;
using InABox.WPF;
using Microsoft.Win32;
namespace PRSDesktop
{
///
/// Interaction logic for EmailForm.xaml
///
public partial class EmailForm : ThemableWindow
{
private readonly ICoreMailMessage _message;
private readonly DataModel _model;
private readonly List> attachments = new();
public EmailForm(ICoreMailMessage message, DataModel model = null)
{
_message = message;
_model = model;
InitializeComponent();
EmailImage.Source = PRSDesktop.Resources.email.AsBitmapImage(32, 32);
Subject.Text = _message.Subject;
Body.Text = _message.Body;
To.Text = _message.To != null ? string.Join("; ", message.To) : "";
Body.SetColor(Colors.LightYellow);
if (_message.Attachments != null)
attachments.AddRange(_message.Attachments);
Attachments.ItemsSource = attachments;
ApplyTemplate.Visibility = model != null ? Visibility.Visible : Visibility.Collapsed;
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += (o, e) =>
{
var bEnabled = !string.IsNullOrWhiteSpace(To.Text) && !string.IsNullOrWhiteSpace(Subject.Text) &&
!string.IsNullOrWhiteSpace(Body.Text);
if (bEnabled != Send.IsEnabled)
Send.IsEnabled = bEnabled;
};
timer.IsEnabled = true;
}
public double Zoom
{
get => Body.ZoomFactor;
set => Body.ZoomFactor = value;
}
private void SendClick(object sender, RoutedEventArgs e)
{
_message.To = To.Text.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
//_message.CC = CC.Text.Split(new String[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
//_message.BCC = BCC.Text.Split(new String[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
_message.Subject = Subject.Text;
_message.Body = Body.Text;
_message.Attachments = attachments.ToArray();
DialogResult = true;
Close();
}
private void AddAttachment_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Title = "Select Attachment";
dlg.Filter = "All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
attachments.Add(new Tuple(Path.GetFileName(dlg.FileName), File.ReadAllBytes(dlg.FileName)));
Attachments.ItemsSource = null;
Attachments.ItemsSource = attachments;
}
private void DeleteAttachment_Click(object sender, RoutedEventArgs e)
{
var attachment = Attachments.SelectedItem as Tuple;
if (attachment != null)
{
attachments.Remove(attachment);
Attachments.ItemsSource = null;
Attachments.ItemsSource = attachments;
}
}
private void ApplyTemplate_Click(object sender, RoutedEventArgs e)
{
var templates = new Client().Query(new Filter(x => x.Model).IsEqualTo(_model.Name));
if (templates.Rows.Any())
{
var context = new ContextMenu();
foreach (var row in templates.Rows)
{
var menu = new MenuItem
{
Header = row.Get(x => x.Name),
Tag = row
};
menu.Click += (o, args) =>
{
var cur = ((MenuItem)o).Tag as CoreRow;
var data = cur.Get(x => x.Template);
try
{
Body.Text = DataModelUtils.ParseTemplate(_model, data);
}
catch (Exception err)
{
MessageBox.Show("Unable to Parse Template!\n\n" + err.Message);
}
};
context.Items.Add(menu);
}
context.Items.Add(new Separator());
var item = new MenuItem
{
Header = "Manage Templates",
Tag = null
};
item.Click += (o, args) =>
{
var list = new DataModelTemplateForm(_model);
list.ShowDialog();
};
context.Items.Add(item);
context.PlacementTarget = ApplyTemplate;
context.IsOpen = true;
}
else
{
var list = new DataModelTemplateForm(_model);
list.ShowDialog();
}
}
}
}