aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Engine/ShiftFS/ShiftDirectory.cs
blob: 249738f10dbc088a30010665d8638b950e1c4f18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Whoa;

namespace ShiftOS.Engine.ShiftFS
{
	[Serializable]
	public class ShiftDirectory : List<IShiftNode>, IShiftNode
	{
		public ShiftDirectory(string name) => Name = name;
		public ShiftDirectory(string name, ShiftDirectory parent)
		{
			Name = name;
			Parent = parent;
		}


		public IShiftNode this[string name] => this.First(n => string.Equals(n.Name, name, StringComparison.Ordinal));

		
		public string Name { get; set; }

		public IEnumerable<ShiftFile> Flatten()
		{
			foreach (var item in this)
			{
				switch (item)
				{
					case ShiftFile file:
						yield return file;
						break;
					case ShiftDirectory dir:
						foreach (var shiftNode in dir.Flatten())
						{
							yield return shiftNode;
						}
						break;
				}
			}
		}

		public IEnumerable<ShiftDirectory> FlattenFolders()
		{
			foreach (var item in this)
			{
				if (!(item is ShiftDirectory dir)) continue;
				yield return dir;

				foreach (var subdir in dir.FlattenFolders())
				{
					yield return subdir;
				}
			}
		}
	 
		public string FullName
		{
			get
			{
				var list = new List<string> { Name };
				var currentNode = Parent;
				while (currentNode?.Parent != null )
				{
					list.Add(currentNode.Name);
					currentNode = currentNode.Parent;
				}
				
				return Path.Combine(list.Reverse<string>().ToArray());
			}
		}

		public ShiftDirectory Parent
		{
			get => Drive.FlattenFolders().FirstOrDefault(x => x.Contains(this));
			set
			{
				value.Add(this);
				Parent?.Remove(this);
			}
		}

		public ShiftTree Drive => ShiftFS.Drives.First(d => d.FlattenFolders().Contains(this));

		
		public Guid Guid { get; } = Guid.NewGuid();
	}
}