package com.dowstats.service.datamaps

import com.dowstats.data.dto.controllers.building.BuildingShortDto
import com.dowstats.data.entities.building.Building
import com.dowstats.data.entities.unit.DowUnitObject.toUnitDto

object CommonMapping {
    fun List<Building>.toBuildingShortDto(withoutUnits: Boolean = false): List<BuildingShortDto> =
        this
            .sortedWith(compareBy<Building> { it.uiIndexHint }.thenBy{ it.buildCostRequisition?.let { 0 - it } })
            .mapNotNull {it.toBuildingShortDto(withoutUnits) }

    fun Building.toBuildingShortDto(withoutUnits: Boolean = false): BuildingShortDto? {
        val name = this.name ?: this.filename?.replace(".rgd", "")?.replace("_", " ")
        val icon = this.icon
        return if (name == null || icon == null) null else BuildingShortDto(
            name, icon, this.id!!,
            this.filename!!,
            if(!withoutUnits) { this.units?.toList()?.sortedBy { it.uiIndexHint }?.toUnitDto() ?: emptyList()} else emptyList(),
            this.armorType?.name!!,
            (this.detectRadius ?: 0) > 0,
        )
    }
    
}