using System;
using System.Collections.Generic;
using QC.CTDS.DM.ApplicationsManager;
using QC.CTDS.DM.DataStructures;
using QC.CTDS.DM.DataStructures.InputDataStructure;
using QC.CTDS.DM.DataStructures.OutputDataStructure;

namespace QC.DataminerController;

public class DataminerController
{
	private ReportGenerator dataminerReportGenerator;

	private IDataminerInputDataBuilder dataminerInputDataBuilder;

	private string formatDescriptorFilePath;

	private string limitDescriptorFilePath;

	private InputData dataminerInputData;

	private ReportSettings reportSettings = new ReportSettings();

	private List<string> enabledChartTypes = new List<string>();

	public ReportSettings DataminerReportSettings
	{
		get
		{
			return reportSettings;
		}
		set
		{
			reportSettings = value;
		}
	}

	public List<string> EnabledChartTypes
	{
		get
		{
			return enabledChartTypes;
		}
		set
		{
			enabledChartTypes = value;
		}
	}

	public DataminerController(IDataminerInputDataBuilder dataminerInputDataBuilder, string formatDescriptorFilePath, string limitDescriptorFilePath = null)
	{
		if (dataminerInputDataBuilder == null)
		{
			throw new ArgumentException("DataminerInputDataBuilder cannot be null");
		}
		dataminerReportGenerator = new ReportGenerator();
		this.dataminerInputDataBuilder = dataminerInputDataBuilder;
		this.dataminerInputDataBuilder.SetReportGenerator(dataminerReportGenerator);
		dataminerReportGenerator.ReportSettingsInfo = DataminerReportSettings;
		this.formatDescriptorFilePath = formatDescriptorFilePath;
		this.limitDescriptorFilePath = limitDescriptorFilePath;
		LoadDescriptors();
	}

	private void LoadDescriptors()
	{
		if (!string.IsNullOrEmpty(formatDescriptorFilePath))
		{
			dataminerReportGenerator.LoadDescriptor(formatDescriptorFilePath);
		}
		if (!string.IsNullOrEmpty(limitDescriptorFilePath))
		{
			dataminerReportGenerator.LoadLimitDescriptor(limitDescriptorFilePath);
		}
	}

	public Dictionary<string, DescriptorAppliedData> GetOutputData(string outputPlugin, bool clearInputData)
	{
		if (string.IsNullOrEmpty(outputPlugin))
		{
			throw new ArgumentException("OutputPlugin name cannot be null or empty");
		}
		LoadInputData();
		dataminerReportGenerator.EnableChartTypes(enabledChartTypes);
		Dictionary<string, DescriptorAppliedData> result = dataminerReportGenerator.GenerateOutputDataAfterPluginProcessing(outputPlugin);
		if (clearInputData)
		{
			dataminerInputData.Clear();
		}
		return result;
	}

	private void LoadInputData()
	{
		dataminerInputData = dataminerInputDataBuilder.GetDataminerInputData();
		if (dataminerInputData != null)
		{
			dataminerReportGenerator.LoadSourceData(dataminerInputData);
		}
	}

	public void GenerateOutput(string outputPlugin, string outputFilePath, bool clearInputData)
	{
		if (string.IsNullOrEmpty(outputPlugin))
		{
			throw new ArgumentException("OutputPlugin name cannot be null or empty");
		}
		LoadInputData();
		dataminerReportGenerator.EnableChartTypes(enabledChartTypes);
		dataminerReportGenerator.GenerateReport(outputPlugin, outputFilePath, clearInputData);
	}
}
