using Microsoft.Office.Core; using Microsoft.Win32; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Properties; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI { public class ChatGPTAITranslationContentViewModel : BindableBase, INavigationAware { public HomeContentViewModel homeContentViewModel = null; public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0); public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1); 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 errorTipVisible=Visibility.Collapsed; public Visibility ErrorTipVisible { get { return errorTipVisible; } set { SetProperty(ref errorTipVisible, value); if (value == Visibility.Visible) { dispatcherTimer.Start(); } } } private string errorTipText = "The uploaded file cannot exceed 10MB"; public string ErrorTipText { get { return errorTipText; } set { SetProperty(ref errorTipText, value); } } public static List FromlanguageFamily { set; get; } = new List(); private void GetFromlanguageOrigin() { FromlanguageFamily.Clear(); FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin(); } public List TolanguageFamily { set; get; } = new List(); private DispatcherTimer dispatcherTimer = new DispatcherTimer(); private void GetTolanguageOrigin() { TolanguageFamily.Clear(); TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin(); } public DelegateCommand SelectFilesCommand { get; set; } public ChatGPTAITranslationContentViewModel() { SelectFilesCommand = new DelegateCommand(selectFiles); dispatcherTimer.Interval = TimeSpan.FromSeconds(5); dispatcherTimer.Tick += Dispatchertimer_Tick; init(); } private void init() { GetFromlanguageOrigin(); GetTolanguageOrigin(); } private void Dispatchertimer_Tick(object sender, EventArgs e) { ErrorTipVisible = Visibility.Collapsed; dispatcherTimer.Stop(); } public async void selectFiles() { string word = Properties.Resources.wordex; string pdf = Properties.Resources.pdf; string allfiles = pdf + word; OpenFileDialog dialog = new OpenFileDialog(); //dialog.Multiselect = true; dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" + $"Pdf({pdf})|{pdf}|" + $"Microsoft Office Word({word})|{word}"); if ((bool)dialog.ShowDialog()) { FileInfo fileInfo = new FileInfo(dialog.FileName); if ((((float)fileInfo.Length) / 1024 / 1024) > 30) { ErrorTipVisible = Visibility.Visible; return; } string newfile = ""; await Task.Run(async delegate { newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage); }); if (!string.IsNullOrEmpty(newfile)) { if (File.Exists(newfile)) { if (homeContentViewModel != null) { homeContentViewModel.OpenFile(new string[] { newfile }); return; } } } //newfile code ErrorTipVisible =Visibility.Visible; } } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.HomeContentViewModel, out homeContentViewModel); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } }