package com.isidroid.c23.ui._component

import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.LocationOn
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
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.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.isidroid.c23.R

@Composable
fun ShortOfficeInfoComponent(
    officeName: String,
    modifier: Modifier,
    containerClick: () -> Unit,
    buttonClick: (() -> Unit)? = containerClick,
    @StringRes titleRes: Int,
    @StringRes buttonTextRes: Int,
    bottomColor: Color = MaterialTheme.colorScheme.secondaryContainer,
    paddingBottom: Dp? = null,
    buttonDisabled: Boolean = false
) {
    Column(modifier = modifier) {
        Row(
            modifier = Modifier
                .background(bottomColor)
                .padding(vertical = 12.dp)
                .padding(horizontal = 12.dp)
                .clickable { containerClick() },
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = Icons.Rounded.LocationOn,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.secondary
            )

            Text(
                text = footerText(stringResource(id = titleRes), officeName),
                modifier = Modifier
                    .weight(1f)
                    .padding(horizontal = 12.dp),
                lineHeight = 20.sp
            )

            if (buttonClick != null)
                Button(
                    shape = RoundedCornerShape(24.dp),
                    onClick = buttonClick,
                    enabled = !buttonDisabled
                ) {
                    Text(stringResource(id = buttonTextRes).uppercase())
                }
        }


        if (paddingBottom != null)
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(paddingBottom)
                    .background(bottomColor)
            )

    }
}

private fun footerText(title: String, officeName: String) = buildAnnotatedString {
    withStyle(style = SpanStyle(fontStyle = FontStyle.Italic, fontSize = 14.sp)) {
        append(title)
    }

    append("\n")
    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, fontSize = 16.sp)) {
        append(officeName)
    }
}


@Preview
@Composable
private fun FooterPreview() {
    Surface {
        ShortOfficeInfoComponent(
            officeName = "Default Office Name",
            modifier = Modifier,
            containerClick = {},
            titleRes = R.string.app_name,
            buttonTextRes = R.string.app_name,
            bottomColor = MaterialTheme.colorScheme.secondaryContainer,
            buttonDisabled = false,
        )
    }
}