package com.vitorpamplona.amethyst.ui.note

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.actions.CloseButton
import com.vitorpamplona.amethyst.ui.actions.PostButton
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class AddBountyAmountViewModel : ViewModel() {
    private var account: Account? = null
    private var bounty: Note? = null

    var nextAmount by mutableStateOf(TextFieldValue(""))

    fun load(account: Account, bounty: Note?) {
        this.account = account
        this.bounty = bounty
    }

    fun sendPost() {
        val newValue = nextAmount.text.trim().toLongOrNull()

        if (newValue != null) {
            account?.sendPost(
                message = newValue.toString(),
                replyTo = listOfNotNull(bounty),
                mentions = listOfNotNull(bounty?.author),
                tags = listOf("bounty-added-reward"),
                wantsToMarkAsSensitive = false,
                replyingTo = null,
                root = null,
                directMentions = setOf()
            )

            nextAmount = TextFieldValue("")
        }
    }

    fun cancel() {
        nextAmount = TextFieldValue("")
    }

    fun hasChanged(): Boolean {
        return nextAmount.text.trim().toLongOrNull() != null
    }
}

@Composable
fun AddBountyAmountDialog(bounty: Note, accountViewModel: AccountViewModel, onClose: () -> Unit) {
    val postViewModel: AddBountyAmountViewModel = viewModel()
    postViewModel.load(accountViewModel.account, bounty)
    val scope = rememberCoroutineScope()

    Dialog(
        onDismissRequest = { onClose() },
        properties = DialogProperties(
            dismissOnClickOutside = false,
            usePlatformDefaultWidth = false
        )
    ) {
        Surface() {
            Column(
                modifier = Modifier
                    .padding(10.dp)
                    .width(IntrinsicSize.Min)
            ) {
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    CloseButton(onPress = {
                        postViewModel.cancel()
                        onClose()
                    })

                    PostButton(
                        onPost = {
                            scope.launch(Dispatchers.IO) {
                                postViewModel.sendPost()
                                onClose()
                            }
                        },
                        isActive = postViewModel.hasChanged()
                    )
                }

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

                Row(
                    modifier = Modifier.padding(vertical = 5.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    OutlinedTextField(
                        label = { Text(text = stringResource(R.string.pledge_amount_in_sats)) },
                        value = postViewModel.nextAmount,
                        onValueChange = {
                            postViewModel.nextAmount = it
                        },
                        keyboardOptions = KeyboardOptions.Default.copy(
                            capitalization = KeyboardCapitalization.None,
                            keyboardType = KeyboardType.Number
                        ),
                        placeholder = {
                            Text(
                                text = "10000, 50000, 5000000",
                                color = MaterialTheme.colorScheme.placeholderText
                            )
                        },
                        singleLine = true
                    )
                }
            }
        }
    }
}
