package previews

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import net.buzzert.kordophonedroid.R
import net.buzzert.kordophonedroid.ui.conversationlist.ConversationListItem
import net.buzzert.kordophonedroid.ui.conversationlist.ConversationListScreen
import net.buzzert.kordophonedroid.ui.conversationlist.NoContentView
import net.buzzert.kordophonedroid.ui.messagelist.AttachmentRowItem
import net.buzzert.kordophonedroid.ui.messagelist.MessageEntry
import net.buzzert.kordophonedroid.ui.messagelist.MessageListItem
import net.buzzert.kordophonedroid.ui.messagelist.MessageMetadata
import net.buzzert.kordophonedroid.ui.messagelist.MessageTranscript
import net.buzzert.kordophonedroid.ui.settings.SettingsScreen
import java.util.Date

// - Conversation List

@Preview
@Composable
fun ConversationListItemPreview() {
    Column(modifier = Modifier.background(MaterialTheme.colors.background)) {
        ConversationListItem(name = "James Magahern", id = "asdf", lastMessagePreview = "This is a test", date = Date(), isUnread = true) {}
    }
}

@Preview
@Composable
fun ConversationListScreenPreview() {
    ConversationListScreen()
}

// - Message List

private fun testMessageMetadata(fromMe: Boolean, delivered: Boolean): MessageMetadata {
    return MessageMetadata(
        fromMe = fromMe,
        fromAddress = if (fromMe) "<me>" else "cool@cool.com",
        date = Date(),
        delivered = delivered,
    )
}

private fun makeTestTextMessageItem(text: String, fromMe: Boolean, delivered: Boolean = true): MessageListItem {
    return MessageListItem.TextMessage(
        text = text,
        metadata = testMessageMetadata(fromMe = fromMe, delivered = delivered)
    )
}

private fun makeTestImageMessageItem(fromMe: Boolean, delivered: Boolean = true): MessageListItem {
    return MessageListItem.ImageAttachmentMessage(
        guid = "asdf",
        metadata = testMessageMetadata(fromMe, delivered)
    )
}

@Preview
@Composable
private fun MessageListScreenPreview() {
    val messages = listOf<MessageListItem>(
        makeTestImageMessageItem(false),

        makeTestTextMessageItem("Hello", false),
        makeTestTextMessageItem( "Hey there, this is a longer text message that might wrap to another line", true),
        makeTestTextMessageItem("How's it going", fromMe = true, delivered = false)
    ).reversed()

    Scaffold() {
        MessageTranscript(
            messages = messages,
            paddingValues = it,
            showSenders = true,
            attachmentUris = setOf(),
            onAddAttachment = {},
            onClearAttachments = {},
            onSendMessage = {}
        )
    }
}

@Preview(showBackground = true)
@Composable
private fun MessageEntryPreview() {
    var textState by rememberSaveable(stateSaver = TextFieldValue.Saver) {
        mutableStateOf(TextFieldValue("Hello this is some text that might wrap multiple lines to show that there must be some padding here. "))
    }

    MessageEntry(onSend = {}, onTextChanged = {}, textFieldValue = textState)
}

@Preview(showBackground = true)
@Composable
private fun MessageEntryWithAttachmentsPreview() {
    var textState by rememberSaveable(stateSaver = TextFieldValue.Saver) {
        mutableStateOf(TextFieldValue("Attachments"))
    }

    MessageEntry(onSend = {}, onTextChanged = {}, textFieldValue = textState, attachmentItems = listOf(
        AttachmentRowItem(painterResource(id = R.drawable.sedona), "id")
    ))
}

// - No content

@Preview
@Composable
fun NoContentPreview() {
    Scaffold {
        NoContentView(
            icon = R.drawable.storage,
            text = "Server not configured",
            onSettings = {},
            modifier = Modifier.padding(it)
        )
    }
}

// - Settings

@Preview
@Composable
fun SettingsPreview() {
    SettingsScreen()
}