using ComPDFKit.PDFDocument;
using ComPDFKitViewer;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Master.CustomControl;
using PDF_Master.Helper;
using PDF_Master.Model;
using PDF_Master.Properties;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
{
public class DocumentaryTranslationDialogViewModel : BindableBase, IDialogAware
{
#region 文案
private string T_title;
public string T_Title
{
get { return T_title; }
set
{
SetProperty(ref T_title, value);
}
}
private string T_limitTip;
public string T_LimitTip
{
get { return T_limitTip; }
set
{
SetProperty(ref T_limitTip, value);
}
}
private string T_supportedLimitTip;
public string T_SupportedLimitTip
{
get { return T_supportedLimitTip; }
set
{
SetProperty(ref T_supportedLimitTip, value);
}
}
private string T_translationLanguage;
public string T_TranslationLanguage
{
get { return T_translationLanguage; }
set
{
SetProperty(ref T_translationLanguage, value);
}
}
private string T_translationBtn;
public string T_TranslationBtn
{
get { return T_translationBtn; }
set
{
SetProperty(ref T_translationBtn, value);
}
}
private string T_no;
public string T_No
{
get { return T_no; }
set
{
SetProperty(ref T_no, value);
}
}
private void InitString()
{
T_Title = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationTitle");
T_SupportedLimitTip = App.HomePageLoader.GetString("AITranslation_NotSupportedTip");
T_LimitTip = App.HomePageLoader.GetString("AITranslation_LimitTip");
T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
T_TranslationBtn = App.HomePageLoader.GetString("ViewRightMenuText_AITranslationBtn");
T_No = App.MainPageLoader.GetString("Convert_No");
}
#endregion
#region 参数和属性
public string Title => "";
public IRegionManager regionManager;
public CPDFViewer PDFViewer;
public ViewContentViewModel viewContentViewModel;
public IDialogService dialogs;
private CPDFViewer pdfViewer = null;
private string fromlanguage = "";
private string tolanguage = "";
private int fromlanguageIndex = 0;
public int FromlanguageIndex
{
get { return fromlanguageIndex; }
set
{
SetProperty(ref fromlanguageIndex, value);
fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
}
}
private int tolanguageIndex = 0;
public int TolanguageIndex
{
get { return tolanguageIndex; }
set
{
SetProperty(ref tolanguageIndex, value);
tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
}
}
///
/// 进度条显示
///
private Visibility progressVisible = Visibility.Collapsed;
public Visibility ProgressVisible
{
get
{
return progressVisible;
}
set
{
SetProperty(ref progressVisible, value);
}
}
private double processvalue = 0;
///
/// 进度条当前值
///
public double Value
{
get { return processvalue; }
set
{
SetProperty(ref processvalue, value);
}
}
private double maxValue = 10;
///
/// 进度条最大值
///
public double MaxValue
{
get { return maxValue; }
set
{
SetProperty(ref maxValue, value);
}
}
///
/// 初始化翻译前语言列表
///
public static List FromlanguageFamily { set; get; } = new List();
public string ViewerRegionName { get; set; }
private void GetFromlanguageOrigin()
{
FromlanguageFamily.Clear();
FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
}
///
/// 初始化翻译到语言列表
///
public List TolanguageFamily { set; get; } = new List();
private void GetTolanguageOrigin()
{
TolanguageFamily.Clear();
TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
}
public static Action CancelProgress { get; set; }
#endregion
#region 委托声明
public DelegateCommand TranslateCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public DelegateCommand CancelProgressCommand { get; set; }
#endregion
public DocumentaryTranslationDialogViewModel(IRegionManager regionManager, IDialogService dialogService)
{
dialogs = dialogService;
this.regionManager = regionManager;
CancelCommand = new DelegateCommand(cancel);
TranslateCommand = new DelegateCommand(translate);
ViewerRegionName = Guid.NewGuid().ToString();
CancelProgressCommand = new DelegateCommand(cancelprogress);
init();
InitString();
}
#region 逻辑函数
///
/// 初始化下拉列表,或其他
///
private void init()
{
GetFromlanguageOrigin();
GetTolanguageOrigin();
}
///
/// 进度条取消
///
private void cancelprogress()
{
if (CancelProgress != null)
{
CancelProgress.Invoke();
}
}
///
/// 关闭窗口
///
private void cancel()
{
RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
}
///
/// 翻译当前文档
///
public async void translate()
{
//添加付费拦截锁
if (!ServiceHelper.IAPBeforeFunction())
{
return;
}
#region 权益弹窗暂时关闭
//bool flg = false;
//权益弹窗
//if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
//{
// DialogParameters value = new DialogParameters();
// value.Add(ParameterNames.Open, "AI");
// dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
// {
// if (dialogResult.Result == ButtonResult.OK)
// {
// flg = true;
// }
// else
// {
// flg = false;
// }
// });
// if (flg == false)
// {
// return ;
// }
//}
#endregion
ProgressVisible = Visibility.Visible;
Value = 1;
string newfile = "";
FileInfo fileInfo = new FileInfo(pdfViewer.Document.FilePath);
Value = 3;
//判断文件大小
if ((((float)fileInfo.Length) / 1024 / 1024) > 30)
{
Value = 9;
AlertsMessage alertsMessage = new AlertsMessage();
alertsMessage.ShowDialog("", "The uploaded file cannot exceed 10MB", App.ServiceLoader.GetString("Text_ok"));
ProgressVisible = Visibility.Collapsed;
return;
}
//判断文件页数
if (fileInfo.Extension.ToLower() == ".pdf")
{
CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(pdfViewer.Document.FilePath);
if (cPDFDocument.PageCount > 30)
{
Value = 9;
AlertsMessage alertsMessage = new AlertsMessage();
alertsMessage.ShowDialog("", "The uploaded file cannot exceed 30Pages", App.ServiceLoader.GetString("Text_ok"));
ProgressVisible = Visibility.Collapsed;
return;
}
}
Value = 4;
await Task.Run(async delegate
{
newfile = await ChatGTPAIHelper.fileTranslate(pdfViewer.Document.FilePath, fromlanguage, tolanguage, false);
});
Value = 8;
if (!string.IsNullOrEmpty(newfile))
{
if (File.Exists(newfile))
{
if (viewContentViewModel != null)
{
viewContentViewModel.OpenFile(new string[] { newfile });
RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
}
}
else
{
//newfile code
if (newfile == "-1")
{
Value = 9;
ProgressVisible = Visibility.Collapsed; return;
}
AlertsMessage alertsMessage = new AlertsMessage();
alertsMessage.ShowDialog("", ChatGTPAIHelper.GetBaiduTranslationCode(newfile), App.ServiceLoader.GetString("Text_ok"));
}
}
Value = 9;
ProgressVisible = Visibility.Collapsed;
}
#endregion
#region 构架行为
public event Action RequestClose;
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
}
public void OnDialogOpened(IDialogParameters parameters)
{
parameters.TryGetValue(ParameterNames.PDFViewer, out pdfViewer);
parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel);
if (pdfViewer != null)
{
if (!regionManager.Regions[ViewerRegionName].Views.Contains(pdfViewer))
{
//设置PDFview为预览
PDFViewer = new CPDFViewer();
PDFViewer.InitDocument(pdfViewer.Document);
regionManager.AddToRegion(ViewerRegionName, PDFViewer);
PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
PDFViewer.Load();
if (PDFViewer.Document.PageCount == 1)
{
//防止单页pdfview闪烁问题
PDFViewer.ChangeViewMode(ViewMode.Single);
}
else
{
PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
}
PDFViewer.ChangeFitMode(FitMode.FitHeight);
PDFViewer.SetMouseMode(MouseModes.PanTool);
PDFViewer.Document.ReleasePages();
PDFViewer.ReloadDocument();
fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
//PageRangeNumber = PDFViewer.Document.PageCount;
//PDFViewer.Zoom(0.1);
}
}
}
#endregion
}
}