package eu.darken.bluemusic.main.ui.onboarding

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import eu.darken.bluemusic.common.compose.PreviewWrapper
import eu.darken.bluemusic.common.error.ErrorEventHandler
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import eu.darken.bluemusic.main.ui.onboarding.OnboardingViewModel.State.Page
import eu.darken.bluemusic.main.ui.onboarding.pages.BetaPage
import eu.darken.bluemusic.main.ui.onboarding.pages.PrivacyPage
import eu.darken.bluemusic.main.ui.onboarding.pages.RewritePage
import eu.darken.bluemusic.main.ui.onboarding.pages.WelcomePage
import kotlinx.coroutines.launch


@Composable
fun OnboardingScreenHost(vm: OnboardingViewModel = hiltViewModel()) {
    ErrorEventHandler(vm)

    val state by vm.state.collectAsStateWithLifecycle()

    state?.let { state ->
        OnboardingScreen(
            state = state,
            onReadPrivacyPolicy = { vm.readPrivacyPolicy() },
            onFinishOnboarding = vm::completeOnboarding,
        )
    }
}


@Composable
private fun OnboardingScreen(
    state: OnboardingViewModel.State,
    onReadPrivacyPolicy: () -> Unit,
    onFinishOnboarding: () -> Unit,
) {

    val pagerState =
        rememberPagerState(
            initialPage = state.pages.indexOf(state.startPage).coerceAtLeast(0),
            pageCount = { state.pages.size }
        )
    val scope = rememberCoroutineScope()

    BackHandler(enabled = pagerState.currentPage > 0) {
        scope.launch { pagerState.animateScrollToPage(pagerState.currentPage - 1) }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .safeDrawingPadding()
            .padding(16.dp)
    ) {
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.weight(1f),
            userScrollEnabled = false
        ) { page ->
            when (state.pages.getOrNull(page)) {
                Page.WELCOME ->
                    WelcomePage(
                        onContinue = {
                            scope.launch {
                                pagerState.animateScrollToPage(pagerState.currentPage + 1)
                            }
                        }
                    )

                Page.REWRITE ->
                    RewritePage(
                        onContinue = {
                            scope.launch {
                                pagerState.animateScrollToPage(pagerState.currentPage + 1)
                            }
                        }
                    )

                Page.BETA ->
                    BetaPage(
                        onContinue = {
                            scope.launch {
                                pagerState.animateScrollToPage(pagerState.currentPage + 1)
                            }
                        }
                    )

                Page.PRIVACY ->
                    PrivacyPage(
                        onReadPrivacyPolicy = onReadPrivacyPolicy,
                        onAccept = { onFinishOnboarding() }
                    )

                null -> {} // Safety fallback
            }
        }
    }
}

@Preview
@Composable
private fun IntroScreenPreview() {
    PreviewWrapper {
        OnboardingScreen(
            state = OnboardingViewModel.State(),
            onReadPrivacyPolicy = {},
            onFinishOnboarding = {},
        )
    }
}
