<commit_before>package org.stepik.android.presentation.step_quiz_review.reducer

import org.stepik.android.domain.step_quiz.model.StepQuizRestrictions
import org.stepik.android.model.DiscountingPolicyType
import org.stepik.android.model.Submission
import org.stepik.android.model.attempts.Attempt
import org.stepik.android.presentation.step_quiz.StepQuizView
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.State
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.Action
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.Message
import ru.nobird.android.core.model.safeCast
import javax.inject.Inject

class StepQuizReviewReducer
@Inject
constructor() : StateReducer<State, Message, Action> {
    override fun reduce(
        state: State,
        message: Message
    ): Pair<State, Set<Action>> =
        when (message) {
            is Message.InitWithStep ->
                if (state is State.Idle ||
                    state is State.Error && message.forceUpdate) {
                    val sessionId = message.stepWrapper.step.session
                    val action =
                        if (sessionId != null && sessionId > 0) {
                            Action.FetchReviewSession(message.stepWrapper.id, message.lessonData.unit?.id, message.stepWrapper.step.instruction ?: -1, sessionId)
                        } else {
                            Action.FetchStepQuizState(message.stepWrapper, message.lessonData)
                        }

                    State.Loading(message.stepWrapper.step) to setOf(action)
                } else {
                    null
                }

            is Message.FetchReviewSessionSuccess ->
                when (state) {
                    is State.Loading -> {
                        val quizState = createAttemptLoadedState(message.sessionData.attempt, message.sessionData.submission)
                        val newState =
                            if (message.sessionData.session.isFinished) {
                                State.Completed(quizState, message.sessionData.session, message.instruction, message.progress)
                            } else {
                                State.SubmissionSelected(quizState, isReviewCreationInProgress = false, message.sessionData.session, message.instruction,  message.progress)
                            }

                        newState to emptySet()
                    }

                    else -> null
                }

            is Message.FetchStepQuizStateSuccess ->
                when (state) {
                    is State.Loading ->
                        State.SubmissionNotMade(
                            quizState = message.quizState,
                            progress = message.progress
                        ) to emptySet()
                    else -> null
                }

            is Message.InitialFetchError ->
                when (state) {
                    is State.Loading ->
                        State.Error to emptySet()
                    else -> null
                }

            is Message.SolveAgain ->
                when (state) {
                    is State.SubmissionNotSelected ->
                        State.SubmissionNotMade(state.quizState, state.progress) to emptySet()

                    else -> null
                }

            is Message.ChangeSubmission -><commit_msg>handle SubmissionNotSelected state init<commit_after>package org.stepik.android.presentation.step_quiz_review.reducer

import org.stepik.android.domain.step_quiz.model.StepQuizRestrictions
import org.stepik.android.model.DiscountingPolicyType
import org.stepik.android.model.Submission
import org.stepik.android.model.attempts.Attempt
import org.stepik.android.presentation.step_quiz.StepQuizView
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.State
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.Action
import org.stepik.android.presentation.step_quiz_review.StepQuizReviewView.Message
import ru.nobird.android.core.model.safeCast
import javax.inject.Inject

class StepQuizReviewReducer
@Inject
constructor() : StateReducer<State, Message, Action> {
    override fun reduce(
        state: State,
        message: Message
    ): Pair<State, Set<Action>> =
        when (message) {
            is Message.InitWithStep ->
                if (state is State.Idle ||
                    state is State.Error && message.forceUpdate) {
                    val sessionId = message.stepWrapper.step.session
                    val action =
                        if (sessionId != null && sessionId > 0) {
                            Action.FetchReviewSession(message.stepWrapper.id, message.lessonData.unit?.id, message.stepWrapper.step.instruction ?: -1, sessionId)
                        } else {
                            Action.FetchStepQuizState(message.stepWrapper, message.lessonData)
                        }

                    State.Loading(message.stepWrapper.step) to setOf(action)
                } else {
                    null
                }

            is Message.FetchReviewSessionSuccess ->
                when (state) {
                    is State.Loading -> {
                        val quizState = createAttemptLoadedState(message.sessionData.attempt, message.sessionData.submission)
                        val newState =
                            if (message.sessionData.session.isFinished) {
                                State.Completed(quizState, message.sessionData.session, message.instruction, message.progress)
                            } else {
                                State.SubmissionSelected(quizState, isReviewCreationInProgress = false, message.sessionData.session, message.instruction,  message.progress)
                            }

                        newState to emptySet()
                    }

                    else -> null
                }

            is Message.FetchStepQuizStateSuccess ->
                when (state) {
                    is State.Loading -> {
                        if (message.quizState is StepQuizView.State.AttemptLoaded &&
                            message.quizState.submissionState is StepQuizView.SubmissionState.Loaded &&
                            message.quizState.submissionState.submission.status == Submission.Status.CORRECT
                        ) {
                            State.SubmissionNotSelected(
                                quizState = message.quizState,
                                isSessionCreationInProgress = false,
                                progress = message.progress
                            )
                        } else {
                            State.SubmissionNotMade(
                                quizState = message.quizState,
                                progress = message.progress
                            )
                        } to emptySet()
                    }
                    else -> null
                }

            is Message.InitialFetchError ->
                when (state) {
                    is State.Loading ->
                        State.Error to emptySet()
                    else -> null
                }

            is Message.SolveAgain ->
                when (state) {
                    is State.SubmissionNotSelected ->
                        State.SubmissionNotMade(state.quizState, state.progress) to emptySet()

                    else -> null
                }

            is Message.ChangeSubmission ->
{'fraction_non_alphanumeric': 0.0729248494370254, 'fraction_numerical': 0.0005236973029588898, 'mean_word_length': 1.4044696254327982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2282611', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1719, 'n_words': 306}