using Comal.Classes;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using InABox.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using InABox.Configuration;
using System.Runtime.CompilerServices;
namespace PRSDesktop;
public class DigitalFormsLibrarySettings : IUserConfigurationSettings
{
public DynamicSplitPanelView View { get; set; }
public double AnchorWidth { get; set; }
public DigitalFormsLibrarySettings()
{
View = DynamicSplitPanelView.Combined;
AnchorWidth = 700;
}
}
///
/// Interaction logic for DigitalFormsLibrary.xaml
///
public partial class DigitalFormsLibrary : UserControl, IPanel, INotifyPropertyChanged
{
private string? CurrentGroup = null;
private bool AllItems = true;
public List> GroupList { get; private set; }
private bool _refreshing;
private DigitalFormsLibrarySettings _settings;
public event PropertyChangedEventHandler? PropertyChanged;
private bool _detailsEnabled;
public bool DetailsEnabled
{
get => _detailsEnabled;
set
{
_detailsEnabled = value;
OnPropertyChanged();
}
}
public DigitalFormsLibrary()
{
InitializeComponent();
Variables.Bind(IsEnabledProperty, this, x => x.DetailsEnabled, mode: BindingMode.OneWay);
Layouts.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
Reports.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
Documents.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
}
private void DoLoadSettings()
{
_settings = new UserConfiguration().Load();
FormsSplitPanel.View = _settings.View;
FormsSplitPanel.AnchorWidth = _settings.AnchorWidth;
}
private void DoSaveSettings()
{
_settings.View = FormsSplitPanel.View;
_settings.AnchorWidth = FormsSplitPanel.AnchorWidth;
new UserConfiguration().Save(_settings);
}
#region Panel Stuff
public bool IsReady { get; set; }
public string SectionName => "Digital Forms Library";
public event DataModelUpdateEvent? OnUpdateDataModel;
public Dictionary Selected()
{
return new Dictionary {
{ typeof(DigitalForm).EntityName(), Forms.SelectedRows }
};
}
public DataModel DataModel(Selection selection)
{
var ids = Forms.ExtractValues(x => x.ID, selection).ToArray();
return new AutoDataModel(Filter.Where(x => x.ID).InList(ids));
}
public void CreateToolbarButtons(IPanelHost host)
{
}
public void Setup()
{
DoLoadSettings();
Variables.LayoutGrid = Layouts;
Layouts.VariableGrid = Variables;
Reports.VariableGrid = Variables;
Reports.LayoutGrid = Layouts;
Forms.Refresh(true, false);
Variables.Refresh(true, false);
Layouts.Refresh(true, false);
Reports.Refresh(true, false);
Documents.Refresh(true, false);
RolesTab.Visibility = Security.CanView()
&& Security.CanView()
&& Security.CanView() ? Visibility.Visible : Visibility.Collapsed;
var visibleTabItems = Tab.Items.OfType().Where(x => x.Visibility == Visibility.Visible).ToList();
if (visibleTabItems.Count <= 1)
{
foreach (var tab in visibleTabItems)
{
tab.Visibility = Visibility.Collapsed;
Tab.SelectedItem = tab;
}
}
}
private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource != Tab) return;
RefreshCurrentTab();
}
private void RefreshCurrentTab()
{
if (Tab.SelectedTab == FormsTab)
{
RefreshFormsTab();
}
else if (Tab.SelectedTab == RolesTab)
{
RefreshRoleTab();
}
}
private void RefreshFormsTab()
{
Forms.Refresh(false, true);
}
private void RefreshRoleTab()
{
DigitalFormRoles.Refresh(true, true);
}
public void Refresh()
{
RefreshCurrentTab();
}
public void Heartbeat(TimeSpan time)
{
}
public void Shutdown(CancelEventArgs? cancel)
{
}
#endregion
private void Forms_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
{
var form = e.Rows?.FirstOrDefault()?.ToObject();
DetailsEnabled = form is not null;
Layouts.Form = form;
Layouts.Refresh(false, true);
Variables.Form = form;
Variables.Refresh(false, true);
Reports.Form = form;
Reports.Refresh(false, true);
Documents.Item = form;
Documents.Refresh(false, true);
}
private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_refreshing || e.OriginalSource != Groups)
return;
string? newCurrentGroup;
if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
{
AllItems = true;
newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
}
else
{
AllItems = false;
var selected = (Tuple)e.AddedItems[0];
newCurrentGroup = selected.Item2;
}
if (!Equals(newCurrentGroup, CurrentGroup))
{
CurrentGroup = newCurrentGroup;
Forms.Refresh(false, false);
}
}
private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args)
{
if (Forms.MasterData is null)
return;
_refreshing = true;
GroupList = new List>
{
new Tuple("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage())
};
foreach (var row in Forms.MasterData!.Rows)
{
var appliesTo = row.Get(x => x.AppliesTo);
var display = appliesTo.NotWhiteSpaceOr("Unassigned");
if (!GroupList.Any(x => x.Item1.Equals(display)))
GroupList.Add(new Tuple(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage()));
}
GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
Groups.ItemsSource = GroupList;
Groups.Visibility = Visibility.Visible;
var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup));
if(index == -1)
{
index = 0;
CurrentGroup = null;
Groups.SelectedIndex = 0;
AllItems = true;
_refreshing = false;
Forms.Refresh(false, false);
}
else
{
Groups.SelectedIndex = index;
AllItems = index == 0;
_refreshing = false;
}
}
private void Forms_OnCreateItem(object sender, object item)
{
if (item is not DigitalForm form) return;
form.AppliesTo = CurrentGroup ?? "";
}
private bool Forms_OnFilterRecord(CoreRow row)
{
return AllItems || Equals(row.Get(x => x.AppliesTo), CurrentGroup);
}
private void DynamicSplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
{
DoSaveSettings();
if (FormsSplitPanel.IsDetailVisible())
{
Layouts.Refresh(false, true);
Variables.Refresh(false, true);
}
}
private void OnPropertyChanged([CallerMemberName] string? property = null)
{
PropertyChanged?.Invoke(this, new(property));
}
}