package eu.gaudian.translator.view

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
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.statusBarsPadding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
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.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.PrimaryButton
import kotlinx.coroutines.launch


@Composable
fun IntroNavHost(onIntroFinished: () -> Unit) {
    val pages = listOf(
        IntroPageData(
            title = stringResource(R.string.intro_title_welcome),
            description = stringResource(R.string.intro_desc_welcome),
            content = { IconContent(iconRes = R.drawable.ic_intro_welcome) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_ai_assistant),
            description = stringResource(R.string.intro_desc_ai_assistant),
            content = {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(4.dp)
                ) {
                    IconContent(iconRes = R.drawable.ic_intro_ai_agents)
                    FlowRow(
                        horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.CenterHorizontally),
                        verticalArrangement = Arrangement.spacedBy(2.dp),
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_mistral)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_your_own_ai)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_openai)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_claude)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_gemini)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_deepseek)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_openrouter)) })
                        SuggestionChip(onClick = { }, label = { Text(stringResource(R.string.text_and_many_more)) })
                    }
                }
            }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_dictionary_translator),
            description = stringResource(R.string.intro_desc_dictionary_translator),
            content = { IconContent(iconRes = R.drawable.ic_intro_lookup) }

        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_flashcards),
            description = stringResource(R.string.intro_desc_flashcards),
            content = { FlashcardTopicsPreview() }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_practice),
            description = stringResource(R.string.intro_desc_practice),
            content = { IconContent(iconRes = R.drawable.ic_inro_practice) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_learning_journey),
            description = stringResource(R.string.intro_desc_learning_journey),
            content = { IconContent(iconRes = R.drawable.ic_intro_learning_journey) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_categories),
            description = stringResource(R.string.intro_desc_categories),
            content = { IconContent(iconRes = R.drawable.ic_intro_categories) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_progress),
            description = stringResource(R.string.intro_desc_progress),
            content = { IconContent(iconRes = R.drawable.ic_intro_track_progress) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_need_help),
            description = stringResource(R.string.intro_if_you_need_help_you),
            content = { IconContent(iconRes = R.drawable.ic_intro_help) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_beta),
            description = stringResource(R.string.intro_desc_beta),
            content = { IconContent(iconRes = R.drawable.ic_icon_construction) }
        ),
        IntroPageData(
            title = stringResource(R.string.intro_title_all_set),
            description = stringResource(R.string.intro_desc_all_set),
            content = { IconContent(iconRes = R.drawable.ic_intro_robot) }
        )
    )

    val pagerState = rememberPagerState(pageCount = { pages.size })
    val scope = rememberCoroutineScope()

    Scaffold(topBar = {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .statusBarsPadding()
                .padding(horizontal = 16.dp, vertical = 8.dp)
        ) {
            eu.gaudian.translator.view.composable.SecondaryButton(
                onClick = { onIntroFinished() },
                text = stringResource(R.string.intro_skip),
                modifier = Modifier.fillMaxWidth()
            )
        }
    }) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            HorizontalPager(
                state = pagerState,
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
            ) { pageIndex ->
                IntroPage(pageData = pages[pageIndex])
            }

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

            PagerIndicator(
                pageCount = pages.size,
                currentPage = pagerState.currentPage
            )

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

            PrimaryButton(
                onClick = {
                    if (pagerState.currentPage < pages.size - 1) {
                        scope.launch {
                            pagerState.animateScrollToPage(pagerState.currentPage + 1)
                        }
                    } else {
                        onIntroFinished()
                    }
                },
                text = if (pagerState.currentPage < pages.size - 1) stringResource(R.string.next) else stringResource(R.string.get_started),
                icon = if (pagerState.currentPage < pages.size - 1) AppIcons.ArrowForwardNoChevron else null,
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}

private data class IntroPageData(
    val title: String,
    val description: String,
    val content: @Composable () -> Unit
)

@Composable
private fun IntroPage(pageData: IntroPageData) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.CenterVertically),
        modifier = Modifier
            .fillMaxSize() // Fixed: This was previously fillMaxHeight()
            .padding(horizontal = 16.dp)
            .verticalScroll(rememberScrollState())
    ) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(
                text = pageData.title,
                style = MaterialTheme.typography.headlineLarge,
                color = MaterialTheme.colorScheme.primary,
                textAlign = TextAlign.Center
            )
            Spacer(modifier = Modifier.height(16.dp))
            Text(
                text = pageData.description,
                style = MaterialTheme.typography.bodyLarge,
                textAlign = TextAlign.Center
            )
        }
        Box(modifier = Modifier.animateContentSize()) {
            pageData.content()
        }
    }
}

@Composable
private fun PagerIndicator(pageCount: Int, currentPage: Int) {
    Row(
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        repeat(pageCount) { iteration ->
            val color = if (currentPage == iteration) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant
            Box(
                modifier = Modifier
                    .size(12.dp)
                    .clip(CircleShape)
                    .background(color)
            )
        }
    }
}

@Composable
private fun IconContent(iconRes: Int) {
    Box(modifier = Modifier.clip(RoundedCornerShape(16.dp))) {
        Icon(
            painter = painterResource(id = iconRes),
            contentDescription = null,
            tint = Color.Unspecified,
            modifier = Modifier.size(250.dp)
        )
    }
}

@Composable
private fun FlashcardTopicsPreview() {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        Icon(
            painter = painterResource(id = R.drawable.ic_intro_flashcards),
            contentDescription = null,
            tint = Color.Unspecified,
            modifier = Modifier.size(250.dp)
        )

        Row(
            horizontalArrangement = Arrangement.spacedBy(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            SuggestionChip(onClick = {}, label = { Text(stringResource(R.string.intro_topic_history)) })
            SuggestionChip(onClick = {}, label = { Text(stringResource(R.string.intro_topic_science)) })
            SuggestionChip(onClick = {}, label = { Text(stringResource(R.string.intro_topic_cooking)) })
        }
    }
}