package com.vitorpamplona.amethyst.ui.screen.loggedIn

import android.widget.Toast
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Cancel
import androidx.compose.material.icons.filled.EditNote
import androidx.compose.material.icons.filled.Share
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextDirection
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.NostrChannelDataSource
import com.vitorpamplona.amethyst.ui.actions.NewChannelView
import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel
import com.vitorpamplona.amethyst.ui.actions.PostButton
import com.vitorpamplona.amethyst.ui.actions.UploadFromGallery
import com.vitorpamplona.amethyst.ui.components.ResizeImage
import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy
import com.vitorpamplona.amethyst.ui.dal.ChannelFeedFilter
import com.vitorpamplona.amethyst.ui.navigation.Route
import com.vitorpamplona.amethyst.ui.note.ChatroomMessageCompose
import com.vitorpamplona.amethyst.ui.screen.ChatroomFeedView
import com.vitorpamplona.amethyst.ui.screen.NostrChannelFeedViewModel

@Composable
fun ChannelScreen(
    channelId: String?,
    accountViewModel: AccountViewModel,
    navController: NavController
) {
    val accountState by accountViewModel.accountLiveData.observeAsState()
    val account = accountState?.account
    val context = LocalContext.current
    val channelScreenModel: NewPostViewModel = viewModel()

    if (account != null && channelId != null) {
        val replyTo = remember { mutableStateOf<Note?>(null) }

        ChannelFeedFilter.loadMessagesBetween(account, channelId)
        NostrChannelDataSource.loadMessagesBetween(account, channelId)

        val channelState by NostrChannelDataSource.channel!!.live.observeAsState()
        val channel = channelState?.channel ?: return

        val feedViewModel: NostrChannelFeedViewModel = viewModel()
        val lifeCycleOwner = LocalLifecycleOwner.current

        LaunchedEffect(Unit) {
            ChannelFeedFilter.loadMessagesBetween(account, channelId)
            NostrChannelDataSource.loadMessagesBetween(account, channelId)

            feedViewModel.invalidateData()
            channelScreenModel.imageUploadingError.collect { error ->
                Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
            }
        }

        DisposableEffect(channelId) {
            val observer = LifecycleEventObserver { _, event ->
                if (event == Lifecycle.Event.ON_RESUME) {
                    println("Channel Start")
                    ChannelFeedFilter.loadMessagesBetween(account, channelId)
                    NostrChannelDataSource.loadMessagesBetween(account, channelId)

                    feedViewModel.invalidateData()
                }
                if (event == Lifecycle.Event.ON_PAUSE) {
                    println("Channel Stop")

                    NostrChannelDataSource.clear()
                    NostrChannelDataSource.stop()
                }
            }

            lifeCycleOwner.lifecycle.addObserver(observer)
            onDispose {
                lifeCycleOwner.lifecycle.removeObserver(observer)
            }
        }

        Column(Modifier.fillMaxHeight()) {
            ChannelHeader(
                channel,
                account,
                navController = navController
            )

            Column(
                modifier = Modifier
                    .fillMaxHeight()
                    .padding(vertical = 0.dp)
                    .weight(1f, true)
            ) {
                ChatroomFeedView(feedViewModel, accountViewModel, navController, "Channel/$channelId") {
                    replyTo.value = it
                }
            }

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

            Row(Modifier.padding(horizontal = 10.dp).animateContentSize(), verticalAlignment = Alignment.CenterVertically) {
                val replyingNote = replyTo.value
                if (replyingNote != null) {
                    Column(Modifier.weight(1f)) {
                        ChatroomMessageCompose(
                            baseNote = replyingNote,
                            null,
                            innerQuote = true,
                            accountViewModel = accountViewModel,
                            navController = navController,
                            onWantsToReply = {
                                replyTo.value = it
                            }
                        )
                    }

                    Column(Modifier.padding(end = 10.dp)) {
                        IconButton(
                            modifier = Modifier.size(30.dp),
                            onClick = { replyTo.value = null }
                        ) {
                            Icon(
                                imageVector = Icons.Default.Cancel,
                                null,
                                modifier = Modifier.padding(end = 5.dp).size(30.dp),
                                tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
                            )
                        }
                    }
                }
            }

            // LAST ROW
            Row(
                modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp, top = 5.dp).fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                TextField(
                    value = channelScreenModel.message,
                    onValueChange = {
                        channelScreenModel.updateMessage(it)
                    },
                    keyboardOptions = KeyboardOptions.Default.copy(
                        capitalization = KeyboardCapitalization.Sentences
                    ),
                    shape = RoundedCornerShape(25.dp),
                    modifier = Modifier.weight(1f, true),
                    placeholder = {
                        Text(
                            text = stringResource(R.string.reply_here),
                            color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
                        )
                    },
                    textStyle = LocalTextStyle.current.copy(textDirection = TextDirection.Content),
                    trailingIcon = {
                        PostButton(
                            onPost = {
                                account.sendChannelMessage(channelScreenModel.message.text, channel.idHex, replyTo.value, null)
                                channelScreenModel.message = TextFieldValue("")
                                replyTo.value = null
                                feedViewModel.refresh() // Don't wait a full second before updating
                            },
                            isActive = channelScreenModel.message.text.isNotBlank() && !channelScreenModel.isUploadingImage,
                            modifier = Modifier.padding(end = 10.dp)
                        )
                    },
                    leadingIcon = {
                        UploadFromGallery(
                            isUploading = channelScreenModel.isUploadingImage,
                            tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
                            modifier = Modifier.padding(start = 5.dp)
                        ) {
                            channelScreenModel.upload(it, context)
                        }
                    },
                    colors = TextFieldDefaults.textFieldColors(
                        focusedIndicatorColor = Color.Transparent,
                        unfocusedIndicatorColor = Color.Transparent
                    )
                )
            }
        }
    }
}

@Composable
fun ChannelHeader(baseChannel: Channel, account: Account, navController: NavController) {
    val channelState by baseChannel.live.observeAsState()
    val channel = channelState?.channel ?: return

    val context = LocalContext.current.applicationContext

    Column() {
        Column(modifier = Modifier.padding(12.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                RobohashAsyncImageProxy(
                    robot = channel.idHex,
                    model = ResizeImage(channel.profilePicture(), 35.dp),
                    contentDescription = context.getString(R.string.profile_image),
                    modifier = Modifier
                        .width(35.dp)
                        .height(35.dp)
                        .clip(shape = CircleShape)
                )

                Column(
                    modifier = Modifier
                        .padding(start = 10.dp)
                        .weight(1f)
                ) {
                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text(
                            "${channel.info.name}",
                            fontWeight = FontWeight.Bold
                        )
                    }

                    Row(verticalAlignment = Alignment.CenterVertically) {
                        Text(
                            "${channel.info.about}",
                            color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
                            maxLines = 2,
                            overflow = TextOverflow.Ellipsis,
                            fontSize = 12.sp
                        )
                    }
                }

                Row(
                    modifier = Modifier
                        .height(35.dp)
                        .padding(bottom = 3.dp)
                ) {
                    NoteCopyButton(channel)

                    if (channel.creator == account.userProfile()) {
                        EditButton(account, channel)
                    }

                    if (account.followingChannels.contains(channel.idHex)) {
                        LeaveButton(account, channel, navController)
                    } else {
                        JoinButton(account, channel, navController)
                    }
                }
            }
        }

        Divider(
            modifier = Modifier.padding(start = 12.dp, end = 12.dp),
            thickness = 0.25.dp
        )
    }
}

@Composable
private fun NoteCopyButton(
    note: Channel
) {
    val clipboardManager = LocalClipboardManager.current
    var popupExpanded by remember { mutableStateOf(false) }

    Button(
        modifier = Modifier
            .padding(horizontal = 3.dp)
            .width(50.dp),
        onClick = { popupExpanded = true },
        shape = RoundedCornerShape(20.dp),
        colors = ButtonDefaults
            .buttonColors(
                backgroundColor = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
            )
    ) {
        Icon(
            tint = Color.White,
            imageVector = Icons.Default.Share,
            contentDescription = stringResource(R.string.copies_the_note_id_to_the_clipboard_for_sharing)
        )

        DropdownMenu(
            expanded = popupExpanded,
            onDismissRequest = { popupExpanded = false }
        ) {
            DropdownMenuItem(onClick = { clipboardManager.setText(AnnotatedString(note.idNote())); popupExpanded = false }) {
                Text(stringResource(R.string.copy_channel_id_note_to_the_clipboard))
            }
        }
    }
}

@Composable
private fun EditButton(account: Account, channel: Channel) {
    var wantsToPost by remember {
        mutableStateOf(false)
    }

    if (wantsToPost) {
        NewChannelView({ wantsToPost = false }, account = account, channel)
    }

    Button(
        modifier = Modifier
            .padding(horizontal = 3.dp)
            .width(50.dp),
        onClick = { wantsToPost = true },
        shape = RoundedCornerShape(20.dp),
        colors = ButtonDefaults
            .buttonColors(
                backgroundColor = MaterialTheme.colors.primary
            )
    ) {
        Icon(
            tint = Color.White,
            imageVector = Icons.Default.EditNote,
            contentDescription = stringResource(R.string.edits_the_channel_metadata)
        )
    }
}

@Composable
private fun JoinButton(account: Account, channel: Channel, navController: NavController) {
    Button(
        modifier = Modifier.padding(horizontal = 3.dp),
        onClick = {
            account.joinChannel(channel.idHex)
            navController.navigate(Route.Message.route)
        },
        shape = RoundedCornerShape(20.dp),
        colors = ButtonDefaults
            .buttonColors(
                backgroundColor = MaterialTheme.colors.primary
            ),
        contentPadding = PaddingValues(vertical = 6.dp, horizontal = 16.dp)
    ) {
        Text(text = stringResource(R.string.join), color = Color.White)
    }
}

@Composable
private fun LeaveButton(account: Account, channel: Channel, navController: NavController) {
    Button(
        modifier = Modifier.padding(horizontal = 3.dp),
        onClick = {
            account.leaveChannel(channel.idHex)
            navController.navigate(Route.Message.route)
        },
        shape = RoundedCornerShape(20.dp),
        colors = ButtonDefaults
            .buttonColors(
                backgroundColor = MaterialTheme.colors.primary
            ),
        contentPadding = PaddingValues(vertical = 6.dp, horizontal = 16.dp)
    ) {
        Text(text = stringResource(R.string.leave), color = Color.White)
    }
}
