private void SetWatchersCheckedText(object param)
 {
     try
     {
         List <string> checkedWatchers = new List <string>();
         WatchingMembers.Where(s => s.IsWatcher).Distinct().ToList().ForEach(s => checkedWatchers.Add(s.Name));
         WatchersText = string.Join(", ", checkedWatchers);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error", ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private async Task CreateTask()
        {
            try
            {
                List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("Title", Title),
                    new KeyValuePair <string, string>("Task Type", TypeText),
                    new KeyValuePair <string, string>("Task Status", StatusText),
                    new KeyValuePair <string, string>("Task Stamp", StampText),
                    new KeyValuePair <string, string>("Sheet", SelectedSheet?.Title),
                };
                if (FieldValidation.ValidateFields(values))
                {
                    if (WatchingMembers.Count() <= 0)
                    {
                        MessageBox.Show("Please add atleast 1 Team Member to the Task", "Add Team Members", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else if (WatchingMembers.Where(w => w.IsWatcher).Count() <= 0)
                    {
                        MessageBox.Show("Please select watching members from the Selected Team Members", "Select Watching Members", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        CanSaveTask = false;

                        List <TaskMembersModel>  taskMembers  = new List <TaskMembersModel>();
                        List <TaskWatchersModel> taskWatchers = new List <TaskWatchersModel>();

                        TeamMembers.Where(s => s.IsChecked).ToList().ForEach(s => taskMembers.Add(
                                                                                 new TaskMembersModel
                        {
                            TaskID        = ID,
                            SiteManagerID = s.ID,
                        }));

                        WatchingMembers.Where(s => s.IsWatcher).ToList().ForEach(s => taskWatchers.Add(
                                                                                     new TaskWatchersModel
                        {
                            TaskID        = ID,
                            SiteManagerID = s.ID,
                        }));


                        TaskModel taskData = new TaskModel()
                        {
                            ProjectID    = ParentLayout.SelectedProject.ID,
                            Title        = Title,
                            Description  = Description,
                            TaskStatusID = SelectedStatus?.ID,
                            Status       = SelectedStatus == null ? new StatusModel {
                                Title = StatusText, CreatedBy = ParentLayout.LoggedInUser.Name
                            } : null,
                            TaskTypeID = SelectedType?.ID,
                            Type       = SelectedType == null ? new TypeModel {
                                Title = TypeText, CreatedBy = ParentLayout.LoggedInUser.Name
                            } : null,
                            StartDate = StartDate,
                            DueDate   = DueDate,
                            Members   = taskMembers,
                            Watchers  = taskWatchers,
                            StampID   = SelectedStamp?.ID,
                            Stamp     = SelectedStamp == null ? new StampModel {
                                Title = StampText, CreatedBy = ParentLayout.LoggedInUser.Name
                            } : null,
                            SheetID = SelectedSheet?.ID,
                        };

                        HttpResponseMessage result = null;
                        if (isUpdate)
                        {
                            taskData.ID         = ID;
                            taskData.CreatedBy  = SelectedTask.CreatedBy;
                            taskData.CreatedOn  = SelectedTask.CreatedOn;
                            taskData.ModifiedBy = ParentLayout.LoggedInUser.Name;
                            taskData.ModifiedOn = DateTime.Now;
                            result = await apiHelper.PutTask(ParentLayout.LoggedInUser.Token, taskData).ConfigureAwait(false);
                        }
                        else
                        {
                            taskData.CreatedBy = ParentLayout.LoggedInUser.Name;
                            taskData.CreatedOn = DateTime.Now;
                            result             = await apiHelper.PostTask(ParentLayout.LoggedInUser.Token, taskData).ConfigureAwait(false);
                        }
                        if (result.IsSuccessStatusCode)
                        {
                            MessageBox.Show($"Task Saved Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                            await GetTasks();

                            #region Log Data

                            ActivityLogModel logData = new ActivityLogModel()
                            {
                                Type        = "Task",
                                Description = $"Task '{taskData.Title}' created by '{ParentLayout.LoggedInUser.Name}'",
                                ProjectID   = ParentLayout.SelectedProject.ID,
                                CreatedBy   = ParentLayout.LoggedInUser.Name,
                                CreatedOn   = DateTime.Now
                            };
                            if (isUpdate)
                            {
                                logData.Description = $"Task '{taskData.Title}' updated by '{ParentLayout.LoggedInUser.Name}'";
                            }
                            await logAPIHelper.PostActivityLog(ParentLayout.LoggedInUser.Token, logData);

                            #endregion

                            IsUpdate = false;
                            ClearFields();
                            await GetStamps();
                            await GetTypes();
                            await GetStatuses();
                            await GetTeamMembers();
                        }
                        else
                        {
                            MessageBox.Show("Error in saving Task", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        CanSaveTask = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                CanSaveTask = true;
            }
        }
        private async void OpenCloseOperations(object value)
        {
            switch (value.ToString())
            {
            case "Edit":
                if (SelectedTask != null)
                {
                    await GetTeamMembers();

                    ColSpan = 1;
                    OperationsVisibility = "Visible";

                    ID             = SelectedTask.ID;
                    Title          = SelectedTask.Title;
                    Description    = SelectedTask.Description;
                    SelectedStatus = SelectedTask.Status;
                    SelectedType   = SelectedTask.Type;

                    StartDate     = SelectedTask.StartDate;
                    DueDate       = SelectedTask.DueDate;
                    SelectedStamp = SelectedTask.Stamp;
                    SelectedSheet = SelectedTask.Sheet;

                    var taskMembers = await taskMembersAPIHelper.GetTaskMembersByTaskID(ParentLayout.LoggedInUser.Token, ID);

                    foreach (var tm in taskMembers)
                    {
                        tm.SiteManager.IsChecked = true;
                        TeamMembers.FirstOrDefault(w => w.ID == tm.SiteManager.ID).IsChecked = true;
                    }

                    SetCheckedText(null);


                    var taskWatchers = await taskWatchersAPIHelper.GetTaskWatchersByTaskID(ParentLayout.LoggedInUser.Token, ID);

                    foreach (var tw in taskWatchers)
                    {
                        tw.SiteManager.IsWatcher = true;
                        WatchingMembers.FirstOrDefault(w => w.ID == tw.SiteManager.ID).IsWatcher = true;
                    }

                    SetWatchersCheckedText(null);

                    IsUpdate = true;
                    return;
                }
                else
                {
                    MessageBox.Show("Please select a record to edit", "Select Record", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

            case "Create":
                ID                   = 0;
                IsUpdate             = false;
                ColSpan              = 1;
                OperationsVisibility = "Visible";
                ClearFields();
                await GetTeamMembers();

                break;

            default:
                ColSpan = ColSpan == 1 ? 2 : 1;
                OperationsVisibility = OperationsVisibility == "Visible" ? "Collapsed" : "Visible";
                break;
            }
        }