package com.example.wabi.view.screens.onboard.screeen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
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.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.wabi.domain.navigation.Routes
import com.example.wabi.domain.repository.UserData
import com.example.wabi.model.appModels.screens.OnBoardItem
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.common_elements.button.ButtonLink
import com.example.wabi.view.common_elements.button.MainButton
import com.example.wabi.view.screens.onboard.items.OnBoardIndicator
import com.example.wabi.view.screens.onboard.items.OnBoardState
import com.example.wabi.view.screens.onboard.viewmodel.OnBoardViewModel
import kotlinx.coroutines.launch

//экран отображения onbard
@Composable
fun Onboard(
    navController: NavHostController, vm: OnBoardViewModel = hiltViewModel()
) {
    val pages = listOf(
        OnBoardItem.First, OnBoardItem.Second, OnBoardItem.Third
    )

   // UserData.currentScreen = Routes.ONBOARD

    val pagerState = rememberPagerState(initialPage = 0) { pages.size }

    val noEnd = remember {
        derivedStateOf {
            when (pagerState.currentPage) {
                0 -> listOf("Далее", "Пропустить")
                1 -> listOf("Далее", "Пропустить")
                2 -> listOf("Завершить", "")
                else -> listOf("", "")
            }
        }
    }

    val scope = rememberCoroutineScope()

    Column(
        modifier = Modifier
            .verticalScroll(rememberScrollState())
            .background(color = WabiTheme.colors.onBoardDark)
            .fillMaxSize(),
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        HorizontalPager(
            state = pagerState, modifier = Modifier
        ) { index ->
            OnBoardState(onboardItem = pages[index], modifier = Modifier.fillMaxHeight())
        }

        Spacer(modifier = Modifier.height(40.dp))
        OnBoardIndicator(
            pageSize = 3, currentPage = pagerState.currentPage
        )
        Spacer(modifier = Modifier.height(40.dp))
        MainButton(
            onClick = {
                scope.launch {
                    if (pagerState.currentPage < pages.size - 1) {
                        pagerState.animateScrollToPage(pagerState.currentPage + 1)
                    } else {
                        vm.Next(navController)
                    }
                }
            },
            color = WabiTheme.colors.onBoardLight,
            textContent = noEnd.value[0],
            colorContent = WabiTheme.colors.onBoardDark,
            modifierButton = Modifier
                .padding(30.dp, 0.dp)
                .fillMaxWidth()
        )
        Spacer(modifier = Modifier.height(15.dp))
        ButtonLink(
            onClick = {
                vm.Next(navController)
            },
            colorContent = WabiTheme.colors.onBoardLight,
            textContent = noEnd.value[1],
            enabled = noEnd.value[1].isNotEmpty()
        )
        Spacer(modifier = Modifier.height(100.dp))
    }
}