private void ReservationsUpdated(Guid reservationId)
        {
            this.FromHour = this._minFromHour;
            this.NoOfPersons = 0;

            this._reservations = new MappedValueCollection();
            var allReservations = this._reservationManager.GetAll();
            foreach (var reservation in allReservations)
            {
                var reservedTables = this._tableManager.GetAll().Where(tbl => reservation.ReservedTableIds.Contains(tbl.TableId));
                var reservedHours = new List<int>();
                for (int reservedHour = reservation.TimeFrom; reservedHour <= reservation.TimeTo; reservedHour++)
                {
                    reservedHours.Add(reservedHour);
                }

                foreach (var reservedHour in reservedHours)
                {
                    foreach (var table in reservedTables)
                    {
                        this._reservations.Add(new MappedValue()
                        {
                            RowBinding = table,
                            ColumnBinding = this.ReservationHours.Where(rsHr => rsHr.Hour.Equals(reservedHour)).First(),
                            Value = true
                        });
                    }
                }
            }

            this.NotifyPropertyChange("Reservations");
        }
        public ReservationDashBoardViewModel(IUnityContainer container, ILoggerFacade logger, IReservationManager reservationManager, ITableManager tableManager, IMessageBoxService messageBoxService, IDialogBoxService dialogBoxService)
        {
            this._logger = logger;
            this._container = container;
            this._reservationManager = reservationManager;
            this._tableManager = tableManager;
            this._messageBoxService = messageBoxService;
            this._dialogBoxService = dialogBoxService;
            this._reservationHours = new ObservableCollection<ReservationHour>();
            this._reservations = new MappedValueCollection();


            this.AddCommand = new DelegateCommand(this.OnAddCommand, () => { return this._noOfPersons > 0; });
            this.BrowseCommand = new DelegateCommand(this.OnBrowseCommand);
            this.ImportCommand = new DelegateCommand(this.OnImportCommand, () => { return !string.IsNullOrEmpty(this._tableXMLFile); });

            this._tables = this._tableManager.GetAll();
            
            // Assumption : Reservation duration is between 10 Am and 10 Pm
            this._minFromHour = 10;
            this._maxFromHour = 22;

            for (int hour = this._minFromHour; hour <= this._maxFromHour; hour++)
            {
                this._reservationHours.Add(new ReservationHour(hour));
            }

            this.FromHour = this._minFromHour;

            TableReservation.Common.ReservationsUpdatedEvent.Instance.Subscribe(this.ReservationsUpdated);
            this.ReservationsUpdated(Guid.NewGuid());
        }
        private void DataGrid_CellDrop(object sender, DragEventArgs e)
        {
            DataGridCell dgc = sender as DataGridCell;

            if (_draggedData != null && !_isMultipleCells)
            {
                MappedValue selectedCell = _draggedData as MappedValue;

                RowViewModel     sourceRowModel    = (selectedCell.RowBinding as RowViewModel);
                ColumnsViewModel sourceColumnModel = (selectedCell.ColumnBinding as ColumnsViewModel);


                RowViewModel     targetRowModel    = ((BindableColumn.ViewModel.RowViewModel)(dgc.DataContext));
                ColumnsViewModel targetColumnModel = ((BindableColumn.ViewModel.ColumnsViewModel)((dgc.Column).Header));

                targetCell = targetRowModel.Name + targetColumnModel.Currency;
                sourceCell = sourceRowModel.Name + sourceColumnModel.Currency;


                if (sourceCell == targetCell)
                {
                    RemoveDraggedAdorner();
                    RemoveInsertionAdorner();
                    (this._sourceItemsControl as DataGrid).UnselectAllCells();
                    dgc.IsSelected = false;
                    return;
                }

                MappedValueCollection mappedValueCollection = this._mappedValueCollection;
                var movingCellData = mappedValueCollection.ReturnIfExistAddIfNot(sourceColumnModel, sourceRowModel);

                if (mappedValueCollection.AddCellValue(targetRowModel, targetColumnModel, movingCellData))
                {
                    if (movingCellData.Value != null)
                    {
                        dgc.IsSelected = true;
                    }

                    mappedValueCollection.EmptyCellValue(sourceRowModel, sourceColumnModel);
                }
            }
            else if (_draggedData != null && _isMultipleCells)
            {
                RowViewModel     targetRowModel    = ((BindableColumn.ViewModel.RowViewModel)(dgc.DataContext));
                ColumnsViewModel targetColumnModel = ((BindableColumn.ViewModel.ColumnsViewModel)((dgc.Column).Header));

                targetCell = targetRowModel.Name + targetColumnModel.Currency;

                if (MoveMultipleCells(targetCell, this._multipleDraggedData.Count))
                {
                    this._multipleDraggedData.Clear();
                }
            }

            RemoveDraggedAdorner();
            RemoveInsertionAdorner();
            this._multipleDraggedData.Clear();
            (this._sourceItemsControl as DataGrid).UnselectAllCells();
        }
        private Dictionary <string, object> GetNextAvailableCells(string targetCell, int countOfCells)
        {
            string rowPosition;
            string columnPosition;

            Dictionary <string, object> _availableCells = new Dictionary <string, object>();

            char[]   arrayofAlphabets = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P' };
            string[] arrayofNumbers   = new string[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };

            bool isRowAlphabet = !String.IsNullOrEmpty(targetCell) && Char.IsLetter(targetCell.ToCharArray()[0]);

            GetPlate_RowColumnPosition(isRowAlphabet, targetCell, out rowPosition, out columnPosition);


            int rowCount    = isRowAlphabet ? Array.IndexOf(arrayofAlphabets, Convert.ToChar(rowPosition)) : Convert.ToInt32(rowPosition);
            int columnCount = !isRowAlphabet?Array.IndexOf(arrayofNumbers, Convert.ToChar(columnPosition)) : Convert.ToInt32(columnPosition);

            ObservableCollection <RowViewModel>     RowCollection     = (this._sourceDataContext as MainWindowViewModel).RowCollection;
            ObservableCollection <ColumnsViewModel> ColumnsCollection = (this._sourceDataContext as MainWindowViewModel).ColumnsCollection;
            MappedValueCollection RowColumnValues = (this._sourceDataContext as MainWindowViewModel).RowColumnValues;


            for (int currentRow = rowCount; currentRow < RowCollection.Count; currentRow++)
            {
                string row            = isRowAlphabet ? Convert.ToString(arrayofAlphabets[currentRow]) : string.Format("{0:D2}", currentRow);
                var    tempRowBinding = RowCollection.Where(q => q.Name.Equals(row)).FirstOrDefault();

                for (int currentColumn = columnCount; currentColumn < ColumnsCollection.Count; currentColumn++)
                {
                    if (countOfCells == _availableCells.Count)
                    {
                        break;
                    }
                    else
                    {
                        string column = !isRowAlphabet?Convert.ToString(arrayofAlphabets[currentColumn]) : string.Format("{0:D2}", currentColumn);

                        var         tempColumnBinding = ColumnsCollection.Where(p => p.Currency.Equals(column)).FirstOrDefault();
                        string      currentCell       = row + column;
                        MappedValue tempRowColumn     = RowColumnValues.Where(Z => Z.RowBinding == tempRowBinding && Z.ColumnBinding == tempColumnBinding).FirstOrDefault();

                        if (tempRowColumn.Value == null || (tempRowColumn.Value != null && (tempRowColumn.Value as CellData).ColorName == null))
                        {
                            _availableCells.Add(currentCell, tempRowColumn);
                        }
                    }
                }

                if (countOfCells == _availableCells.Count)
                {
                    break;
                }
            }


            return(_availableCells);
        }
        private bool ExchangeData(Dictionary <string, object> availableCells)
        {
            if (availableCells.Count > 1)
            {
                foreach (KeyValuePair <string, object> movingCellData in availableCells)
                {
                    MappedValueCollection mappedValueCollection = this._mappedValueCollection;
                    var CellData    = mappedValueCollection.ReturnIfExistAddIfNot((movingCellData.Value as MappedValue).ColumnBinding, (movingCellData.Value as MappedValue).RowBinding);
                    var bindingData = GetDataForBinding();

                    if (bindingData != null)
                    {
                        CellData.Value = (bindingData as MappedValue).Value;
                        MappedValue value = this._mappedValueCollection.ReturnIfExistAddIfNot((bindingData as MappedValue).ColumnBinding, (bindingData as MappedValue).RowBinding);
                        value.Value = null;
                    }
                }
                return(true);
            }
            return(false);
        }
        private void DropTarget_PreviewDrop(object sender, DragEventArgs e)
        {
            object draggedItem = e.Data.GetData(this._format.Name);

            int indexRemoved = -1;

            if (draggedItem != null)
            {
                if ((e.Effects & DragDropEffects.Move) != 0 && (this._sourceItemsControl == this._targetItemsControl || GetIsDropRejectFromOthers(this._targetItemsControl)))
                {
                    this._targetDataContext = (sender as DataGrid).DataContext;

                    // Remove the selected Cell here from Mapped Values
                    DataGrid dgc = sender as DataGrid;

                    RowViewModel     sourceRowHeader    = ((RowViewModel)(dgc.CurrentItem));
                    ColumnsViewModel sourceColumnHeader = ((ColumnsViewModel)((dgc.CurrentColumn).Header));
                    string           srcCell            = sourceRowHeader.Name + sourceColumnHeader.Currency;


                    MappedValueCollection sourceCell  = AttachedColumnBehavior.GetMappedValues(sender as DependencyObject);
                    MappedValue           mappedValue = sourceCell.ReturnIfExistAddIfNot(sourceColumnHeader, sourceRowHeader);
                    int getIndex = sourceCell.IndexOf(mappedValue);

                    //var hitTestInfo = (sender as DataGrid).InputHitTest(new Point{ X = e.})

                    sourceCell[getIndex].Value = (draggedItem as MappedValue).Value;


                    //indexRemoved = RemoveItemFromItemsControl(this._sourceItemsControl, draggedItem);
                }
                // This happens when we drag an item to a later position within the same ItemsControl.
                if (indexRemoved != -1 && this._sourceItemsControl == this._targetItemsControl && indexRemoved < this._insertionIndex)
                {
                    this._insertionIndex--;
                }
                if (!GetIsDropRejectFromOthers(this._targetItemsControl))
                {
                    IEnumerable itemsSource      = _targetItemsControl.ItemsSource;
                    Type        type             = itemsSource.GetType();
                    Type        genericIListType = type.GetInterface("IList`1");
                    int         elementsCount    = 0;
                    if (genericIListType != null)
                    {
                        elementsCount = (int)type.GetProperty("Count").GetValue(itemsSource, null);
                    }
                    if (elementsCount > this._insertionIndex)
                    {
                        RemoveItemFromItemsControlByIndex(this._targetItemsControl, this._insertionIndex);
                        InsertItemInItemsControl(this._targetItemsControl, draggedItem, this._insertionIndex);

                        if (this._sourceItemsControl == this._targetItemsControl)
                        {
                            Type   draggedType = draggedItem.GetType();
                            object newitem     = Activator.CreateInstance(draggedType);
                            InsertItemInItemsControl(this._sourceItemsControl, newitem, indexRemoved);
                        }
                    }
                }
                else
                {
                    Type   draggedType = draggedItem.GetType();
                    object newitem     = Activator.CreateInstance(draggedType);
                    //InsertItemInItemsControl(this._sourceItemsControl, newitem, indexRemoved);
                }

                RemoveDraggedAdorner();
                RemoveInsertionAdorner();
            }
            e.Handled = true;
        }
 public static void SetMappedValues(DependencyObject dataGrid, MappedValueCollection value)
 {
     dataGrid.SetValue(MappedValuesProperty, value);
 }