package com.isidroid.c23.ui.screen.map

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.isidroid.c23.R
import com.isidroid.c23.ui._component.ShortOfficeInfoComponent
import com.isidroid.c23.ui._component.TopAppBarComponent
import com.isidroid.c23.ui.screen.map._components.RequestLocationPermissionComponent
import com.isidroid.c23.ui.screen.map._components.TPMapComponent
import com.isidroid.core.vm.SIDE_EFFECTS_KEY
import com.isidroid.ui.maps.model.MapMarker
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MapScreen(
    state: State<MapContract.State>,
    effectFlow: Flow<MapContract.Effect>?,
    spotResultStateFlow: StateFlow<List<MapMarker>>,
    onEventSent: (event: MapContract.Event) -> Unit,
    onNavigationRequested: (navigationEffect: MapContract.Effect.Navigation) -> Unit,
    modifier: Modifier = Modifier,
) {
    LaunchedEffect(SIDE_EFFECTS_KEY) {
        effectFlow?.collect { effect ->
            when (effect) {
                is MapContract.Effect.Navigation -> onNavigationRequested(effect)
            }
        }
    }

    RequestLocationPermissionComponent(
        state = state,
        onGranted = { onEventSent(MapContract.Event.PermissionGranted) }
    )

    Scaffold(
        modifier = modifier.fillMaxSize(),
        topBar = {
            TopAppBarComponent(
                text = stringResource(id = R.string.appbar_select_spot),
                alpha = .65f,
                onNavigationClick = { onEventSent(MapContract.Event.ToBack) }
            )
        }
    ) { paddingValues ->
        TPMapComponent(
            state = state,
            modifier = Modifier
                .fillMaxSize()
                .consumeWindowInsets(paddingValues),
            onEventSent = onEventSent,
            mapMarkersStateFlow = spotResultStateFlow
        )

        OfficeInfoComponent(
            state = state,
            containerClick = { onEventSent(MapContract.Event.ClickOnMarker(null)) },
            buttonClick = { onEventSent(MapContract.Event.SelectSpot(it)) },
            modifier = Modifier.padding(bottom = 0.dp),
            paddingBottom = paddingValues.calculateBottomPadding(),
        )
    }
}

@Composable
private fun OfficeInfoComponent(
    state: State<MapContract.State>,
    containerClick: () -> Unit,
    buttonClick: (String) -> Unit,
    modifier: Modifier = Modifier,
    paddingBottom: Dp = 0.dp
) {
    val spotsInfoList = state.value.spotsInfoList
    val isVisible = !spotsInfoList.isNullOrEmpty()
    val bottomColor = MaterialTheme.colorScheme.secondaryContainer.copy(alpha = .75f)

    Box(modifier = modifier.fillMaxSize()) {
        AnimatedVisibility(
            visible = isVisible,
            enter = slideInVertically(initialOffsetY = { it * 2 }),
            exit = slideOutVertically(targetOffsetY = { it }),
            modifier = Modifier.align(Alignment.BottomCenter)
        ) {
            Column(modifier = Modifier.fillMaxWidth()) {
                if (!spotsInfoList.isNullOrEmpty()) {
                    LazyColumn(modifier = Modifier) {
                        items(spotsInfoList) { richSpot ->
                            ShortOfficeInfoComponent(
                                officeName = buildString {
                                    append(richSpot.spot.name)
                                },
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(horizontal = 2.dp),
                                containerClick = containerClick,
                                buttonClick = { buttonClick(richSpot.spot.id) },
                                titleRes = R.string.preview_footer_title,
                                buttonTextRes = R.string.select_spot,
                                bottomColor = bottomColor,
                            )
                        }
                    }
                }

                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(paddingBottom)
                        .background(bottomColor)
                )
            }

        }
    }
}