using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprTestObjects.Tests.Base;
using QC.QsprTestObjects.Tests.TestCase;
using QC.QTMInterface.Business_Objects;

namespace QC.QTMApplication;

public class TestPlanDragDrop
{
	private TTDWpfTreeView _treeView;

	public TestPlanDragDrop(TTDWpfTreeView treeView)
	{
		_treeView = treeView;
	}

	public TestPlanMaster CreateTestPlanData(RootTest test, string fileName)
	{
		_treeView._scheduler.Registry.UpdateDLLVersions();
		TestPlanMaster testPlanMaster = new TestPlanMaster();
		testPlanMaster.Name = test.Properties.TestName;
		TestPlanMaster testPlanMaster2 = testPlanMaster;
		if (string.IsNullOrWhiteSpace(testPlanMaster2.Name))
		{
			testPlanMaster2.Name = test.GetRootTest().RealName;
		}
		if (testPlanMaster2.Name.Length < 5)
		{
			testPlanMaster2.Name += " TestPlan";
		}
		if (!(test.RootProperties is TestPlanProperties))
		{
			test.RootProperties = new TestPlanProperties(test.RootProperties);
		}
		if (test.RootProperties is TestPlanProperties)
		{
			(test.RootProperties as TestPlanProperties).SetAsTestPlan();
			testPlanMaster2.Id = (test.Properties as TestPlanProperties).TestPlanMasterID;
			if (!string.IsNullOrWhiteSpace((test.Properties as TestPlanProperties).TestPlanName))
			{
				testPlanMaster2.Name = (test.Properties as TestPlanProperties).TestPlanName;
			}
			if (string.IsNullOrWhiteSpace(testPlanMaster2.Name))
			{
				testPlanMaster2.Name = test.TestPath.Replace(".0", "").Replace(".", "");
			}
		}
		_treeView.SaveAsFile(fileName, saveAsLatestFormat: true);
		TestPlanVersion testPlanVersion = null;
		try
		{
			TestPlanVersion testPlanVersion2 = new TestPlanVersion();
			testPlanVersion2.XmlData = GenerateXmlData(fileName);
			testPlanVersion2.MasterId = testPlanMaster2.Id;
			testPlanVersion2.TestCases = new List<TestCaseVersion>();
			testPlanVersion2.TestCaseMastersToAdd = new List<TestCaseMaster>();
			testPlanVersion2.DynamicLimits = new DynamicLimitsData
			{
				ExcelFile = test.RootProperties.DynamicLimitsFileExcel,
				XmlFile = test.RootProperties.DynamicLimitsFileXML
			};
			testPlanVersion = testPlanVersion2;
			if (!string.IsNullOrWhiteSpace(test.RootProperties.DynamicLimitsFileExcel) && File.Exists(test.RootProperties.DynamicLimitsFileExcel))
			{
				testPlanVersion.DynamicLimits.ExcelData = File.ReadAllBytes(test.RootProperties.DynamicLimitsFileExcel);
			}
			if (!string.IsNullOrWhiteSpace(test.RootProperties.DynamicLimitsFileXML) && File.Exists(test.RootProperties.DynamicLimitsFileXML))
			{
				testPlanVersion.DynamicLimits.XmlData = File.ReadAllBytes(test.RootProperties.DynamicLimitsFileXML);
			}
		}
		catch (IOException ex)
		{
			AppMessageDialog.Show(ex.Message, "Error opening file", DialogResult.OK, MessageDialogType.Error);
			throw ex;
		}
		if (test.RootProperties is TestPlanProperties)
		{
			testPlanVersion.Id = (test.Properties as TestPlanProperties).TestPlanVersionID;
			Enum.TryParse<ApprovalStates>((test.Properties as TestPlanProperties).Status, out var _);
			testPlanVersion.CreatedBy = (test.Properties as TestPlanProperties).CreatedBy;
		}
		testPlanMaster2.Versions = new List<TestPlanVersion> { testPlanVersion };
		if (test != null)
		{
			List<TestCaseMaster> list = new List<TestCaseMaster>();
			foreach (BaseTest test2 in test.TestList)
			{
				AddTestCases(test2, list);
			}
			foreach (TestCaseMaster item in list)
			{
				testPlanMaster2.LatestVersion.TestCaseMastersToAdd.Add(item);
			}
		}
		return testPlanMaster2;
	}

	private static void AddTestCases(BaseTest test, List<TestCaseMaster> testCases)
	{
		if (test is TestCaseTest)
		{
			TestCaseMaster testCaseMaster = new TestCaseMaster();
			testCaseMaster.Id = ((TestCaseTestProperties)test.Properties).TestCaseMasterID;
			testCaseMaster.Name = ((TestCaseTestProperties)test.Properties).TestCaseName;
			TestCaseMaster testCaseMaster2 = testCaseMaster;
			testCaseMaster2.Versions.Add(new TestCaseVersion
			{
				Id = ((TestCaseTestProperties)test.Properties).TestCaseID
			});
			testCaseMaster2.LatestVersion.ApprovalState = ((TestCaseTestProperties)test.Properties).Status;
			testCases.Add(testCaseMaster2);
		}
		else
		{
			if (!(test is FolderTest))
			{
				return;
			}
			FolderTest folderTest = (FolderTest)test;
			foreach (BaseTest test2 in folderTest.TestList)
			{
				AddTestCases(test2, testCases);
			}
		}
	}

	private static string GenerateXmlData(string fileName)
	{
		using StreamReader streamReader = new StreamReader(fileName);
		return streamReader.ReadToEnd();
	}
}
