1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using AutoMapper ;
4+ using AvaloniaControls . ControlServices ;
5+ using AvaloniaControls . Services ;
6+ using MSURandomizer . ViewModels ;
7+ using MSURandomizerLibrary . Services ;
8+
9+ namespace MSURandomizer . Services ;
10+
11+ public class UnknownMsuWindowService (
12+ IMsuUserOptionsService userOptionsService ,
13+ IMapper mapper ,
14+ IMsuTypeService msuTypeService ,
15+ IMsuCacheService msuCacheService ,
16+ IMsuLookupService msuLookupService ) : ControlService
17+ {
18+ private UnknownMsuWindowViewModel _model = new ( ) ;
19+ private const int MaxPathLength = 50 ;
20+
21+ public UnknownMsuWindowViewModel InitilizeModel ( )
22+ {
23+ List < string > msuTypes = [ "" ] ;
24+ msuTypes . AddRange ( msuTypeService . MsuTypes . Select ( x => x . DisplayName ) . OrderBy ( x => x ) ) ;
25+
26+ List < MsuDetailsWindowViewModel > msuModels = [ ] ;
27+
28+ foreach ( var msu in msuLookupService . Msus . Where ( x => x is { IgnoreUnknown : false , MsuType : null , NumUniqueTracks : >= 15 } && string . IsNullOrEmpty ( x . Settings . MsuTypeName ) ) )
29+ {
30+ var msuModel = mapper . Map < MsuDetailsWindowViewModel > ( msu . Settings ) ;
31+ msuModel . Msu = msu ;
32+ msuModel . TrackCount = msu . NumUniqueTracks ;
33+ msuModel . MsuTypeNames = msuTypes ;
34+ msuModel . HasBeenModified = false ;
35+
36+ if ( msuModel . MsuPath . Length > MaxPathLength )
37+ {
38+ msuModel . AbbreviatedPath = $ "...{ msuModel . MsuPath . Substring ( msuModel . MsuPath . Length - MaxPathLength ) } ";
39+ }
40+ else
41+ {
42+ msuModel . AbbreviatedPath = msuModel . MsuPath ;
43+ }
44+
45+ msuModel . PropertyChanged += ( sender , args ) =>
46+ {
47+ _model . HasBeenModified = true ;
48+ } ;
49+ msuModels . Add ( msuModel ) ;
50+ }
51+
52+ msuModels . Last ( ) . IsNotLast = false ;
53+
54+ _model . UnknownMsus = msuModels ;
55+ _model . HasBeenModified = false ;
56+
57+ return _model ;
58+ }
59+
60+ public void Save ( )
61+ {
62+ foreach ( var msuModel in _model . UnknownMsus . Where ( x => x . Msu != null ) )
63+ {
64+ mapper . Map ( msuModel , msuModel . Msu ! . Settings ) ;
65+ msuModel . Msu . Settings . IsUserUnknownMsu = string . IsNullOrEmpty ( msuModel . MsuTypeName ) ;
66+ userOptionsService . UpdateMsuSettings ( msuModel . Msu ) ;
67+ msuCacheService . Remove ( msuModel . Msu . Path , false ) ;
68+ }
69+
70+ userOptionsService . Save ( ) ;
71+
72+ ITaskService . Run ( ( ) =>
73+ {
74+ msuLookupService . LookupMsus ( ) ;
75+ } ) ;
76+
77+ }
78+
79+ public void SaveIgnore ( )
80+ {
81+ foreach ( var msuModel in _model . UnknownMsus . Where ( x => x . Msu != null ) )
82+ {
83+ msuModel . Msu ! . Settings . IsUserUnknownMsu = true ;
84+ userOptionsService . UpdateMsuSettings ( msuModel . Msu ) ;
85+ }
86+
87+ userOptionsService . Save ( ) ;
88+ }
89+ }
0 commit comments