package com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.links

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.theme.lwa
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Stable
data class ResourcesDialogUio(
    val resources: List<LinkResource>,
) {
    @Stable
    data class LinkResource(
        val icon: DrawableResource,
        val tint: Boolean = false,
        val name: StringResource,
        val action: () -> Unit,
    )
}

@Immutable
object ResourcesDialogDefault {
    @Stable
    val padding: PaddingValues = PaddingValues(vertical = 4.dp, horizontal = 8.dp)

    @Stable
    val spacing: Dp = 4.dp
}

@Immutable
object ResourcesItemDefault {
    @Stable
    val padding: PaddingValues = PaddingValues(vertical = 2.dp, horizontal = 8.dp)

    @Stable
    val shape: Shape = RoundedCornerShape(8.dp)

    @Stable
    val spacing: Dp = 8.dp
}

@Composable
fun ResourcesMenu(
    modifier: Modifier = Modifier,
    viewModel: ResourcesViewModel = koinViewModel(),
) {
    ResourcesContent(
        modifier = modifier,
        dialog = viewModel.dialog,
    )
}

@Composable
private fun ResourcesContent(
    modifier: Modifier = Modifier,
    dialog: ResourcesDialogUio,
    padding: PaddingValues = ResourcesDialogDefault.padding,
    spacing: Dp = ResourcesDialogDefault.spacing,
) {
    Column(
        modifier = Modifier
            .padding(paddingValues = padding)
            .then(other = modifier),
        verticalArrangement = Arrangement.spacedBy(space = spacing),
    ) {
        dialog.resources.forEach { resource ->
            ResourcesItem(
                resource = resource,
                onClick = resource.action,
            )
        }
    }
}

@Composable
private fun ResourcesItem(
    modifier: Modifier = Modifier,
    padding: PaddingValues = ResourcesItemDefault.padding,
    spacing: Dp = ResourcesItemDefault.spacing,
    shape: Shape = ResourcesItemDefault.shape,
    resource: ResourcesDialogUio.LinkResource,
    onClick: () -> Unit,
) {
    Row(
        modifier = Modifier
            .clip(shape = shape)
            .clickable(onClick = onClick)
            .minimumInteractiveComponentSize()
            .fillMaxWidth()
            .padding(paddingValues = padding)
            .then(other = modifier),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(space = spacing),
    ) {
        Image(
            modifier = Modifier.size(size = 24.dp),
            colorFilter = when (resource.tint) {
                true -> ColorFilter.tint(color = MaterialTheme.lwa.colorScheme.base.primary)
                else -> null
            },
            painter = painterResource(resource = resource.icon),
            contentDescription = null,
        )
        Text(
            style = MaterialTheme.lwa.typography.system.body1,
            fontWeight = FontWeight.SemiBold,
            color = MaterialTheme.lwa.colorScheme.base.primary,
            text = stringResource(resource = resource.name),
        )
    }
}