package moe.lava.neon.ui.screens

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.touchlab.kermit.Logger
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesIntoMap
import dev.zacsweers.metro.Inject
import dev.zacsweers.metrox.viewmodel.ViewModelKey
import dev.zacsweers.metrox.viewmodel.metroViewModel
import kotlinx.coroutines.launch
import moe.lava.neon.core.api.gateway.GatewayHandler
import moe.lava.neon.core.repository.AuthRepository
import moe.lava.neon.resources.Res
import moe.lava.neon.resources.compose_multiplatform
import moe.lava.neon.ui.Greeting
import org.jetbrains.compose.resources.painterResource

@Composable
fun Sample(
    navTest: (Boolean) -> Unit,
    onRequestLogout: () -> Unit,
) {
    val viewModel: SampleViewModel = metroViewModel()
    var showContent by remember { mutableStateOf(false) }
    Column(
        modifier = Modifier
            .background(MaterialTheme.colorScheme.primaryContainer)
            .safeContentPadding()
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        Button(onClick = { showContent = !showContent }) {
            Text("Click me!")
        }
        Button(onClick = { navTest(true) }) {
            Text("Click me (left)!")
        }
        Button(onClick = { navTest(false) }) {
            Text("Click me (bottom!")
        }
        AnimatedVisibility(showContent) {
            val greeting = remember { Greeting().greet() }
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Image(painterResource(Res.drawable.compose_multiplatform), null)
                Text("Compose: $greeting")
                Text("Passed token: ${viewModel.token?.slice(0..10)}...")
            }
        }
        Button(onClick = {
            viewModel.connect()
        }) {
            Text("Connect!")
        }
        Button(onClick = {
            viewModel.disconnect()
        }) {
            Text("Disconnect!")
        }
        Button(onClick = {
            viewModel.logout()
            onRequestLogout()
        }) {
            Text("Logout!")
        }
    }
}

@Inject
@ViewModelKey(SampleViewModel::class)
@ContributesIntoMap(AppScope::class)
class SampleViewModel(
    private val auth: AuthRepository,
    private val gateway: GatewayHandler,
) : ViewModel() {
    private val logger = Logger.withTag("neon.ui.screens/Sample")
    val token get() = auth.token

    fun connect() {
        viewModelScope.launch {
            try {
                gateway.connect()
            } catch(e: Throwable) {
                logger.e(e) { "Failed to connect to gateway: ${e.stackTraceToString()}" }
            }
        }
    }
    fun disconnect() {
        viewModelScope.launch {
            try {
                gateway.disconnect()
            } catch(e: Throwable) {
                logger.e(e) { "Failed to connect to gateway: ${e.stackTraceToString()}" }
            }
        }
    }
    fun logout() {
        auth.logout()
    }
}
