using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.Rpc;
using InABox.Wpf;
using System;
using System.Collections.Generic;
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.Shapes;
namespace PRSDesktop.Forms.Issues;
///
/// Interaction logic for IssuesWindow.xaml
///
public partial class IssuesWindow : Window
{
public IQueryProviderFactory ClientFactory
{
set => Grid.ClientFactory = value;
}
public IssuesWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Grid.Refresh(true, true);
}
private static Guid? _customerID = null;
private static bool CheckCustomerID()
{
if (_customerID is not null)
return true;
var license = Client.Query(new Filter().All(), Columns.None().Add(x => x.Data))
.ToObjects().FirstOrDefault();
if (license is not null && LicenseUtils.TryDecryptLicense(license.Data, out var result, out var error))
{
_customerID = result.CustomerID;
return true;
}
return false;
}
private static RpcClientSocketTransport? _transport = null;
private static bool CheckTransport()
{
if (_transport is not null)
return true;
var transport = new RpcClientSocketTransport(["remote.prsdigital.com.au:8006"]);
var client = new RpcClient(transport);
if(client.Validate("SYSTEM", "DO!pt%Qi!R0_h@LW", Guid.Empty).Status != InABox.Clients.ValidationStatus.VALID)
return false;
_transport = transport;
return true;
// var transport = new RpcClientSocketTransport(["127.0.0.1:8000"]);
// var client = new RpcClient(transport);
// if(client.Validate("frank", "frank", Guid.Empty).Status != InABox.Clients.ValidationStatus.VALID)
// {
// MessageWindow.ShowMessage("Could not connect to PRS digital database.", "Connection error.");
// return;
// }
}
public static bool Check()
{
if (!CheckCustomerID())
return false;
if (!CheckTransport())
return false;
var client = new RpcClient(_transport!);
var waiting = client.Query(
new Filter(x => x.Closed).IsEqualTo(Guid.Empty)
.And(x => x.Status).IsEqualTo(KanbanStatus.Waiting)
.And(x=>x.JobLink.Customer.ID).IsEqualTo(_customerID!.Value)
.And(x=>x.CreatedBy).IsEqualTo(App.EmployeeName),
Columns.None().Add(x => x.LastUpdate),
new SortOrder(x => x.LastUpdate, SortDirection.Descending),
CoreRange.Database(1)
).Rows.Any();
return waiting;
}
public static void Execute()
{
if (!CheckCustomerID())
{
MessageWindow.ShowMessage("Could not load issues: no customer ID", "Error");
return;
}
if (!CheckTransport())
{
MessageWindow.ShowMessage("Could not connect to PRS digital database.", "Connection error.");
return;
}
var issues = new IssuesWindow();
issues.Grid.CustomerID = _customerID!.Value;
issues.ClientFactory = new _Factory(_transport!);
issues.ShowDialog();
}
private class _Factory(IRpcClientTransport transport) : IQueryProviderFactory
{
public bool ExcludeCustomProperties => true;
IRpcClientTransport Transport = transport;
public InABox.Core.IQueryProvider Create(Type T)
{
var result = (IClient)Activator.CreateInstance(typeof(RpcClient<>).MakeGenericType(T), Transport);
result.ExcludeCustomProperties = ExcludeCustomProperties;
return result;
}
}
}