using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CategoryTheory;
using DiagramUI.Interfaces;
using DiagramUI.Labels;
using DiagramUI;
using TestCategory.Interfaces;
namespace DataPerformer.TestInterface.UI.UserConrols
{
public partial class UserControlDataPerformerTest : UserControl
{
#region Fields
TestData test;
List<object> l = new List<object>();
Dictionary<string, object[]> output;
IComponentCollection collection;
event Action<object, DataRow, TestData> onAdd = (object o, DataRow row, TestData td) => { };
event Action<object, ICategoryObject, bool[]> isAdmissible = (object l, ICategoryObject o, bool[] b) => { b[0] = false; };
private Func<TestData> createTest;
#endregion
#region Ctor
public UserControlDataPerformerTest()
{
InitializeComponent();
}
#endregion
#region Public
public event Action<object, DataRow, TestData> OnAdd
{
add { onAdd += value; }
remove { onAdd -= value; }
}
public event Action<object, ICategoryObject, bool[]> OnAdmissible
{
add { isAdmissible += value; }
remove { isAdmissible -= value; }
}
public Func<TestData> CreateTestData
{
get
{
return createTest;
}
set
{
createTest = value;
}
}
#endregion
#region Protected
protected void Add(object o, DataRow row, TestData test)
{
IObjectLabel l = o as IObjectLabel;
ICategoryObject co = l.Object;
string name = l.GetName(collection);
if (co is global::Regression.AliasRegression)
{
test.AddRegression(name, (uint)row[3], collection);
return;
}
if (o is IProperties)
{
object ob = (o as IProperties).Properties;
if (ob.GetType().Equals(typeof(DataPerformerUI.Labels.GraphLabel)))
{
DataPerformerUI.Interfaces.IGraphLabel lab = ob as DataPerformerUI.Labels.GraphLabel;
DataPerformer.DataConsumer cons = co as DataPerformer.DataConsumer;
test.AddChart(name, cons.Start, cons.Step, cons.Steps, lab.Argument,
lab.Colors.Keys.ToArray<string>(), collection);
return;
}
}
onAdd(o, row, test);
}
protected virtual bool IsAdmissible(object o, ICategoryObject cob)
{
if (cob is global::Regression.AliasRegression)
{
return true;
}
if (o is IProperties)
{
object ob = (o as IProperties).Properties;
if (ob.GetType().Equals(typeof(DataPerformerUI.Labels.GraphLabel)))
{
return true;
}
}
bool[] b = new bool[1];
isAdmissible(o, cob, b);
return b[0];
}
protected void Add(object o, DataRow row)
{
Add(o, row, test);
}
#endregion
#region Internal & Private
internal bool HasTests(ITest test, IComponentCollection collection)
{
List<object> l = new List<object>();
IEnumerable<object> c = collection.AllComponents;
foreach (object o in c)
{
string name = "";
IObjectLabel ol = null;
ICategoryObject cob = null;
if (o is IObjectLabel)
{
ol = o as IObjectLabel;
cob = ol.Object;
name = ol.GetName(collection);
}
if (IsAdmissible(o, cob))
{
return true;
}
}
return false;
}
internal void Set(ITest test, IComponentCollection collection)
{
this.collection = collection;
if (test != null)
{
this.test = test as TestData;
output = this.test.Output;
}
IEnumerable<object> c = collection.AllComponents;
foreach (object o in c)
{
string name = "";
IObjectLabel ol = null;
ICategoryObject cob = null;
if (o is IObjectLabel)
{
ol = o as IObjectLabel;
cob = ol.Object;
name = ol.GetName(collection);
}
if (IsAdmissible(o, cob))
{
l.Add(o);
}
}
List<string> ls = new List<string>();
List<Control> controls = new List<Control>();
uint i = 1;
uint j = 1;
foreach (object o in l)
{
int num;
int count;
Create(o, out num, out count);
object[] ob = new object[] { i, (o as IObjectLabel).GetName(collection), (uint)num, (uint)count };
dataTable.Rows.Add(ob);
++i;
}
}
internal bool Set(int i)
{
if (i == 0)
{
return CreateTest();
}
if (i == 2)
{
test = null;
}
return true;
}
internal ITest Test
{
get
{
return test;
}
}
private void Create(object o, out int num, out int count)
{
num = 0;
count = 1;
if (output != null)
{
string name = (o as IObjectLabel).GetName(collection);
if (output.ContainsKey(name))
{
object[] ob = output[name];
num = (int)ob[0] + 1;
if (ob.Length > 1)
{
count = (int)ob[1];
}
}
}
}
private bool CreateTest()
{
Dictionary<uint, DataRow> d = new Dictionary<uint, DataRow>();
Dictionary<uint, int> di = new Dictionary<uint, int>();
int i = 0;
foreach (DataRow row in dataTable.Rows)
{
uint n = (uint)row[2];
if (n > 0)
{
if (di.ContainsKey(n))
{
return false;
}
di[n] = i;
d[n] = row;
}
++i;
}
List<uint> ll = new List<uint>();
ll.AddRange(di.Keys);
ll.Sort();
test = createTest();
foreach (uint ik in ll)
{
Add(l[di[ik]], d[ik]);
}
test.Close();
return true;
}
#endregion
}
}