using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using Microsoft.Extensions.Options; namespace Azaion.LoaderUI; public partial class Login { private readonly IAzaionApi _azaionApi; private readonly DirectoriesConfig _dirConfig; public Login(IAzaionApi azaionApi, IOptions directoriesConfig) { _azaionApi = azaionApi; _dirConfig = directoriesConfig.Value; InitializeComponent(); } private void LoginClick(object sender, RoutedEventArgs e) { var creds = new ApiCredentials(TbEmail.Text, TbPassword.Password); if (!creds.IsValid()) return; LoginBtn.Cursor = Cursors.Wait; Cursor = Cursors.Wait; _azaionApi.Login(creds); if (GetInstallerVer() > GetLocalVer()) DownloadAndRunInstaller(); Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}"); Close(); } private void DownloadAndRunInstaller() { var (installerName, stream) = _azaionApi.DownloadInstaller(_dirConfig.SuiteInstallerDirectory); var localFileStream = new FileStream(installerName, FileMode.Create, FileAccess.Write); stream.CopyTo(localFileStream); localFileStream.Close(); stream.Close(); var processInfo = new ProcessStartInfo(installerName) { UseShellExecute = true, Verb = "runas" }; Process.Start(processInfo); } private Version GetInstallerVer() { var installerName = _azaionApi.GetLastInstallerName(_dirConfig.SuiteInstallerDirectory); var version = installerName .Replace("AzaionSuite.Iterative.", "") .Replace(".exe", ""); return new Version(version); } private Version GetLocalVer() { var localFileInfo = FileVersionInfo.GetVersionInfo(Constants.AZAION_SUITE_EXE); if (string.IsNullOrWhiteSpace(localFileInfo.ProductVersion)) throw new Exception($"Can't find {Constants.AZAION_SUITE_EXE} and its version"); return new Version(localFileInfo.FileVersion!); } private void CloseClick(object sender, RoutedEventArgs e) => Close(); private void MainMouseMove(object sender, MouseEventArgs e) { if (e.OriginalSource is Button || e.OriginalSource is TextBox) return; if (e.LeftButton == MouseButtonState.Pressed) DragMove(); } }