package com.pixelized.desktop.lwa.ui.composable.character.inventory

import com.pixelized.desktop.lwa.repository.settings.model.Settings
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.utils.extention.unAccent
import com.pixelized.shared.lwa.model.item.Item

class InventoryDialogFactory {

    fun filterItems(
        items: Collection<Item>,
        filter: String,
        setting: Settings,
    ): Collection<Item> {
        return if (setting.isGameMaster == true) {
            items.filter {
                it.metadata.label.unAccent().contains(other = filter, ignoreCase = true)
            }
        } else {
            items.filter {
                it.tags.contains(ADDABLE_TAG_ID)
            }.filter {
                it.metadata.label.unAccent().contains(other = filter, ignoreCase = true)
            }
        }
    }

    fun convertToDialogUio(
        items: Collection<Item>,
        filter: LwaTextFieldUio,
        characterSheetId: String?,
    ): InventoryDialogUio? {
        if (characterSheetId == null) return null

        return InventoryDialogUio(
            characterSheetId = characterSheetId,
            filter = filter,
            items = items.map { item ->
                InventoryDialogUio.Item(
                    itemId = item.id,
                    label = item.metadata.label,
                    tooltips = takeIf { item.metadata.description.isNotEmpty() }?.let {
                        InventoryDialogUio.Item.ToolTip(
                            label = item.metadata.label,
                            description = item.metadata.description,
                            image = item.metadata.image,
                        )
                    },
                )
            }
        )
    }

    companion object {
        const val ADDABLE_TAG_ID = "META:ADDABLE"
    }
}