using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using BS.Shared.DataContracts; using BS.Shared.Extensions; namespace BeWo.ViewModel.ListViewModel { public abstract class AbstractDCListMapperVM : AbstractBaseVM where VMType : AbstractDCMapperVM where DCType : new() { public static string PropertyName_EditVM = "EditVM"; public static string PropertyName_NewVM = "NewVM"; private readonly ObservableCollection _DCBackup; private VMType _EditVM; private VMType _NewVM; private VMType _SelectedVM; private BindingList _VMList; //public AbstractDCListMapperVM(IEnumerable> listVMs, bool ignore) //{ // var dcLists = listVMs.SelectMany(x => x.DCBackup); // _DCBackup = dcLists != null ? dcLists.ToObservableCollection() : new ObservableCollection(); // _DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast().Select(CreateVM)); // var vmLists = listVMs.SelectMany(x => x.VMList).ToList(); // _VMList = new BindingList(vmLists); //} public AbstractDCListMapperVM(IEnumerable pDCList) { _DCBackup = pDCList != null ? pDCList.ToObservableCollection() : new ObservableCollection(); _DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast().Select(CreateVM)); } public AbstractDCListMapperVM() : this(null) { } public virtual int AddedCount { get { return this.VMList.Where(vm => vm.IsNew).Count(); } } public virtual int ChangedCount { get { return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Count(); } } public ObservableCollection DCBackup { get { return this._DCBackup; } } public VMType EditVM { get { return _EditVM; } set { _EditVM = value; FirePropertyChanged(PropertyName_EditVM); } } public VMType SelectedVM { get => _SelectedVM; set { _SelectedVM = value; FirePropertyChanged(nameof(SelectedVM)); } } public override bool IsDirty { get { return ChangedCount != 0 || RemovedCount != 0 || AddedCount != 0; } } public VMType NewVM { get { return _NewVM ?? (NewVM = CreateVM(new DCType())); } set { _NewVM = value; FirePropertyChanged(PropertyName_NewVM); } } public virtual int RemovedCount { get { return GetRemoved().Count; } } public BindingList VMList { get { if (_VMList == null) { _VMList = new BindingList(_DCBackup.Select(CreateVM).ToList()) { AllowNew = true }; _VMList.AddingNew += (s, e) => e.NewObject = CreateVM(new DCType()); } return _VMList; } set { _VMList = value; } } protected virtual Comparison Comparison { get { return null; } } public virtual VMType AddNewVMToList() { VMList.Add(NewVM); // VMList.Sort(); VMType lVM = NewVM; NewVM = null; //FirePropertyChanged("VMList"); return lVM; } public VMType AddNewVMToList(bool pSetAsEdit) { _VMList.Add(_NewVM); // _VMList.Sort(); if (pSetAsEdit) { EditVM = _NewVM; } NewVM = null; return EditVM; } public virtual VMType AddNewVMToListAndSelect(bool select) { // Wichtig! NewVM, wenn _NewVM null ist var vm = NewVM; VMList.Add(vm); // _VMList.Sort(); if (select) { SelectedVM = vm; } NewVM = null; FirePropertyChanged(nameof(VMList)); return vm; } public virtual List CommitAdded() { return this.VMList.Where(vm => vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList(); } public virtual List CommitChanged() { return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList(); } public virtual List CopyToDCList(bool doCommit) { if (doCommit && this._VMList != null) { return doCommit ? this._VMList.Select(vm => vm.CommitToDataContract()).ToList() : this._VMList.Select(i => i.CopyToEmptyDataContract()).ToList(); } return null; } public void DeleteEditVM() { this._VMList.Remove(this._EditVM); this.EditVM = null; } public bool DeleteSelectedVM(string displayname = null) => DeleteSelectedVM(SelectedVM, displayname); public bool DeleteSelectedVM(VMType remove, string displayname = null) { if (displayname is object) { var res = MessageBox.Show($"Möchten Sie {displayname} wirklich löschen?", "Löschen bestätigen", MessageBoxButton.YesNo); if (res != MessageBoxResult.Yes) return false; } VMList.Remove(remove); FirePropertyChanged(nameof(VMList)); return true; } public virtual List GetRemoved() { return this._DCBackup.Where(dc => !this._VMList.Select(vm => vm.DataContract).Contains(dc)).ToList(); } protected virtual VMType CreateVM(DCType pDC) { var lConstr = typeof(VMType).GetConstructor(new[] { typeof(DCType) }); return lConstr.Invoke(new object[] { pDC }) as VMType; } public void SetSelectByIndex(int index) { if(-1 < index && index < VMList.Count){ var x = VMList[index]; SelectedVM = x; } } public virtual string GetVisibleInformationString() { return null; } } }