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

import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextField
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextFieldFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.character__inventory__add_to_purse__title
import lwacharactersheet.composeapp.generated.resources.character__inventory__remove_from_purse__title
import lwacharactersheet.composeapp.generated.resources.generic__copper__singular
import lwacharactersheet.composeapp.generated.resources.generic__gold__singular
import lwacharactersheet.composeapp.generated.resources.generic__silver__singular
import org.jetbrains.compose.resources.getString

class PurseDialogFactory {
    private val decimalChecker = Regex("""^\s*\d*\s*${'$'}""")

    suspend fun convertToDialogUio(
        scope: CoroutineScope,
        characterSheetId: String?,
        signFlow: StateFlow<Boolean>,
    ): PurseDialogUio? {
        if (characterSheetId == null) return null

        val gold = createLwaTextFieldFlow(
            label = getString(Res.string.generic__gold__singular),
            value = "",
        )
        val silver = createLwaTextFieldFlow(
            label = getString(Res.string.generic__silver__singular),
            value = "",
        )
        val copper = createLwaTextFieldFlow(
            label = getString(Res.string.generic__copper__singular),
            value = "",
        )
        return PurseDialogUio(
            characterSheetId = characterSheetId,
            label = signFlow.map {
                when (it) {
                    true -> getString(Res.string.character__inventory__add_to_purse__title)
                    false -> getString(Res.string.character__inventory__remove_from_purse__title)
                }
            }.stateIn(
                scope = scope,
                started = SharingStarted.Lazily,
                initialValue = getString(Res.string.character__inventory__add_to_purse__title),
            ),
            add = signFlow,
            gold = gold.createLwaTextField {
                gold.errorFlow.value = check(it)
                gold.valueFlow.value = it
            },
            silver = silver.createLwaTextField {
                silver.errorFlow.value = check(it)
                silver.valueFlow.value = it
            },
            copper = copper.createLwaTextField {
                copper.errorFlow.value = check(it)
                copper.valueFlow.value = it
            },
            enableConfirm = combine(
                gold.errorFlow,
                silver.errorFlow,
                copper.errorFlow
            ) { goldError, silverError, copperError ->
                !goldError && !silverError && !copperError
            }.stateIn(
                scope = scope,
                started = SharingStarted.Lazily,
                initialValue = !gold.errorFlow.value && !silver.errorFlow.value && !copper.errorFlow.value,
            ),
        )
    }

    private fun check(value: String): Boolean = !decimalChecker.matches(value)
}