using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Newtonsoft.Json;
using RustTools.muqing;
using static RustTools.muqing.wl;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace RustTools.WindowUI;
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class ModuleInfoWin : WindowEx
{
public static List ModulePageList = new();
public ObservableCollection ScreenshotsList = new();
public ModInfo.Data Mod { get; set; } = new();
public ModuleInfoWin(string v)
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
gj.SetBackTheme(this);
var frame = App.AppTitlebar as FrameworkElement;
if (frame != null)
{
gj.UpdateTitleBar(this, frame.ActualTheme);
grid.RequestedTheme = frame.ActualTheme;
}
if (v != string.Empty)
{
init(v);
}
// 设置窗口背景颜色
}
private async void init(string v)
{
var json = await ApiFox.mod.getInfo("", v);
var a = JsonConvert.DeserializeObject(json);
if (a != null)
{
Mod = a.data;
Mod.IconUrl = Mod.IconUrl.Equals("") ? "/Assets/image/image_106.svg" : Mod.IconUrl.Replace("..", wl.api);
Mod.Link = Mod.Link.Replace("..", wl.api);
Title = Mod.Name;
var l = " | ";
title_b.Text = "单位数" + Mod.UnitNumber + l + Mod.DownloadNumber + "次下载" + l + "版本名称" + Mod.VersionName;
title_c.Text = "发布时间:" + Mod.CreationTime + " 最近更新:" + Mod.UpdateTime;
var ScrList = Mod.Screenshots.Split(",");
foreach (var s in ScrList)
{
gj.sc(s);
if (string.IsNullOrEmpty(s))
{
continue;
}
ScreenshotsList.Add(s.Replace("..", wl.api));
}
if (ScreenshotsList.Count > 0)
{
fliptext.Visibility = Visibility.Visible;
flipview.Visibility = Visibility.Visible;
}
else
{
fliptext.Visibility = Visibility.Collapsed;
flipview.Visibility = Visibility.Collapsed;
}
grid.DataContext = this; // 设置数据上下文
}
}
private void WindowEx_SizeChanged(object sender, WindowSizeChangedEventArgs args)
{
DescriptionText.Width = Width - 50;
}
private void WindowEx_Closed(object sender, WindowEventArgs args)
{
// 当窗口关闭时,从列表中移除
ModulePageList.Remove(this);
}
private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var label = ((AppBarButton)sender).Name;
gj.sc(label);
switch (label)
{
case "ReportButton":
gj.sc("举报这个模组抄袭");
//if (Toast.IsOpen) Toast.IsOpen = false;
Toast.Title = "举报此模组";
Toast.Subtitle = "请合理举报,拒接无脑举报!";
Toast.IsOpen = true;
break;
case "LikeButton":
gj.sc("给这个模组投了一个币");
//if (Toast.IsOpen) Toast.IsOpen = false;
Toast.Title = "支持此模组";
Toast.Subtitle = "支持后自动扣除你的一个硬币,如果没有则支持失败";
Toast.IsOpen = true;
break;
case "DownLoadButton":
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.Config);
var v = iniHelper.GetValue(IniHelper.CODE.Rust, IniHelper.KEY.ModFileUrl);
if (v == string.Empty)
{
var dialog = new ContentDialog()
{
XamlRoot = grid.XamlRoot,
Title = "警告",
Content = "请在设置中配置好模组路径再来",
SecondaryButtonText = "取消"
};
await dialog.ShowAsync();
return;
}
var cancellationTokenSource = new CancellationTokenSource();
gj.sc("下载此模组:" + Mod.Link);
var downloader = new FileDownloader();
DownloadDialog.XamlRoot = grid.XamlRoot;
var asyncOperation = DownloadDialog.ShowAsync();
DownloadDialog.Closing += DownloadDialog_Closing;
DownloadDialog.SecondaryButtonClick += (a, b) =>
{
gj.sc("删除");
cancellationTokenSource.Cancel();
downloader.Delete();
};
try
{
var PathMod = Path.Combine(v, Mod.Name);
if (File.Exists(PathMod + ".rwmod"))
{
File.Delete(PathMod + ".rwmod");
}
//下载链接 保存文件的本地路径
await downloader.DownloadFileAsync(Mod.Link, PathMod, (totalBytesRead, totalBytes) =>
{
DownloadDialogText.Text = $"Downloaded {totalBytesRead} of {totalBytes} bytes. {(totalBytes > 0 ? (double)totalBytesRead / totalBytes * 100 : 0):0.00}% complete.";
//Debug.WriteLine($"Downloaded {totalBytesRead} of {totalBytes} bytes. {(totalBytes > 0 ? (double)totalBytesRead / totalBytes * 100 : 0):0.00}% complete.");
}, cancellationTokenSource.Token);
Debug.WriteLine("文件下载完成");
File.Move(PathMod, PathMod + ".rwmod");
DownloadDialog.Closing -= DownloadDialog_Closing;
// 手动隐藏对话框
DownloadDialog.Hide();
}
catch (Exception ex)
{
Debug.WriteLine($"Error downloading file: {ex.Message}");
}
finally
{
DownloadDialog.Closing -= DownloadDialog_Closing;
DownloadDialog.Hide();
}
break;
}
}
private void Toast_ActionButtonClick(TeachingTip sender, object args)
{
}
private void DownloadDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
// Prevent the dialog from closing
if (args.Result != ContentDialogResult.Secondary)
{
args.Cancel = true;
}
}
}