package com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.onClick
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.PointerButton
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.lordcodes.turtle.Arguments
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonAlteration
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonPortrait
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonRoll
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonStats
import com.pixelized.desktop.lwa.ui.shaders.packs.GoldenFlow
import com.pixelized.desktop.lwa.ui.shaders.shaderBorder
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.utils.extention.invert

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CharacterRibbon(
    modifier: Modifier = Modifier,
    layoutDirection: LayoutDirection,
    viewModel: CharacterRibbonViewModel,
    shape: Shape = MaterialTheme.lwa.shapes.portrait,
    size: DpSize = MaterialTheme.lwa.dimen.portrait.minimized,
    padding: PaddingValues = MaterialTheme.lwa.dimen.paddingValues,
    onCharacterLeftClick: (characterSheetId: String) -> Unit,
    onCharacterRightClick: (characterSheetId: String) -> Unit,
) {
    val characters = viewModel.characters.collectAsState()

    CompositionLocalProvider(
        LocalLayoutDirection provides layoutDirection
    ) {
        LazyColumn(
            modifier = modifier,
            contentPadding = padding,
            verticalArrangement = Arrangement.spacedBy(
                space = MaterialTheme.lwa.dimen.paddingValue,
            ),
        ) {
            items(
                items = characters.value,
                key = { it.characterSheetId },
            ) {
                Box(
                    modifier = Modifier
                        .animateItem()
                        .clip(shape = shape)
                        .graphicsLayer { if (it.hideOverruled) this.alpha = 0.3f }
                        .onClick(
                            matcher = PointerMatcher.mouse(PointerButton.Primary),
                            enabled = it.enableDetail,
                            onClick = { onCharacterLeftClick(it.characterSheetId) },
                        )
                        .onClick(
                            matcher = PointerMatcher.mouse(PointerButton.Secondary),
                            enabled = it.enableDetail,
                            onClick = { onCharacterRightClick(it.characterSheetId) },
                        ),
                ) {
                    CharacterRibbonPortrait(
                        size = size,
                        character = it.portrait,
                    )
                    Column(
                        modifier = Modifier.size(size),
                        verticalArrangement = Arrangement.SpaceBetween,
                    ) {
                        CompositionLocalProvider(
                            LocalLayoutDirection provides layoutDirection.invert
                        ) {
                            CharacterRibbonAlteration(
                                alterations = it.alterations,
                            )
                        }
                        CharacterRibbonStats(
                            status = it.status,
                        )
                    }
                    CharacterRibbonRoll(
                        roll = viewModel.roll(characterSheetId = it.characterSheetId),
                    )
                    AnimatedVisibility(
                        visible = it.levelUp,
                        enter = fadeIn(),
                        exit = fadeOut()
                    ) {
                        Box(
                            modifier = Modifier
                                .size(size)
                                .shaderBorder(width = 2.dp, shape = shape, shader = GoldenFlow)
                        )
                    }
                }
            }
        }
    }
}