Newer
Older
Qwilight / Qwilight / ViewModel / LevelViewModel.cs
@Taehui Taehui on 20 Aug 5 KB v1.16.39
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Qwilight.MSG;
using Qwilight.UIComponent;
using Qwilight.Utilities;
using System.Collections.ObjectModel;
using System.IO;
using Windows.Win32.UI.WindowsAndMessaging;

namespace Qwilight.ViewModel
{
    public sealed partial class LevelViewModel : BaseViewModel
    {
        public ObservableCollection<LevelItem> LevelItemCollection { get; } = new();

        public override double TargetLength => 0.4;

        public override double TargetHeight => 0.6;

        public void OnInput() => OnPropertyChanged(nameof(IsTotalWantLevelID));

        public bool IsTotalWantLevelID => LevelItemCollection.All(levelItem => levelItem.IsWanted);

        void SetLevelItemCollection()
        {
            LevelItemCollection.Clear();
            foreach (var levelID in LevelSystem.Instance.LevelIDCollection)
            {
                LevelItemCollection.Add(new LevelItem
                {
                    LevelID = levelID,
                    IsWanted = Configure.Instance.LastWantLevelIDs.Contains(levelID)
                });
            }
            OnPropertyChanged(nameof(IsTotalWantLevelID));
        }

        public void OnNewLevel()
        {
            if (IsLoaded)
            {
                LevelSystem.Instance.Load(true);
                SetLevelItemCollection();
            }
        }

        [RelayCommand]
        void OnTotalWantLevel(bool? e)
        {
            if (e.HasValue)
            {
                foreach (var levelItem in LevelItemCollection)
                {
                    levelItem.IsWanted = e.Value;
                }
                OnPropertyChanged(nameof(IsTotalWantLevelID));
            }
        }

        [RelayCommand]
        async Task OnLoadWww(string www)
        {
            if (string.IsNullOrEmpty(www))
            {
                var inputTextViewModel = ViewModels.Instance.InputTextValue;
                inputTextViewModel.Text = LanguageSystem.Instance.LevelInputContents;
                inputTextViewModel.Input = string.Empty;
                inputTextViewModel.HandleOK = new Action<string>(async text =>
                {
                    if (!string.IsNullOrEmpty(text))
                    {
                        if (await LevelSystem.Instance.GetWww(text))
                        {
                            LevelSystem.Instance.Load(true);
                            SetLevelItemCollection();
                        }
                    }
                });
                inputTextViewModel.Open();
            }
            else
            {
                if (await LevelSystem.Instance.GetWww(www))
                {
                    LevelSystem.Instance.Load(true);
                    SetLevelItemCollection();
                }
            }
        }

        [RelayCommand]
        async Task OnLoadFile()
        {
            var filePath = StrongReferenceMessenger.Default.Send(new ViewFileWindow
            {
                Filters = [".htm", ".html"]
            }).Response;
            if (!string.IsNullOrEmpty(filePath))
            {
                if (await LevelSystem.Instance.GetLevelNotifyItem(async levelNotifyItem => await LevelSystem.Instance.GetHTML(levelNotifyItem, await File.ReadAllTextAsync(filePath), string.Empty)))
                {
                    LevelSystem.Instance.Load(true);
                    SetLevelItemCollection();
                }
            }
        }

        [RelayCommand]
        async Task OnGetLevel()
        {
            if (Configure.Instance.LevelTargetMap.TryGetValue(Configure.Instance.LastWantLevelName, out var target) && !string.IsNullOrEmpty(target))
            {
                if (await LevelSystem.Instance.GetWww(target))
                {
                    LevelSystem.Instance.Load(true);
                    SetLevelItemCollection();
                }
            }
            else
            {
                NotifySystem.Instance.Notify(NotifySystem.NotifyVariety.Warning, NotifySystem.NotifyConfigure.Default, LanguageSystem.Instance.NotHaveLevelTarget);
            }
        }

        [RelayCommand]
        static void OnWipeLevel()
        {
            if (StrongReferenceMessenger.Default.Send(new ViewAllowWindow
            {
                Text = LanguageSystem.Instance.WipeLevelNotify,
                Data = MESSAGEBOX_STYLE.MB_YESNO | MESSAGEBOX_STYLE.MB_ICONQUESTION | MESSAGEBOX_STYLE.MB_DEFBUTTON1
            }) == MESSAGEBOX_RESULT.IDYES)
            {
                Utility.WipeFile(Path.Combine(LevelSystem.EntryPath, $"{Configure.Instance.LastWantLevelName}.json"));
                Utility.WipeFile(Path.Combine(LevelSystem.EntryPath, $"#{Configure.Instance.LastWantLevelName}.json"));
                Configure.Instance.LevelTargetMap.Remove(Configure.Instance.LastWantLevelName);
                var i = LevelSystem.Instance.LevelFileNames.IndexOf(Configure.Instance.LastWantLevelName);
                LevelSystem.Instance.LevelFileNames.RemoveAt(i);
                Configure.Instance.LastWantLevelName = LevelSystem.Instance.LevelFileNames.ElementAtOrDefault(i) ?? LevelSystem.Instance.LevelFileNames.LastOrDefault();
            }
        }

        public override void OnOpened()
        {
            base.OnOpened();
            LevelSystem.Instance.LoadLevelFiles();
        }

        public override void OnCollapsed()
        {
            base.OnCollapsed();
            Configure.Instance.LastWantLevelIDs = LevelItemCollection.Where(levelItem => levelItem.IsWanted).Select(levelItem => levelItem.LevelID).ToArray();
            Configure.Instance.Save(true);
            ViewModels.Instance.MainValue.Want();
        }
    }
}