using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using TGS.Interface;
using TGS.Interface.Components;
namespace TGS.ControlPanel
{
///
/// Form used for managing manipulation functions
///
sealed partial class InstanceSelector : CountedForm
{
///
/// The we build instance connections from
///
readonly IServerInterface masterInterface;
///
/// List of from
///
IList InstanceData;
///
/// Used for modifying without invoking its side effects
///
bool UpdatingEnabledCheckbox = false;
///
/// Construct an
///
/// An connected a the
public InstanceSelector(IServerInterface I)
{
InitializeComponent();
InstanceListBox.MouseDoubleClick += InstanceListBox_MouseDoubleClick;
masterInterface = I;
RefreshInstances();
}
///
/// Connects to a if it is double clicked in
///
/// The sender of the event
/// The
void InstanceListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
TryConnectToIndexInstance(InstanceListBox.IndexFromPoint(e.Location));
}
///
/// Returns the associated with 's current selected index
///
/// The associated with 's current selected index if it exists, otherwise
InstanceMetadata GetSelectedInstanceMetadata()
{
var index = InstanceListBox.SelectedIndex;
return index != ListBox.NoMatches ? InstanceData[index] : null;
}
///
/// Called by
///
void Cleanup()
{
masterInterface.Dispose();
}
///
/// Loads the using
///
async void RefreshInstances()
{
InstanceListBox.Items.Clear();
await WrapServerOp(() => {
InstanceData = masterInterface.GetServiceComponent().ListInstances();
});
foreach(var I in InstanceData)
InstanceListBox.Items.Add(String.Format("{0}: {1} - {2} - {3}", I.LoggingID, I.Name, I.Path, I.Enabled ? "ONLINE" : "OFFLINE"));
var HasServerAdmin = masterInterface.ConnectionStatus().HasFlag(ConnectivityLevel.Administrator);
CreateInstanceButton.Enabled = HasServerAdmin;
ImportInstanceButton.Enabled = HasServerAdmin;
RenameInstanceButton.Enabled = HasServerAdmin;
DetachInstanceButton.Enabled = HasServerAdmin;
EnabledCheckBox.Enabled = HasServerAdmin;
if(InstanceData.Count > 0)
InstanceListBox.SelectedIndex = 0;
}
///
/// Tries to start a for a given
///
/// The of to connect to
async void TryConnectToIndexInstance(int index)
{
if (index == ListBox.NoMatches)
return;
var instanceName = InstanceData[index].Name;
if (ControlPanel.InstancesInUse.TryGetValue(instanceName, out ControlPanel activeCP))
{
activeCP.BringToFront();
return;
}
var InstanceAccessor = masterInterface.IsRemoteConnection ? new ServerInterface(masterInterface.LoginInfo) : new ServerInterface();
try
{
ConnectivityLevel res = ConnectivityLevel.None;
await WrapServerOp(() => { res = InstanceAccessor.ConnectToInstance(instanceName); });
if (!res.HasFlag(ConnectivityLevel.Connected))
{
MessageBox.Show("Unable to connect to instance! Does it exist?");
RefreshInstances();
}
else if (!res.HasFlag(ConnectivityLevel.Authenticated))
MessageBox.Show("The current user is not authorized to access this instance!");
else
new ControlPanel(InstanceAccessor).Show();
}
catch
{
InstanceAccessor.Dispose();
throw;
}
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void DetachInstanceButton_Click(object sender, EventArgs e)
{
var imd = GetSelectedInstanceMetadata();
if (imd == null)
return;
if (MessageBox.Show(String.Format("This will dissociate the server instance at \"{0}\"! Are you sure?", imd.Path), "Instance Detach", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
await WrapServerOp(() => res = masterInterface.GetServiceComponent().DetachInstance(imd.Name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Calls
///
/// The sender of the event
/// The
void RefreshButton_Click(object sender, EventArgs e)
{
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void RenameInstanceButton_Click(object sender, EventArgs e)
{
var imd = GetSelectedInstanceMetadata();
if (imd == null)
return;
var new_name = Program.TextPrompt("Instance Rename", "Enter a new name for the instance:");
if (new_name == null)
return;
if (imd.Enabled && MessageBox.Show(String.Format("This will temporarily offline the server instance! Are you sure?", imd.Path), "Instance Restart", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
await WrapServerOp(() => res = masterInterface.GetServiceComponent().RenameInstance(imd.Name, new_name));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void ImportInstanceButton_Click(object sender, EventArgs e)
{
var instance_path = Program.TextPrompt("Instance Import", "Enter the full path to the instance:");
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => res = masterInterface.GetServiceComponent().ImportInstance(instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Prompts the user for parameters to
///
/// The sender of the event
/// The
async void CreateInstanceButton_Click(object sender, EventArgs e)
{
var instance_name = Program.TextPrompt("Instance Creation", "Enter the name of the instance:");
if (instance_name == null)
return;
var instance_path = Program.TextPrompt("Instance Creation", "Enter the full path to the instance:");
if (instance_path == null)
return;
string res = null;
await WrapServerOp(() => res = masterInterface.GetServiceComponent().CreateInstance(instance_name, instance_path));
if (res != null)
MessageBox.Show(res);
RefreshInstances();
}
///
/// Attempts to connect the user to an based on the of
///
/// The sender of the event
/// The
void ConnectButton_Click(object sender, EventArgs e)
{
TryConnectToIndexInstance(InstanceListBox.SelectedIndex);
}
///
/// Prompts the user if they want to call to either online or offline an based on its current state
///
/// The sender of the event
/// The
async void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (UpdatingEnabledCheckbox)
return;
var enabling = EnabledCheckBox.Checked;
try
{
if (MessageBox.Show(String.Format("Are you sure you want to {0} this instance?", enabling ? "online" : "offline"), "Instance Status Change", MessageBoxButtons.YesNo) != DialogResult.Yes)
return;
string res = null;
var index = InstanceListBox.SelectedIndex;
await WrapServerOp(() => res = masterInterface.GetServiceComponent().SetInstanceEnabled(InstanceData[index].Name, enabling));
if (res != null)
MessageBox.Show(res);
}
finally
{
RefreshInstances();
}
}
///
/// Update based on the selected 's property
///
/// The sender of the event
/// The
void InstanceListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var index = InstanceListBox.SelectedIndex;
if (index != ListBox.NoMatches)
{
UpdatingEnabledCheckbox = true;
EnabledCheckBox.Checked = InstanceData[index].Enabled;
UpdatingEnabledCheckbox = false;
}
}
}
}