using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.AzureUtils.Environment;
using Tango.AzureUtils.Firmware;
using Tango.Core.Commands;
namespace Tango.AzureUtils.UI.ViewModels
{
public class EnvironmentFirmwareUpgradeViewVM : AzureDashboardViewModel
{
private FirmwareManager _firmwareManager;
private List<IWebAppBase> _deploymentSlots;
public List<IWebAppBase> DeploymentSlots
{
get { return _deploymentSlots; }
set { _deploymentSlots = value; RaisePropertyChangedAuto(); }
}
private IWebAppBase _selectedDeploymentSlot;
public IWebAppBase SelectedDeploymentSlot
{
get { return _selectedDeploymentSlot; }
set { _selectedDeploymentSlot = value; RaisePropertyChangedAuto(); }
}
private String _filePath;
public String FilePath
{
get { return _filePath; }
set { _filePath = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _firmwareVersion;
public String FirmwareVersion
{
get { return _firmwareVersion; }
set { _firmwareVersion = value; RaisePropertyChangedAuto(); }
}
public RelayCommand UpgradeFirmwareCommand { get; set; }
public RelayCommand BrowseFileCommand { get; set; }
public EnvironmentFirmwareUpgradeViewVM()
{
UpgradeFirmwareCommand = new RelayCommand(UpgradeFirmware,() => FilePath != null);
BrowseFileCommand = new RelayCommand(BrowseFile);
}
public override void OnAuthenticated(IAzure azure, List<IWebAppBase> apps)
{
DeploymentSlots = apps.Where(x => x.Name.Contains("MachineService")).ToList();
SelectedDeploymentSlot = DeploymentSlots.FirstOrDefault(x => x.Name.EndsWith("DEV"));
_firmwareManager = new FirmwareManager(azure);
_firmwareManager.ConfirmationRequired += ConfirmationHandler;
_firmwareManager.Progress += ProgressHandler;
}
private async void BrowseFile()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Select Tango Firmware Package";
dlg.Filter = "Tango Firmware Package Files|*.tfp";
if (dlg.ShowDialog().Value)
{
FilePath = dlg.FileName;
try
{
FirmwareVersion = await _firmwareManager.GetFirmwareVersion(FilePath);
}
catch (Exception ex)
{
FilePath = null;
StatusManager.UpdateStatus(ex);
}
}
}
private async void UpgradeFirmware()
{
try
{
if (!Validate()) return;
IsFree = false;
await _firmwareManager.InjectFirmwarePackage(SelectedDeploymentSlot, FilePath);
}
catch (Exception ex)
{
StatusManager.UpdateStatus(ex);
}
finally
{
RequireRefresh();
IsFree = true;
}
}
protected override void OnRefreshRequired()
{
base.OnRefreshRequired();
var old = SelectedDeploymentSlot;
SelectedDeploymentSlot = null;
SelectedDeploymentSlot = old;
}
}
}