package com.example.livingai.pages.onboarding

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
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.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
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 com.example.livingai.R
import com.example.livingai.pages.commons.Dimentions.OBJECT_SPACING
import com.example.livingai.pages.commons.Dimentions.SMALL_OBJECT_LEN
import com.example.livingai.pages.commons.Dimentions.SMALL_PADDING_BUTTON
import com.example.livingai.pages.components.FlexButton
import com.example.livingai.pages.components.FlexTextButton
import com.example.livingai.pages.components.OnBoardingPage
import com.example.livingai.pages.components.PageIndicator
import kotlinx.coroutines.launch

@Composable
fun OnBoardingScreen(
    events: (OnBoardingEvents) -> Unit
) {
    val pageLen = pages.size
    Column(modifier = Modifier.fillMaxSize()) {
        val pagerState = rememberPagerState(initialPage = 0) {
            pages.size
        }
        val buttonState = remember {
            derivedStateOf {
                when (pagerState.currentPage) {
                    0 -> listOf(R.string.english_language, R.string.hindi_language)
                    pageLen - 1 -> listOf(R.string.back_button, R.string.get_started_button)
                    else -> listOf(R.string.back_button, R.string.next_button)
                }
            }
        }
        HorizontalPager(state = pagerState) {
            OnBoardingPage(page = pages[it])
        }
        Spacer(modifier = Modifier.weight(1F))
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = OBJECT_SPACING)
            .navigationBarsPadding(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            PageIndicator(
                modifier = Modifier.width(SMALL_OBJECT_LEN * pageLen),
                pageSize = pageLen,
                selectedPage = pagerState.currentPage
            )

            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                val scope = rememberCoroutineScope()

                if (pagerState.currentPage != 0) {
                    FlexTextButton(
                        text = buttonState.value[0],
                        onClick = {
                            scope.launch {
                                pagerState.animateScrollToPage(page = pagerState.currentPage - 1)
                            }
                        }
                    )
                } else {
                    FlexButton(
                        text = buttonState.value[0],
                        onClick = {
                            events(OnBoardingEvents.SaveLanguage("en"))
                            scope.launch {
                                pagerState.animateScrollToPage(page = pagerState.currentPage + 1)
                            }
                        }
                    )
                    Spacer(modifier = Modifier.width(SMALL_PADDING_BUTTON))
                }

                FlexButton(
                    text = buttonState.value[1],
                    onClick = {
                        if (pagerState.currentPage == 0) {
                            events(OnBoardingEvents.SaveLanguage("hi"))
                        }

                        if (pagerState.currentPage + 1 >= pageLen) {
                            events(OnBoardingEvents.SaveAppEntry)
                        } else {
                            scope.launch {
                                pagerState.animateScrollToPage(page = pagerState.currentPage + 1)
                            }
                        }
                    }
                )
            }
        }
    }
}