using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using Tango.Core.Commands; using Tango.Core.Helpers; using Tango.BL.Entities; using Tango.MachineStudio.Common.Notifications; using Tango.SharedUI; using SimpleValidator.Extensions; using Tango.MachineStudio.Common.StudioApplication; using Tango.MachineStudio.Common; using Tango.BL; using Tango.AutoComplete.Editors; using System.Data.Entity; using Tango.BL.Builders; using Tango.MachineStudio.MachineDesigner.Contracts; namespace Tango.MachineStudio.MachineDesigner.ViewModels { public class MainViewVM : StudioViewModel { private INotificationProvider _notification; private ObservablesContext _dbContext; private ObservablesContext _machineContext; private ObservablesContext _configContext; #region Properties private ObservableCollection _machines; /// /// Gets or sets the machines. /// public ObservableCollection Machines { get { return _machines; } set { _machines = value; RaisePropertyChangedAuto(); } } private ObservableCollection _dispensers; /// /// Gets or sets the dispensers. /// public ObservableCollection Dispensers { get { return _dispensers; } set { _dispensers = value; RaisePropertyChangedAuto(); } } private Machine _selectedMachine; /// /// Gets or sets the selected machine from the drop down. /// public Machine SelectedMachine { get { return _selectedMachine; } set { if (_selectedMachine != value) { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } } private Machine _activeMachine; /// /// Gets or sets the active machine. /// public Machine ActiveMachine { get { return _activeMachine; } set { _activeMachine = value; RaisePropertyChangedAuto(); } } private Configuration _configuration; /// /// Gets or sets the editable machine configuration. /// public Configuration Configuration { get { return _configuration; } set { _configuration = value; RaisePropertyChangedAuto(); } } private IdsPack _selectedIds; /// /// Gets or sets the selected ids pack. /// public IdsPack SelectedIds { get { return _selectedIds; } set { _selectedIds = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _dispensersFilter; /// /// Gets or sets the dispensers filter. /// public String DispensersFilter { get { return _dispensersFilter; } set { _dispensersFilter = value; RaisePropertyChangedAuto(); OnDispensersFilterChanged(); } } private String _filter; /// /// Gets or sets the machines filter. /// public String Filter { get { return _filter; } set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } #endregion #region Commands /// /// Gets or sets the save command. /// public RelayCommand SaveCommand { get; set; } /// /// Gets or sets the add ids command. /// public RelayCommand AddIdsCommand { get; set; } /// /// Gets or sets the remove ids command. /// public RelayCommand RemoveIdsCommand { get; set; } /// /// Gets or sets the remove machine command. /// public RelayCommand RemoveMachineCommand { get; set; } /// /// Gets or sets the add machine command. /// public RelayCommand AddMachineCommand { get; set; } /// /// Gets or sets the edit machine command. /// public RelayCommand EditMachineCommand { get; set; } #endregion #region Constructors public MainViewVM() { } /// /// Initializes a new instance of the class. /// public MainViewVM(INotificationProvider notification) { _dbContext = ObservablesContext.CreateDefault(); _notification = notification; //SaveCommand = new RelayCommand(Save); AddIdsCommand = new RelayCommand(AddIds, (x) => Configuration != null && Configuration.IdsPacks.Count < 8); RemoveIdsCommand = new RelayCommand(RemoveIds, (x) => SelectedIds != null); EditMachineCommand = new RelayCommand(EditSelectedMachine, () => SelectedMachine != null); } private async void EditSelectedMachine() { using (_notification.PushTaskItem("Loading machine details...")) { IsFree = false; if (_machineContext != null) { _machineContext.Dispose(); } _machineContext = ObservablesContext.CreateDefault(); ActiveMachine = (await new MachineBuilder(_machineContext).Set(SelectedMachine).WithOrganization().WithConfiguration().BuildAsync()); View.NavigateTo(MachineDesignerNavigationView.MachineDetailsView); IsFree = true; } } #endregion #region Application Ready public override void OnApplicationReady() { } #endregion //private Task InitCollections() //{ // return Task.Factory.StartNew(() => // { // CanWork = false; // _dbContext = ObservablesContext.CreateDefault(); // Adapter = new ObservablesStaticCollections(); // Adapter.ApplicationDisplayPanelVersions = _dbContext.ApplicationDisplayPanelVersions.ToObservableCollection(); // Adapter.ApplicationFirmwareVersions = _dbContext.ApplicationFirmwareVersions.ToObservableCollection(); // Adapter.ApplicationOsVersions = _dbContext.ApplicationOsVersions.ToObservableCollection(); // Adapter.EmbeddedFirmwareVersions = _dbContext.EmbeddedFirmwareVersions.ToObservableCollection(); // Adapter.DispenserTypes = _dbContext.DispenserTypes.ToObservableCollection(); // Adapter.LiquidTypes = _dbContext.LiquidTypes.ToObservableCollection(); // Adapter.MidTankTypes = _dbContext.MidTankTypes.ToObservableCollection(); // Adapter.CartridgeTypes = _dbContext.CartridgeTypes.ToObservableCollection(); // Adapter.IdsPackFormulas = _dbContext.IdsPackFormulas.ToObservableCollection(); // Adapter.HardwareVersions = _dbContext.HardwareVersions.ToObservableCollection(); // Adapter.MachineVersions = _dbContext.MachineVersions.ToObservableCollection(); // Adapter.Organizations = _dbContext.Organizations.ToObservableCollection(); // Adapter.InitCollectionSources(); // CanWork = true; // }); //} private void Reset() { //using (_notification.PushTaskItem("Resetting designer...")) //{ // SelectedMachine = null; // Machine = new Machine(); // Configuration = new Configuration(); // InitCollections(); //} } private async void OnDispensersFilterChanged() { if (!String.IsNullOrWhiteSpace(DispensersFilter)) { IsFree = false; Dispensers = (await _configContext.Dispensers.Where(x => x.SerialNumber.ToLower().StartsWith(DispensersFilter.ToLower())).ToListAsync()).ToObservableCollection(); IsFree = true; } } #region Virtual Methods #endregion #region Drag Drop Handlers /// /// Drops the ids pack. /// /// The ids pack1. /// The ids pack2. public void DropIdsPack(IdsPack idsPack1, IdsPack idsPack2) { Configuration.IdsPacks.Swap(idsPack1, idsPack2); } /// /// Drops the touch panel. /// /// The application display panel version. public void DropTouchPanel(ApplicationDisplayPanelVersion applicationDisplayPanelVersion) { Configuration.ApplicationDisplayPanelVersion = applicationDisplayPanelVersion; Configuration.ApplicationDisplayPanelVersionGuid = applicationDisplayPanelVersion.Guid; } /// /// Drops the application firmware version. /// /// The application firmware version. public void DropApplicationFirmwareVersion(ApplicationFirmwareVersion applicationFirmwareVersion) { Configuration.ApplicationFirmwareVersion = applicationFirmwareVersion; Configuration.ApplicationFirmwareVersionGuid = applicationFirmwareVersion.Guid; } /// /// Drops the hardware version. /// /// The hardware version. public void DropHardwareVersion(HardwareVersion hardwareVersion) { Configuration.HardwareVersion = hardwareVersion; Configuration.HardwareVersionGuid = hardwareVersion.Guid; } /// /// Drops the embedded firmware. /// /// The embedded firmware version. public void DropEmbeddedFirmware(EmbeddedFirmwareVersion embeddedFirmwareVersion) { Configuration.EmbeddedFirmwareVersion = embeddedFirmwareVersion; Configuration.EmbeddedFirmwareVersionGuid = embeddedFirmwareVersion.Guid; } /// /// Drops the application os version. /// /// The application os version. public void DropApplicationOsVersion(ApplicationOsVersion applicationOsVersion) { Configuration.ApplicationOsVersion = applicationOsVersion; Configuration.ApplicationOsVersionGuid = applicationOsVersion.Guid; } /// /// Drops the dispenser. /// /// The dispenser. /// The ids pack. public void DropDispenser(Dispenser dispenser, IdsPack idsPack) { idsPack.Dispenser = dispenser; idsPack.DispenserGuid = dispenser.Guid; } /// /// Drops the type of the cartridge. /// /// Type of the cartridge. /// The ids pack. public void DropCartridgeType(CartridgeType cartridgeType, IdsPack idsPack) { idsPack.CartridgeType = cartridgeType; idsPack.CartridgeTypeGuid = cartridgeType.Guid; } /// /// Drops the type of the mid tank. /// /// Type of the mid tank. /// The ids pack. public void DropMidTankType(MidTankType midTankType, IdsPack idsPack) { idsPack.MidTankType = midTankType; idsPack.MidTankTypeGuid = midTankType.Guid; } /// /// Drops the type of the liquid. /// /// Type of the liquid. /// The ids pack. public void DropLiquidType(LiquidType liquidType, IdsPack idsPack) { idsPack.LiquidType = liquidType; idsPack.LiquidTypeGuid = liquidType.Guid; } /// /// Drops the ids formula. /// /// The ids pack formula. /// The ids pack. /// public void DropIdsFormula(IdsPackFormula idsPackFormula, IdsPack idsPack) { idsPack.IdsPackFormula = idsPackFormula; idsPack.IdsPackFormulaGuid = idsPackFormula.Guid; } #endregion #region Private Methods /// /// Removes the selected IDS pack. /// private void RemoveIds() { _dbContext.IdsPacks.Remove(SelectedIds); SelectedIds = null; } /// /// Adds a new IDS pack. /// private void AddIds() { _dbContext.IdsPacks.Add(new IdsPack() { Configuration = Configuration }); InvalidateRelayCommands(); } ///// ///// Saves the current machine configuration. ///// //private async void Save() //{ // foreach (var ids in Configuration.IdsPacks) // { // ids.PackIndex = Configuration.IdsPacks.IndexOf(ids); // ids.Configuration = Configuration; // ids.ConfigurationGuid = Configuration.Guid; // } // //Validate // List errors = new List(); // if (Machine.MachineVersion == null) // { // errors.Add("Machine version is required."); // } // if (Machine.Name.IsNullOrWhiteSpace()) // { // errors.Add("Machine name is required."); // } // if (Machine.Organization == null) // { // errors.Add("Machine organization is required."); // } // if (Machine.SerialNumber.IsNullOrWhiteSpace()) // { // errors.Add("Machine serial number is required."); // } // if (Configuration.Name.IsNullOrWhiteSpace()) // { // errors.Add("Configuration name is required."); // } // if (Configuration.ApplicationDisplayPanelVersion == null) // { // errors.Add("Touch Panel is required."); // } // if (Configuration.ApplicationFirmwareVersion == null) // { // errors.Add("Application firmware is required."); // } // if (Configuration.ApplicationOsVersion == null) // { // errors.Add("Application operation system is required."); // } // if (Configuration.EmbeddedFirmwareVersion == null) // { // errors.Add("Embedded firmware is required."); // } // if (Configuration.HardwareVersion == null) // { // errors.Add("Hardware version is required."); // } // foreach (var pack in Configuration.IdsPacks) // { // if (pack.LiquidType != null || pack.CartridgeType != null || pack.Dispenser != null || pack.IdsPackFormula != null || pack.MidTankType != null) // { // if (pack.CartridgeType == null) // { // errors.Add(String.Format("Cartridge type is required on IDS pack '{0}'.", pack.PackIndex)); // } // if (pack.Dispenser == null) // { // errors.Add(String.Format("Dispenser is required on IDS pack '{0}'.", pack.PackIndex)); // } // if (pack.LiquidType == null) // { // errors.Add(String.Format("Liquid type is required on IDS pack '{0}'.", pack.PackIndex)); // } // if (pack.MidTankType == null) // { // errors.Add(String.Format("Mid Tank type is required on IDS pack '{0}'.", pack.PackIndex)); // } // if (pack.IdsPackFormula == null) // { // errors.Add(String.Format("Formula type is required on IDS pack '{0}'.", pack.PackIndex)); // } // pack.IsEmpty = false; // } // else // { // pack.IsEmpty = true; // } // } // if (errors.Count > 0) // { // String errorsString = "Please fix the following validation errors before trying to save." + Environment.NewLine + Environment.NewLine; // errorsString += String.Join(Environment.NewLine, errors); // _notification.ShowError(errorsString); // return; // } // //Validate // try // { // CanWork = false; // using (_notification.PushTaskItem("Saving Machine Configuration...")) // { // if (!_dbContext.Machines.Any(x => x.SerialNumber.ToLower() == Machine.SerialNumber.ToLower())) // { // if (!_notification.ShowQuestion("The specified machine serial number does not exist. Do you wish to create a new machine?")) // { // CanWork = true; // return; // } // else // { // Machine.Configuration = Configuration; // Configuration.CreationDate = DateTime.UtcNow; // Machine.ProductionDate = DateTime.UtcNow; // _dbContext.Machines.Add(Machine); // } // } // await _dbContext.SaveChangesAsync(); // OnSelectedMachineChanged(); // } // } // catch (Exception ex) // { // _notification.ShowError("An error occurred while trying to save the configuration" + Environment.NewLine + ex.Message); // } // finally // { // CanWork = true; // InvalidateRelayCommands(); // } //} ///// ///// Sets the current configuration to the selected machine version default configuration. ///// //private async void SetVersionConfiguration() //{ // if (Machine.MachineVersion != null) // { // using (_notification.PushTaskItem("Applying default configuration...")) // { // CanWork = false; // await Task.Factory.StartNew(() => // { // var version = _dbContext.MachineVersions.Where(x => x.Guid == Machine.MachineVersion.Guid).Include(x => x.DefaultConfiguration).FirstOrDefault(); // var version_config = _dbContext.Adapter.GetConfiguration(x => x.Guid == version.DefaultConfiguration.Guid); // Configuration = version_config.Clone(); // Machine.Configuration = Configuration; // }); // CanWork = true; // } // } // else // { // _notification.ShowError("No machine version selected."); // } //} ///// ///// Sets the current configuration as a default machine version configuration. ///// //private void SetAsDefaultConfiguration() //{ // _notification.ShowModalDialog(async (vm) => // { // CanWork = false; // try // { // using (_notification.PushTaskItem("Saving Default Configuration...")) // { // if (vm.SelectedVersion != null) // { // var version = _dbContext.MachineVersions.Where(x => x.Guid == vm.SelectedVersion.Guid).Include(x => x.DefaultConfiguration).SingleOrDefault(x => x.Guid == vm.SelectedVersion.Guid); // _dbContext.Configurations.Remove(version.DefaultConfiguration); // var cloned = Configuration.Clone(); // _dbContext.Configurations.Add(cloned); // version.DefaultConfiguration = cloned; // await _dbContext.SaveChangesAsync(); // } // else // { // MachineVersion newVersion = new MachineVersion(); // newVersion.Version = vm.Version; // newVersion.Name = vm.VersionName; // var cloned = Configuration.Clone(); // _dbContext.Configurations.Add(cloned); // newVersion.DefaultConfiguration = cloned; // _dbContext.MachineVersions.Add(newVersion); // await _dbContext.SaveChangesAsync(); // } // } // } // catch (Exception ex) // { // _notification.ShowError(ex.Message); // } // finally // { // CanWork = true; // } // }, () => // { // }); //} #endregion private async void OnFilterChanged() { if (!String.IsNullOrWhiteSpace(Filter)) { IsFree = false; Machines = (await _dbContext.Machines.Where(x => x.SerialNumber.StartsWith(Filter)).ToListAsync()).ToObservableCollection(); IsFree = true; } } } }