# ObservationsControllerMock.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2020 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.api.observations

    import dk.nsp.models.*
    import dk.nsp.shared.ResultWrapper
    import dk.nsp.shared.Success
    import dk.nsp.shared.toSuccess
    import dk.nsp.shared.util.asyncDefault
    import kotlinx.coroutines.CoroutineScope
    import kotlinx.coroutines.Deferred
    import java.math.RoundingMode
    import java.time.LocalDateTime
    import java.util.*
    import kotlin.math.ln

    class ObservationsControllerMock : IObservationsController {
        override suspend fun getObservations(
            scope: CoroutineScope,
            episodeOfCareIdentifier: String
        ): Deferred<ResultWrapper<List<Observation>>> =
            scope.asyncDefault {
                val now = LocalDateTime.now()
                val pubisMeasurements = (1L..40L).mapNotNull {
                    if (it % 5L != 0L) {
                        return@mapNotNull null
                    }
                    Observation(
                        now.plusWeeks(it),
                        listOf(
                            ObservationValues(
                                (ln(it.toDouble()) * 10).toInt().toString(),
                                "cm",
                                "PubisMeasurement",
                            )
                        ),
                        measuredBy = HealthCarePerformer(
                            person = Person(PersonName(prefixName = "Dr.", firstNames = listOf("Test"), middleNames = listOf("Middle", "Names"), lastName = "Testsen")),
                            role = "SuperRole",
                            healthCareOrganization = null,
                            healthCareOrganizationAdress = null,
                            healthCareOrganizationPhoneNumber = null
                        ),
                        "fetus string",
                        "activityRef",
                        ObservationType.PubisMeasurements,
                        UUID.randomUUID().toString()
                    )
                }

                Success(listOf(pubisMeasurements).flatten())
            }

        override suspend fun getClinicalObservationsTable(
            scope: CoroutineScope,
            episodeOfCareIdentifier: String
        ): Deferred<ResultWrapper<ObservationTable>> =
            scope.asyncDefault {
                val now = LocalDateTime.now()
                val random = Random()

                val maxWeeks = 40
                val startWeek = 5
                val numberOfActivities = 3
                val weekStep = 5

                val motherInfo = ObservationTableMother(
                    (startWeek..(numberOfActivities * weekStep) step weekStep).mapNotNull { mockWeekNumber ->
                        val week = mockWeekNumber + Random().nextDouble().toBigDecimal()
                            .setScale(1, RoundingMode.HALF_UP).toDouble()

                        val urineString = (2..(Random().nextInt(4)) + 1).joinToString("\n") {
                            "+ Urine"
                        }

                        val edemaString = (1..(Random().nextInt(3) + 1)).joinToString("\n") {
                            "Edema week $mockWeekNumber"
                        }

                        ObservationTableMotherRows(
                            activityId = "$week",
                            date = now.minusWeeks((maxWeeks - mockWeekNumber).toLong()),
                            gestationAge = "$week",
                            weeksIntoPregnancy = week,
                            weight = NumericValueWithUnit(week.toBigDecimal(), "kg"),
                            bloodPressure = "120/80",
                            urine = urineString,
                            edema = edemaString,
                            pubismeasurement = NumericValueWithUnit(
                                (random.nextDouble() * maxWeeks).toBigDecimal(),
                                "cm"
                            )
                        )
                    }
                )

                val numberOfFetuses = 2

                val fetusesInfo = (1..numberOfFetuses).map { fetusId ->
                    ObservationTableFetuses(
                        "$fetusId",
                        "Fetus $fetusId",
                        motherInfo.rows?.mapIndexedNotNull { index, motherActivity ->
                            if (index == 0 && fetusId == numberOfFetuses) {
                                return@mapIndexedNotNull null
                            }

                            val fetusActivitiesStrings = (1..(Random().nextInt(4) + 1)).map {
                                "$fetusId Act$it"
                            }
                            ObservationTableRows(
                                activityId = motherActivity.activityId,
                                date = motherActivity.date,
                                gestationAge = motherActivity.gestationAge,
                                fetusPresentation = "FetusPres $fetusId",
                                fetusWeightEstimate = NumericValueWithUnit(
                                    (4500 * Random().nextDouble()).toBigDecimal(),
                                    "g"
                                ),
                                fetusActivity = fetusActivitiesStrings
                            )
                        }
                    )
                }

                ObservationTable(
                    motherInfo,
                    fetusesInfo
                ).toSuccess()
            }

        override suspend fun getScanObservationsTable(
            scope: CoroutineScope,
            episodeOfCareIdentifier: String
        ): Deferred<ResultWrapper<ScanningObservationTable>> =
            scope.asyncDefault {
                val now = LocalDateTime.now()

                val maxWeeks = 40
                val startWeek = 5
                val endWeek = 10
                val weekStep = 5

                val motherInfo = ScanningObservationTableMother(
                    (startWeek..endWeek step weekStep).mapNotNull { mockWeekNumber ->
                        val week = mockWeekNumber + Random().nextDouble()

                        val amnioticFluid = Random().nextInt(3).toBigDecimal()

                        val flow = (1..(Random().nextInt(3) + 1)).joinToString("\n") {
                            "Flow $mockWeekNumber"
                        }

                        ScanningObservationTableMotherRows(
                            activityId = "$week",
                            date = now.minusWeeks((maxWeeks - mockWeekNumber).toLong()),
                            gestationAge = "$week",
                            weeksIntoPregnancy = week,
                            amnioticFluid = NumericValueWithUnit(amnioticFluid, "+ fluid"),
                            flow = flow,
                            cervix = NumericValueWithUnit(
                                (mockWeekNumber * Random().nextDouble()).toBigDecimal(),
                                "cm"
                            ),
                            dvp = NumericValueWithUnit(
                                (mockWeekNumber * Random().nextDouble()).toBigDecimal(),
                                "cm"
                            )
                        )
                    }
                )

                val numberOfFetuses = 2

                val fetusesInfo = (1..numberOfFetuses).map { fetusId ->

                    ScanningObservationTableFetuses(
                        "$fetusId",
                        "Fetus $fetusId",
                        motherInfo.rows?.mapIndexedNotNull { index, motherActivity ->
                            if (index == 0 && fetusId == numberOfFetuses) {
                                return@mapIndexedNotNull null
                            }
                            ScanningObservationTableRows(
                                activityId = motherActivity.activityId,
                                date = motherActivity.date,
                                gestationAge = motherActivity.gestationAge,
                                fetusPresentation = "FetusPres $fetusId",
                                weeksIntoPregnancy = motherActivity.weeksIntoPregnancy,
                                ulWeight = NumericValueWithUnit(
                                    (4500 * Random().nextDouble()).toBigDecimal(),
                                    "g"
                                ),
                                weightDeviation = RangeValueWithUnit(
                                    (100 * Random().nextDouble()).toBigDecimal().toString(),
                                    "g"
                                )
                            )
                        }
                    )
                }

                ScanningObservationTable(
                    motherInfo,
                    fetusesInfo
                ).toSuccess()
            }
    }