package cl.homelogic.platform.designsystem.components.organisms

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
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.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import cl.homelogic.platform.designsystem.components.molecules.SymbolButton
import cl.homelogic.platform.designsystem.extensions.requirePrecondition
import cl.homelogic.platform.designsystem.foundations.Padding
import cl.homelogic.platform.designsystem.foundations.Sizes
import cl.homelogic.platform.designsystem.foundations.Symbols

sealed class HeaderTypes {
    data class Home(
        val title: String,
        val onSettings: () -> Unit,
    ) : HeaderTypes()

    data class SubHome(
        val title: String,
    ) : HeaderTypes()

    data class Step(
        val title: String,
        val onBack: () -> Unit,
    ) : HeaderTypes()

    data class Modal(
        val onClosed: () -> Unit,
    ) : HeaderTypes()

    data class Flex(
        val title: String,
        val leftShortcuts: List<FlexHeaderShortcut>,
        val rightShortcuts: List<FlexHeaderShortcut>,
    ) : HeaderTypes() {
        init {
            requirePrecondition(leftShortcuts.size <= 3)
            requirePrecondition(rightShortcuts.size <= 3)
        }
    }
}

data class FlexHeaderShortcut(
    val icon: Symbols,
    val onClick: () -> Unit,
)

@Composable
fun Header(
    type: HeaderTypes,
    showDivider: Boolean = true,
    modifier: Modifier = Modifier,
) {
    Surface(
        modifier = modifier
            .fillMaxWidth()
            .background(MaterialTheme.colorScheme.surface)
            .height(Sizes.Header.Regular),
    ) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ) {
            when (type) {
                is HeaderTypes.Home -> buildHomeHeader(type)
                is HeaderTypes.SubHome -> buildSubHomeHeader(type)
                is HeaderTypes.Step -> buildFlowHeader(type)
                is HeaderTypes.Modal -> buildModalHeader(type)
                is HeaderTypes.Flex -> buildFlexHeader(type)
            }

            if (showDivider) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(Sizes.Stroke.Regular)
                        .background(MaterialTheme.colorScheme.outline)
                        .align(Alignment.BottomStart),
                )
            }
        }
    }
}

@Composable
private fun buildHomeHeader(type: HeaderTypes.Home) {
    Text(
        text = type.title,
        textAlign = TextAlign.Center,
        style = typography.titleMedium,
        maxLines = 1,
        overflow = TextOverflow.Clip,
    )

    Row(
        modifier = Modifier
            .fillMaxSize()
            .padding(Padding.small),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.End,
    ) {
        SymbolButton(
            icon = Symbols.IconSettings,
            onClick = type.onSettings,
        )
    }
}

@Composable
private fun buildSubHomeHeader(type: HeaderTypes.SubHome) {
    Text(
        text = type.title,
        textAlign = TextAlign.Center,
        style = typography.titleMedium,
        maxLines = 1,
        overflow = TextOverflow.Clip,
    )
}

@Composable
private fun buildFlowHeader(type: HeaderTypes.Step) {
    Row(
        modifier = Modifier
            .fillMaxSize()
            .padding(Padding.small),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start,
    ) {
        SymbolButton(
            icon = Symbols.IconArrowBack,
            onClick = type.onBack,
        )
    }

    Text(
        text = type.title,
        textAlign = TextAlign.Center,
        style = typography.titleMedium,
        maxLines = 1,
        overflow = TextOverflow.Clip,
    )
}

@Composable
private fun buildModalHeader(type: HeaderTypes.Modal) {
    Row(
        modifier = Modifier
            .fillMaxSize()
            .padding(Padding.small),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start,
    ) {
        SymbolButton(
            icon = Symbols.IconClose,
            onClick = type.onClosed,
        )
    }
}

@Composable
private fun buildFlexHeader(type: HeaderTypes.Flex) {
    Row(
        modifier = Modifier
            .fillMaxSize()
            .padding(Padding.small),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start,
    ) {
        type.leftShortcuts.forEach { shortcut ->
            SymbolButton(
                icon = shortcut.icon,
                onClick = shortcut.onClick,
            )
        }
    }

    Text(
        text = type.title,
        textAlign = TextAlign.Center,
        style = typography.titleMedium,
        maxLines = 1,
        overflow = TextOverflow.Clip,
    )

    Row(
        modifier = Modifier
            .fillMaxSize()
            .padding(Padding.small),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.End,
    ) {
        type.rightShortcuts.forEach { shortcut ->
            SymbolButton(
                icon = shortcut.icon,
                onClick = shortcut.onClick,
            )
        }
    }
}
