/*
 * Copyright 2024 Mifos Initiative
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
 */
package org.mifospay.feature.standing.instruction.components

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun InstructionTypeChip(
    type: String?,
    modifier: Modifier = Modifier,
) {
    val typeEnum = StandingInstructionType.fromString(type)

    val (backgroundColor, contentColor) = when (typeEnum) {
        StandingInstructionType.FIXED ->
            MaterialTheme.colorScheme.primaryContainer to
                MaterialTheme.colorScheme.onPrimaryContainer

        StandingInstructionType.DUES ->
            MaterialTheme.colorScheme.tertiaryContainer to
                MaterialTheme.colorScheme.onTertiaryContainer

        else -> MaterialTheme.colorScheme.surfaceVariant to MaterialTheme.colorScheme.onSurfaceVariant
    }

    Surface(
        modifier = modifier,
        shape = RoundedCornerShape(4.dp),
        color = backgroundColor,
        contentColor = contentColor,
    ) {
        Text(
            text = typeEnum?.name ?: "Not Available",
            modifier = Modifier.padding(4.dp),
            color = if (typeEnum == null) {
                MaterialTheme.colorScheme.error
            } else {
                MaterialTheme.colorScheme.onBackground
            },
            style = MaterialTheme.typography.bodySmall,
        )
    }
}

enum class StandingInstructionType {
    FIXED,
    DUES,
    ;

    companion object {
        fun fromString(value: String?): StandingInstructionType? {
            return entries.find { it.name.equals(value, ignoreCase = true) }
        }
    }
}
