internal static MedicationStatement ToFhirInternal(HVMedication hvMedication, MedicationStatement medicationStatement)
        {
            var embeddedMedication = new FhirMedication();

            embeddedMedication.Id = "med" + Guid.NewGuid();

            medicationStatement.Contained.Add(ToFhirInternal(hvMedication, embeddedMedication));
            medicationStatement.Medication = embeddedMedication.GetContainerReference();

            medicationStatement.SetStatusAsActive();
            medicationStatement.SetTakenAsNotApplicable();

            medicationStatement.Dosage = AddDosage(hvMedication.Dose, hvMedication.Frequency, hvMedication.Route);

            if (hvMedication.Prescription != null)
            {
                var embeddedMedicationRequest = new MedicationRequest();
                embeddedMedicationRequest.Id = "medReq" + Guid.NewGuid();

                Practitioner prescribedBy = hvMedication.Prescription.PrescribedBy.ToFhir();
                prescribedBy.Id = "prac" + Guid.NewGuid();
                medicationStatement.Contained.Add(prescribedBy);

                MedicationRequest request = ToFhirInternal(hvMedication.Prescription, embeddedMedicationRequest);
                request.Medication = embeddedMedication.GetContainerReference();
                request.Requester  = new MedicationRequest.RequesterComponent
                {
                    Agent = prescribedBy.GetContainerReference()
                };
                medicationStatement.Contained.Add(request);
                medicationStatement.BasedOn.Add(embeddedMedicationRequest.GetContainerReference());
            }

            return(medicationStatement);
        }
        public void WhenMedicationStatementTransformedToHealthVault_ThenDosageIsCopiedToDoseFrequencyAndRoute()
        {
            var embeddedMedication = new FhirMedication()
            {
                Code = new CodeableConcept()
                {
                    Text = "Amoxicillin 250mg/5ml Suspension"
                }
            };
            string embeddedMedicationId = "med" + Guid.NewGuid();

            embeddedMedication.Id = embeddedMedicationId;

            const int    dosage              = 1;
            const int    period              = 8;
            const string routeCode           = "po";
            var          medicationStatement = new MedicationStatement()
            {
                Medication = new ResourceReference(embeddedMedicationId),
                Dosage     = new System.Collections.Generic.List <Dosage>
                {
                    new Dosage
                    {
                        Dose = new SimpleQuantity()
                        {
                            Value  = dosage,
                            Unit   = "Tablets",
                            System = HealthVaultVocabularies.GenerateSystemUrl
                                         (HealthVaultVocabularies.MedicationDoseUnits,
                                         HealthVaultVocabularies.Wc),
                            Code = "tablet"
                        },
                        Timing = new Timing
                        {
                            Repeat = new Timing.RepeatComponent
                            {
                                Period     = period,
                                PeriodUnit = Timing.UnitsOfTime.H
                            }
                        },
                        Route = new CodeableConcept
                        {
                            Text   = "By mouth",
                            Coding = new System.Collections.Generic.List <Coding>
                            {
                                new Coding
                                {
                                    Code   = routeCode,
                                    System = HealthVaultVocabularies.GenerateSystemUrl
                                                 (HealthVaultVocabularies.MedicationRoutes,
                                                 HealthVaultVocabularies.Wc),
                                    Version = "2"
                                }
                            }
                        }
                    }
                }
            };

            medicationStatement.Contained.Add(embeddedMedication);
            medicationStatement.Medication = embeddedMedication.GetContainerReference();

            var hvMedication = medicationStatement.ToHealthVault() as HVMedication;

            Assert.AreEqual(dosage, hvMedication?.Dose?.Structured?.First()?.Value);
            Assert.AreEqual(period, hvMedication?.Frequency?.Structured?.First()?.Value);
            Assert.AreEqual(routeCode, hvMedication?.Route?.First()?.Value);
        }