using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using QC.QsprTestObjects.Parameters;

namespace QC.QTMApplication;

public class OutputParameterValueDataTemplateSelector : DataTemplateSelector
{
	private DataRowView currentDataRowView;

	public DataTemplate OutputParameterDataTableEditCellDataTemplate { get; set; }

	public DataTemplate OutputArrayEditCellDataTemplate { get; set; }

	public DataTemplate DefaultCellEditTemplate { get; set; }

	public override DataTemplate SelectTemplate(object item, DependencyObject container)
	{
		if (item != null && item is DataRowView dataRowView)
		{
			currentDataRowView = dataRowView;
			if (!Convert.IsDBNull(dataRowView.Row["Parameter_Object"]) && dataRowView.Row["Parameter_Object"] is ParameterCls parameterCls)
			{
				if (IsArrayParameter(parameterCls))
				{
					return OutputArrayEditCellDataTemplate;
				}
				if (parameterCls.IsParamDataTable())
				{
					return OutputParameterDataTableEditCellDataTemplate;
				}
			}
		}
		return DefaultCellEditTemplate;
	}

	private bool IsArrayParameter(ParameterCls param)
	{
		if (!param.IsParamDataTable())
		{
			if (param.NoOfElements <= 1)
			{
				return param.IsDynamicArray;
			}
			return true;
		}
		return false;
	}
}
