using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
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 outputFolderPath;
private string settingModelName;
private List toBeSavedFiles = new List();
public SaveSdCard()
{
InitializeComponent();
outputFolderPath = mySettings.OutputFolderPath;
uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
uxModelNameTestBox.Text = mySettings.ModelName;
}
private void SaveToDirectory()
{
if (Directory.Exists(Utility.ZIP_SOURCE_DIRECTORY))
{
Directory.Delete(Utility.ZIP_SOURCE_DIRECTORY, true);
}
if (Directory.Exists(Utility.ZIP_FILE_DIRECTORY))
{
Directory.Delete(Utility.ZIP_FILE_DIRECTORY, true);
}
Directory.CreateDirectory(Utility.ZIP_SOURCE_DIRECTORY);
Directory.CreateDirectory(Utility.ZIP_FILE_DIRECTORY);
if (uxSaveAllRadioButton.IsChecked.Value)
{
toBeSavedFiles = new List(Utility.ChangedFiles.Keys);
}
else
{
toBeSavedFiles = (from p in Utility.ChangedFiles
where p.Value
select p.Key).ToList();
}
foreach (string fileName in toBeSavedFiles)
{
File.Copy(Path.Combine(Utility.OUTPUT_DIRECTORY, fileName),
Path.Combine(Utility.ZIP_SOURCE_DIRECTORY, 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(Utility.ZIP_SOURCE_DIRECTORY, resName));
}
string modelName = uxModelNameTestBox.Text;
string zipFileName = string.Format("DWIN_SET_{0}_{1}.zip", modelName, DateTime.Now.ToString("yyyyMMddHHmmss"));
string temZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, Utility.TEMP_ZIP_FILE_NAME);
ZipFile.CreateFromDirectory(Utility.ZIP_SOURCE_DIRECTORY, temZipPath, CompressionLevel.Fastest, true);
string tempHeaderBuildedZipPath = Path.Combine(Utility.ZIP_FILE_DIRECTORY, zipFileName);
FirmwareHeaderBuilder.AddFirmwareHeader(temZipPath, modelName, tempHeaderBuildedZipPath);
File.Copy(tempHeaderBuildedZipPath, Path.Combine(outputFolderPath, zipFileName));
switch (toBeSavedFiles.Count())
{
case 0:
uxMessageTextBlock.Text = "Archived no image and 11 required files to File";
break;
case 1:
uxMessageTextBlock.Text = "Archived 1 image and 11 required files to File";
break;
default:
uxMessageTextBlock.Text = "Archived " + toBeSavedFiles.Count() + " images and 11 required files to File";
break;
}
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = outputFolderPath;
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 output directory",
};
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
outputFolderPath = dlg.FileName;
uxOutputDirectoryBrowseTextBox.Text = outputFolderPath;
//mySettings.SdCardPath = sdFolderPath;
mySettings.OutputFolderPath = outputFolderPath;
mySettings.Save();
}
break;
case "Save":
if (uxModelNameTestBox.Text.Length != 14)
{
MessageBox.Show("Model Name length shoud be 14",
"Invalid Model Name", MessageBoxButton.OK, MessageBoxImage.Error);
break;
}
mySettings.ModelName = uxModelNameTestBox.Text;
mySettings.Save();
if (!Directory.Exists(outputFolderPath))
{
MessageBox.Show("Please locate the output directory first",
"Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
break;
}
SaveToDirectory();
break;
default:
break;
}
}
}
}