package com.tomhempel.rbarometer.questionnaire

import android.content.Context
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerClientCoachCode
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerDateSpinner
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerFreeText
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerGlassScaleQuestion
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerLastPage
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerMultiCheckboxQuestion
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerRadioQuestion
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerSliderQuestion
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerStringSpinner
import com.tomhempel.rbarometer.questionnaire.handlers.HandlerValueSpinner

enum class NavigationMode { Step, Embedded }

data class HandlerDependencies(
    val context: Context,
    val answers: MutableMap<String, Any>,
    val languageID: String,
    val questionnaireMetaId: String,
    val goToNext: () -> Unit,
    val goToPrev: () -> Unit,
    val goToById: (String) -> Unit,
    val showToast: (String) -> Unit,
    val saveAnswers: suspend (Map<String, Any>) -> Unit = {},
    val onAnswerChanged: () -> Unit = {},
)

object QuestionHandlerFactory {

    fun create(question: QuestionItem, mode: NavigationMode, deps: HandlerDependencies): QuestionHandler? {
        val next = if (mode == NavigationMode.Embedded) ({}) else deps.goToNext
        val prev = if (mode == NavigationMode.Embedded) ({}) else deps.goToPrev
        val byId: (String) -> Unit = if (mode == NavigationMode.Embedded) ({ _ -> }) else deps.goToById

        return when (question) {
            is QuestionItem.RadioQuestion -> HandlerRadioQuestion(
                deps.context, deps.answers, deps.languageID,
                next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.ClientCoachCodeQuestion -> HandlerClientCoachCode(
                deps.answers, deps.languageID, next, prev, byId, deps.showToast,
            )
            is QuestionItem.DateSpinnerQuestion -> HandlerDateSpinner(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.ValueSpinnerQuestion -> HandlerValueSpinner(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.SliderQuestion -> HandlerSliderQuestion(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.onAnswerChanged,
            )
            is QuestionItem.GlassScaleQuestion -> HandlerGlassScaleQuestion(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
                onAnswerChanged = deps.onAnswerChanged,
            )
            is QuestionItem.StringSpinnerQuestion -> HandlerStringSpinner(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.MultiCheckboxQuestion -> HandlerMultiCheckboxQuestion(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.FreeTextQuestion -> HandlerFreeText(
                deps.context, deps.answers, deps.languageID, next, prev, byId, deps.showToast, deps.questionnaireMetaId,
            )
            is QuestionItem.LastPage -> HandlerLastPage(
                deps.answers, deps.languageID, next, prev, deps.saveAnswers,
            )
            else -> null
        }
    }
}
