using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace QC.QMine.QMineInterface;

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class FileSystemItemsGroupingCollection : BindableObject
{
	private bool isQtmData;

	private readonly string defaultGroupName = "<Default group>";

	private ObservableCollection<FileSystemItemsGroup> dataFilesGroupList = new ObservableCollection<FileSystemItemsGroup>();

	public bool IsQtmData
	{
		get
		{
			return isQtmData;
		}
		set
		{
			if (isQtmData != value)
			{
				isQtmData = value;
				OnPropertyChanged("IsQtmData");
			}
		}
	}

	public bool HasDefaultGroup => HasDefaultGroupInCollection();

	public string DefaultGroupName => defaultGroupName;

	[XmlElement("FileSystemItemsGroupList", Form = XmlSchemaForm.Unqualified)]
	public ObservableCollection<FileSystemItemsGroup> FileSystemItemsGroupList
	{
		get
		{
			return dataFilesGroupList;
		}
		set
		{
			if (dataFilesGroupList != value)
			{
				dataFilesGroupList = value;
				OnPropertyChanged("FileSystemItemsGroupList");
			}
		}
	}

	private bool HasDefaultGroupInCollection()
	{
		if (FileSystemItemsGroupList != null && FileSystemItemsGroupList.Count > 0 && FileSystemItemsGroupList.Any((FileSystemItemsGroup grp) => grp.GroupName == defaultGroupName))
		{
			return true;
		}
		return false;
	}

	public FileSystemItemsGroupingCollection()
	{
		FileSystemItemsGroupList.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		FileSystemItemsGroupList.CollectionChanged += FileSystemItemsGroupList_CollectionChanged;
	}

	public void AddDefaultGroup()
	{
		FileSystemItemsGroupList.CollectionChanged -= FileSystemItemsGroupList_CollectionChanged;
		FileSystemItemsGroupList.Add(new FileSystemItemsGroup
		{
			GroupName = DefaultGroupName,
			IsSelected = true
		});
		FileSystemItemsGroupList.CollectionChanged += FileSystemItemsGroupList_CollectionChanged;
	}

	private void FileSystemItemsGroupList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		OnPropertyChanged("FileSystemItemsGroupList");
	}

	public static void WriteFileSystemItemsGroupingCollection(FileSystemItemsGroupingCollection fileFolderGrouping, string filePath)
	{
		CommonFunctions.Serialize(filePath, fileFolderGrouping);
		fileFolderGrouping.ResetChange();
		fileFolderGrouping.HasObjectChanged = false;
	}

	public static FileSystemItemsGroupingCollection ReadFileSystemItemsGroupingCollection(string filePath)
	{
		return CommonFunctions.Deserialize<FileSystemItemsGroupingCollection>(filePath);
	}

	public void ResetGroupingCollection()
	{
		FileSystemItemsGroupList.Clear();
	}

	public List<FileItem> GetFiles()
	{
		List<FileItem> list = new List<FileItem>();
		foreach (FileSystemItemsGroup fileSystemItemsGroup in FileSystemItemsGroupList)
		{
			list.AddRange(fileSystemItemsGroup.GetFiles());
		}
		return list;
	}
}
