1 using System;
2 using System.Globalization;
3 using OpenBveApi.Interface;
4 using Prism.Mvvm;
5 using TrainEditor2.Systems;
6 
7 namespace TrainEditor2.Models.Panels
8 {
9 	internal enum SubjectBase
10 	{
11 		Acc,
12 		Atc,
13 		Ats,
14 		Bc,
15 		LocoBrakeCylinder,
16 		Bp,
17 		LocoBrakePipe,
18 		Brake,
19 		LocoBrake,
20 		Csc,
21 		Door,
22 		DoorL,
23 		DoorR,
24 		DoorButtonL,
25 		DoorButtonR,
26 		Er,
27 		Hour,
28 		Kmph,
29 		Min,
30 		Motor,
31 		Mph,
32 		Mr,
33 		Ms,
34 		Power,
35 		Rev,
36 		Sap,
37 		Sec,
38 		True,
39 		Klaxon,
40 		PrimaryKlaxon,
41 		SecondaryKlaxon,
42 		MusicKlaxon
43 	}
44 
45 	internal enum SubjectSuffix
46 	{
47 		None,
48 		D
49 	}
50 
51 	internal class Subject : BindableBase, ICloneable
52 	{
53 		private SubjectBase _base;
54 		private int baseOption;
55 		private SubjectSuffix suffix;
56 		private int suffixOption;
57 
58 		internal SubjectBase Base
59 		{
60 			get
61 			{
62 				return _base;
63 			}
64 			set
65 			{
66 				SetProperty(ref _base, value);
67 			}
68 		}
69 
70 		internal int BaseOption
71 		{
72 			get
73 			{
74 				return baseOption;
75 			}
76 			set
77 			{
78 				SetProperty(ref baseOption, value);
79 			}
80 		}
81 
82 		internal SubjectSuffix Suffix
83 		{
84 			get
85 			{
86 				return suffix;
87 			}
88 			set
89 			{
90 				SetProperty(ref suffix, value);
91 			}
92 		}
93 
94 		internal int SuffixOption
95 		{
96 			get
97 			{
98 				return suffixOption;
99 			}
100 			set
101 			{
102 				SetProperty(ref suffixOption, value);
103 			}
104 		}
105 
Subject()106 		internal Subject()
107 		{
108 			Base = SubjectBase.True;
109 			BaseOption = -1;
110 			Suffix = SubjectSuffix.None;
111 			SuffixOption = -1;
112 		}
113 
ToString()114 		public override string ToString()
115 		{
116 			string result = Base.ToString();
117 
118 			switch (Base)
119 			{
120 				case SubjectBase.Ats:
121 				case SubjectBase.DoorL:
122 				case SubjectBase.DoorR:
123 					if (BaseOption >= 0)
124 					{
125 						result += BaseOption;
126 					}
127 					break;
128 			}
129 
130 			if (Suffix != SubjectSuffix.None)
131 			{
132 				result += Suffix.ToString();
133 
134 				if (SuffixOption >= 0)
135 				{
136 					result += SuffixOption;
137 				}
138 			}
139 
140 			return result;
141 		}
142 
Clone()143 		public object Clone()
144 		{
145 			return MemberwiseClone();
146 		}
147 
StringToSubject(string value, string errorLocation)148 		internal static Subject StringToSubject(string value, string errorLocation)
149 		{
150 			Subject result = new Subject();
151 
152 			CultureInfo culture = CultureInfo.InvariantCulture;
153 
154 			// detect d# suffix
155 			{
156 				int i;
157 
158 				for (i = value.Length - 1; i >= 0; i--)
159 				{
160 					int a = char.ConvertToUtf32(value, i);
161 
162 					if (a < 48 | a > 57)
163 					{
164 						break;
165 					}
166 				}
167 
168 				if (i >= 0 & i < value.Length - 1)
169 				{
170 					if (value[i] == 'd' | value[i] == 'D')
171 					{
172 						result.Suffix = SubjectSuffix.D;
173 						int n;
174 
175 						if (int.TryParse(value.Substring(i + 1), NumberStyles.Integer, culture, out n))
176 						{
177 							result.SuffixOption = n;
178 							value = value.Substring(0, i);
179 						}
180 					}
181 				}
182 			}
183 
184 			// transform subject
185 			SubjectBase _base;
186 			bool ret = Enum.TryParse(value, true, out _base);
187 			result.Base = _base;
188 
189 			if (!ret)
190 			{
191 				bool unsupported = true;
192 
193 				if (value.StartsWith("ats", StringComparison.OrdinalIgnoreCase))
194 				{
195 					string a = value.Substring(3);
196 					int n;
197 
198 					if (int.TryParse(a, NumberStyles.Integer, culture, out n))
199 					{
200 						result.Base = SubjectBase.Ats;
201 						result.BaseOption = n;
202 						unsupported = false;
203 					}
204 				}
205 				else if (value.StartsWith("doorl", StringComparison.OrdinalIgnoreCase))
206 				{
207 					string a = value.Substring(5);
208 					int n;
209 
210 					if (int.TryParse(a, NumberStyles.Integer, culture, out n))
211 					{
212 						result.Base = SubjectBase.DoorL;
213 						result.BaseOption = n;
214 						unsupported = false;
215 					}
216 				}
217 				else if (value.StartsWith("doorr", StringComparison.OrdinalIgnoreCase))
218 				{
219 					string a = value.Substring(5);
220 					int n;
221 
222 					if (int.TryParse(a, NumberStyles.Integer, culture, out n))
223 					{
224 						result.Base = SubjectBase.DoorR;
225 						result.BaseOption = n;
226 						unsupported = false;
227 					}
228 				}
229 
230 				if (unsupported)
231 				{
232 					result.Base = SubjectBase.True;
233 					Interface.AddMessage(MessageType.Error, false, $"Invalid subject {value} encountered in {errorLocation}");
234 				}
235 			}
236 
237 			return result;
238 		}
239 	}
240 }
241