//public 

        public AddHazardousSubstanceFurtherControlMeasureTaskViewModel GetViewModel()
        {
            var riskAssessment = _hazardousSubstanceRiskAssessmentService.GetRiskAssessment(_riskAssessmentId, _companyId);
            var viewModel = new AddHazardousSubstanceFurtherControlMeasureTaskViewModel()
                                {
                                    CompanyId = _companyId,
                                    RiskAssessmentId = _riskAssessmentId,
                                    ExistingDocuments = new ExistingDocumentsViewModel()
                                                            {
                                                                DocumentTypeId = (int) DocumentTypeEnum.HSRADocumentType
                                                            },
                                    TaskReoccurringTypes = new TaskReoccurringType().ToSelectList(),
                                    DoNotSendTaskCompletedNotification = riskAssessment.RiskAssessor == null ? false : riskAssessment.RiskAssessor.DoNotSendTaskCompletedNotifications,
                                    DoNotSendTaskOverdueNotification = riskAssessment.RiskAssessor == null ? false : riskAssessment.RiskAssessor.DoNotSendTaskOverdueNotifications
                                };

            return viewModel;
        }
        public JsonResult NewFurtherControlMeasureTask(AddHazardousSubstanceFurtherControlMeasureTaskViewModel viewModel, DocumentsToSaveViewModel documentsToSave)
        {
            if (!ModelState.IsValid)
                return Json(new { Success = false, Errors = ModelState.GetErrorMessages() });

            if (!viewModel.IsRecurring)
            {
                viewModel.TaskReoccurringTypeId = (int)TaskReoccurringType.None;
            }

            var taskGuid = Guid.NewGuid();
            var request = SaveFurtherControlMeasureTaskRequest.Create(
                viewModel.Title,
                viewModel.Description,
                viewModel.Reference,
                viewModel.TaskCompletionDueDate,
                viewModel.TaskStatusId,
                viewModel.CompanyId,
                viewModel.RiskAssessmentId,
                viewModel.TaskAssignedToId.Value,
                viewModel.TaskReoccurringTypeId,
                viewModel.FirstDueDate, 
                viewModel.TaskReoccurringEndDate, 
                CurrentUser.UserId, 
                documentsToSave.CreateDocumentRequests,
                documentsToSave.DeleteDocumentRequests,
                !viewModel.DoNotSendTaskNotification,
                !viewModel.DoNotSendTaskCompletedNotification,
                !viewModel.DoNotSendTaskOverdueNotification,
                viewModel.DoNotSendTaskDueTomorrowNotification,
                taskGuid);

            HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDto result;
            using (var session = _businessSafeSessionManager.Session)
            {
                result = _furtherControlMeasureTaskService.AddFurtherControlMeasureTask(request);
                _businessSafeSessionManager.CloseSession();
            }

            _bus.Publish(new TaskAssigned { TaskGuid = taskGuid });
            
            return Json(new { Success = true, result.Id, CreatedOn = result.CreatedDate });
        }
        public void Given_valid_post_When_CreateFurtherControlMeasureTask_Then_mapped_request_to_service_has_correct_values_and_returns_json()
        {
            // Given
            var controller = GetTarget();
            var returnedTaskDto = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDto()
            {
                Id = 123,
                Title = "title",
                Description = "description",
                CreatedDate = "23/06/1980",
                TaskCompletionDueDate = "23/06/2016",
                TaskAssignedTo = new EmployeeDto()
                {
                    Id = Guid.NewGuid(),
                    FullName = "Marcellas Wallace"
                }
            };
            var passedSaveFurtherControlMeasureTaskRequest = new SaveFurtherControlMeasureTaskRequest();

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(returnedTaskDto)
                .Callback<SaveFurtherControlMeasureTaskRequest>(y => passedSaveFurtherControlMeasureTaskRequest = y);

            // When
            var postedAddEditFurtherControlMeasureTaskViewModel = new AddHazardousSubstanceFurtherControlMeasureTaskViewModel()
                                                                  {
                                                                      RiskAssessmentId = 1,
                                                                      Reference = "Test",
                                                                      Description = "Test",
                                                                      Title = "Test",
                                                                      CompanyId = 1,
                                                                      TaskAssignedToId = new Guid(),
                                                                      TaskCompletionDueDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                                                                      ExistingDocuments = new ExistingDocumentsViewModel()
                                                                  };

            var result = controller.NewFurtherControlMeasureTask(postedAddEditFurtherControlMeasureTaskViewModel, new DocumentsToSaveViewModel());

            // Then
            _furtherControlMeasureTaskService
                .Verify(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.RiskAssessmentId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.RiskAssessmentId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Reference, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Reference));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Description, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Description));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Title, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Title));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.CompanyId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.CompanyId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.TaskAssignedToId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.TaskAssignedToId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.TaskCompletionDueDate.Value.ToString("dd/MM/yyyy HH:mm:ss"), Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.TaskCompletionDueDate));


            dynamic data = result.Data;
            var success = data.GetType().GetProperty("Success").GetValue(data, null);
            var id = data.GetType().GetProperty("Id").GetValue(data, null);
            var createdDate = data.GetType().GetProperty("CreatedOn").GetValue(data, null);

            Assert.That(success, Is.EqualTo(true));
            Assert.That(id, Is.EqualTo(returnedTaskDto.Id));
            Assert.That(createdDate, Is.EqualTo(returnedTaskDto.CreatedDate));
        }
        public void Given_isrecurring_is_false_When_CreateFurtherControlMeasureTask_Then_setl_recurringType_to_none()
        {
            // Given
            var controller = GetTarget();
            var returnedTaskDto = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDto()
            {
                Id = 123,
                Title = "title",
                Description = "description",
                CreatedDate = "23/06/1980",
                TaskCompletionDueDate = "23/06/2016",
                TaskAssignedTo = new EmployeeDto()
                {
                    Id = Guid.NewGuid(),
                    FullName = "Marcellas Wallace"
                },
                IsReoccurring = false,
                TaskReoccurringType = TaskReoccurringType.FiveYearly
            };
            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(returnedTaskDto);

            // When
            var postedAddEditFurtherControlMeasureTaskViewModel = new AddHazardousSubstanceFurtherControlMeasureTaskViewModel()
            {
                RiskAssessmentId = 1,
                Reference = "Test",
                Description = "Test",
                Title = "Test",
                CompanyId = 1,
                TaskAssignedToId = new Guid(),
                TaskCompletionDueDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                ExistingDocuments = new ExistingDocumentsViewModel()
            };

            var result = controller.NewFurtherControlMeasureTask(postedAddEditFurtherControlMeasureTaskViewModel, new DocumentsToSaveViewModel());

            // Then
            _furtherControlMeasureTaskService
                .Verify(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(y => y.TaskReoccurringTypeId == (int)TaskReoccurringType.None)));

        }