using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace Phihong_EVSE_UI_Tool
{
///
/// SaveSdCard.xaml 的互動邏輯
///
public partial class SaveSdCard : UserControl
{
private Properties.Settings mySettings = Properties.Settings.Default;
private string sdFolderPath;
private List toBeSavedFiles = new List();
public SaveSdCard()
{
InitializeComponent();
sdFolderPath = mySettings.SdCardPath;
uxSdBrowseTextBox.Text = sdFolderPath;
}
private void SaveToSdCard()
{
if (uxSaveAllRadioButton.IsChecked.Value)
{
toBeSavedFiles = new List(Utility.ChangedFiles.Keys);
}
else
{
toBeSavedFiles = (from p in Utility.ChangedFiles
where p.Value
select p.Key).ToList();
}
Directory.CreateDirectory(Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR));
foreach (string fileName in toBeSavedFiles)
{
File.Copy(Path.Combine(Utility.OUTPUT_DIRECTORY, fileName),
Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR, fileName), true);
}
foreach (string resName in Utility.REQ_FILELIST)
{
Uri srcPath = new Uri(Path.Combine(Utility.REQ_PARENTFOLDER, resName), UriKind.Relative);
Utility.CopyFileFromResource(srcPath, Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR, resName));
}
switch (toBeSavedFiles.Count())
{
case 0:
uxMessageTextBlock.Text = "Saved no image and 11 required files to SD card";
break;
case 1:
uxMessageTextBlock.Text = "Saved 1 image and 11 required files to SD card";
break;
default:
uxMessageTextBlock.Text = "Saved " + toBeSavedFiles.Count() + " images and 11 required files to SD card";
break;
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = Path.Combine(sdFolderPath, Utility.SDCARD_SUBDIR);
process.Start();
}
private void uxBrowseButton_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
if (btn is null)
{
return;
}
switch (btn.Tag.ToString())
{
case "Browse":
var dlg = new CommonOpenFileDialog()
{
IsFolderPicker = true,
Title = "Locate the SD card",
};
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
sdFolderPath = dlg.FileName;
uxSdBrowseTextBox.Text = sdFolderPath;
mySettings.SdCardPath = sdFolderPath;
mySettings.Save();
}
break;
case "Save":
if(!Directory.Exists(sdFolderPath))
{
MessageBox.Show("Please locate the SD card path first",
"Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
break;
}
SaveToSdCard();
break;
default:
break;
}
}
}
}