package xyz.magicalbits.smsremote.sim

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.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import xyz.magicalbits.smsremote.R
import xyz.magicalbits.smsremote.components.baselineHeight
import xyz.magicalbits.smsremote.theme.JetchatTheme

@Composable
fun SimScreen(
    simData: SimScreenState,
    nestedScrollInteropConnection: NestedScrollConnection = rememberNestedScrollInteropConnection(),
    onConversationClicked: (String) -> Unit = { },
) {
    val scrollState = rememberScrollState()

    Box(
        modifier = Modifier.fillMaxSize().nestedScroll(nestedScrollInteropConnection)
    ) {
        Surface {
            Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState).padding(horizontal = 16.dp)) {
                Name(simData, Modifier.baselineHeight(32.dp))

                Spacer(modifier = Modifier.height(8.dp))

                simData.conversations.forEach {
                    ConversationPreview(it, onConversationClicked)
                }
            }
        }
    }
}

@Composable
private fun Name(
    simData: SimScreenState,
    modifier: Modifier
) {
    Text(
        text = simData.phoneNumber,
        modifier = modifier,
        style = MaterialTheme.typography.headlineSmall
    )
}

@Composable
private fun ConversationPreview(
    conversation: ConversationRowState,
    onConversationClicked: (String) -> Unit,
) {
    Column(modifier = Modifier
        .clickable(
            onClick = {
                onConversationClicked(conversation.phoneNumber)
            }
        )
    ) {
        HorizontalDivider()
        Row(
            modifier = Modifier
                .wrapContentHeight()
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(text = conversation.phoneNumber)
            Spacer(modifier = Modifier.weight(1f))
            Text(text = conversation.messageTimestamp.toString())
        }
        Row(
            modifier = Modifier
                .padding(start = 16.dp, end = 16.dp)
        ) {
            Text(text = conversation.lastMessage)
        }
    }
}

@Preview
@Composable
fun SimScreenPreview() {
    JetchatTheme {
        SimScreen(
            simData = SimScreenState(
                phoneNumber = "+420123456789",
                conversations = listOf(
                    ConversationRowState("+15558880000", "last msg", 12345),
                    ConversationRowState("+15558880000", "last msg", 12345),
                    ConversationRowState("+15558880000", "last msg", 12345),
                )
            )
        )
    }
}

@Composable
fun SimError() {
    Text(stringResource(R.string.sim_error))
}

