# Checklist.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021-2023 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.mingraviditet.core.data.domain.knowledge

    import android.os.Parcelable
    import dk.nsp.mingraviditet.core.datastore.model.ChecklistDb
    import dk.nsp.mingraviditet.core.datastore.model.ChecklistItemDb
    import dk.nsp.mingraviditet.core.network.cms.models.NetworkChecklist
    import dk.nsp.mingraviditet.core.network.cms.models.NetworkChecklistItem
    import kotlinx.parcelize.Parcelize
    import kotlin.math.roundToInt

    @Parcelize
    data class Checklist(
        val id: String,
        val title: String,
        val description: String?,
        val items: List<ChecklistItem>,
    ) : Parcelable {
        fun checkedItems() = items.filter { it.value }

        fun completeness(): Float {
            return if (items.isNotEmpty()) {
                checkedItems().size.toFloat() / items.size
            } else {
                0F
            }
        }

        fun completenessAsDivision(): String {
            return "${items.filter { it.value }.size}/${items.size}"
        }

        fun getPercentageAsString(): String {
            return "${(completeness() * 100).roundToInt()}%"
        }
    }

    fun List<ChecklistItem>.completenessAsDivision(): String {
        return "${count { it.value }}/$size"
    }

    fun List<ChecklistItem>.completeness(): Float {
        return if (isNotEmpty()) {
            count { it.value }.toFloat() / size
        } else {
            0F
        }
    }

    fun List<ChecklistItem>.getPercentageAsString(): String {
        return "${(completeness() * 100).toInt()}%"
    }

    @Parcelize
    data class ChecklistItem(
        val id: String,
        val title: String,
        val value: Boolean,
        val order: Int,
    ) : Parcelable

    fun List<NetworkChecklist>.toDomainModel(): List<Checklist> {
        return this.map {
            Checklist(
                it.id.toString(),
                it.title,
                it.text,
                it.items.mapIndexed { i, item ->
                    item.toDomainModel(order = i)
                },
            )
        }
    }

    fun NetworkChecklistItem.toDbModel(index: Int): ChecklistItemDb {
        return ChecklistItemDb(id, title, false, order = index)
    }

    fun NetworkChecklist.toDbModel(): ChecklistDb {
        return ChecklistDb(id.toString(), title, text, items.mapIndexed { i, it -> it.toDbModel(i) })
    }

    fun List<NetworkChecklist>.toDbModel(): List<ChecklistDb> {
        return map { it.toDbModel() }
    }

    fun Checklist.toDbModel(): ChecklistDb {
        return ChecklistDb(id, title, description, items.map { it.toDbModel() })
    }

    fun NetworkChecklistItem.toDomainModel(order: Int): ChecklistItem {
        return ChecklistItem(id, title, false, order)
    }

    fun ChecklistItem.toDbModel(): ChecklistItemDb {
        return ChecklistItemDb(id, title, value, order)
    }

    fun ChecklistDb.toDomainModel(): Checklist {
        return Checklist(id, title, description, items.map { it.toDomainModel() })
    }

    fun ChecklistItemDb.toDomainModel(): ChecklistItem {
        return ChecklistItem(id, title, checked, order)
    }