package com.seven.ikar.kotlin.feature.bots

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.seven.ikar.kotlin.R
import com.seven.ikar.kotlin.core.model.BotSummaryDto
import com.seven.ikar.kotlin.ui.IkarCenteredHeader
import com.seven.ikar.kotlin.ui.IkarHeaderIconBubble
import com.seven.ikar.kotlin.ui.IkarScreenSurface
import com.seven.ikar.kotlin.ui.IkarSectionCard
import com.seven.ikar.kotlin.ui.theme.TelegramBlue
import com.seven.ikar.kotlin.ui.theme.TelegramInkMuted

@Composable
fun BotsScreen(
    state: BotsUiState,
    onBackClick: () -> Unit,
    onCreateBotClick: () -> Unit,
    onBotClick: (BotSummaryDto) -> Unit
) {
    IkarScreenSurface(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical))
        ) {
            Column(
                modifier = Modifier.padding(start = 18.dp, top = 18.dp, end = 18.dp, bottom = 14.dp),
                verticalArrangement = Arrangement.spacedBy(14.dp)
            ) {
                IkarCenteredHeader(
                    title = "Боты",
                    left = {
                        IkarHeaderIconBubble(onClick = onBackClick) {
                            Icon(Icons.AutoMirrored.Outlined.ArrowBack, contentDescription = "Назад", tint = MaterialTheme.colorScheme.onSurface)
                        }
                    },
                    right = {
                        IkarHeaderIconBubble {
                            Image(
                                painter = painterResource(id = R.drawable.dotnet_bot),
                                contentDescription = "API ботов",
                                modifier = Modifier.align(Alignment.Center)
                            )
                        }
                    }
                )

                Button(onClick = onCreateBotClick) {
                    Text("Создать бота")
                }

                state.errorMessage?.let {
                    Text(it, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodyMedium)
                }
            }

            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 18.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                if (state.bots.isEmpty()) {
                    item {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 24.dp, vertical = 48.dp),
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.spacedBy(8.dp)
                        ) {
                            Text("Ботов пока нет", style = MaterialTheme.typography.headlineMedium)
                            Text(
                                "Создайте первого бота и подключите его через Bot API.",
                                style = MaterialTheme.typography.bodyMedium,
                                color = TelegramInkMuted
                            )
                        }
                    }
                } else {
                    items(state.bots, key = { it.id }) { bot ->
                        IkarSectionCard(
                            modifier = Modifier
                                .fillMaxWidth()
                                .clickable { onBotClick(bot) }
                        ) {
                            Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                                Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
                                    Text(bot.displayName, style = MaterialTheme.typography.headlineMedium)
                                    Text("@${bot.username}", style = MaterialTheme.typography.bodySmall, color = TelegramBlue)
                                }
                                Text(bot.about.orEmpty().ifBlank { "Без описания" }, style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
                                Text(
                                    if (bot.commands.isEmpty()) "Команд пока нет" else "Команд: ${bot.commands.joinToString { it.command }}",
                                    style = MaterialTheme.typography.bodySmall,
                                    color = TelegramInkMuted
                                )
                                Text(
                                    if (bot.isEnabled) "Включён" else "Выключен",
                                    style = MaterialTheme.typography.labelLarge,
                                    color = TelegramBlue
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}
