import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked/stacked_annotations.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/assessment_completion.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/assessment_failure.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/assessment_intro.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/assessment_result.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/first_assessment_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/fourth_assessment_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/result_analysis.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/retake_assessment.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/second_assessment_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/start_lesson.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/assessment/third_assessment_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/age_group_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/challenge_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/country_region_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/educational_background_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/full_name_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/learning_goal_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/learning_reason_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/occupation_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/forms/topic_form.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/language_selector.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/welcome/first_welcome.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/welcome/second_welcome.dart';
import 'package:yimaru_app/ui/views/onboarding/screens/welcome/third_welcome.dart';

import '../../common/validators/onboarding_form_validator.dart';
import 'onboarding_viewmodel.dart';
import 'onboarding_view.form.dart';

@FormView(fields: [
  FormTextField(
      name: 'answer', validator: OnboardingFormValidator.validateForm),
  FormTextField(
      name: 'fullName', validator: OnboardingFormValidator.validateForm),
  FormTextField(
      name: 'challenge', validator: OnboardingFormValidator.validateForm),
  FormTextField(
      name: 'occupation', validator: OnboardingFormValidator.validateForm),
  FormTextField(
      name: 'learningReason', validator: OnboardingFormValidator.validateForm),
  FormTextField(name: 'topic', validator: OnboardingFormValidator.validateForm),
])
class OnboardingView extends StackedView<OnboardingViewModel>
    with $OnboardingView {
  const OnboardingView({Key? key}) : super(key: key);

  @override
  Widget builder(
    BuildContext context,
    OnboardingViewModel viewModel,
    Widget? child,
  ) =>
      _buildOnboardingScreensWrapper(viewModel);

  Widget _buildOnboardingScreensWrapper(OnboardingViewModel viewModel) =>
      PopScope(
          canPop: false,
          onPopInvokedWithResult: (value, data) => viewModel.pop(
              language: viewModel.currentPage == 23 ? true : false),
          child: _buildOnboardingScreens(viewModel));

  Widget _buildOnboardingScreens(OnboardingViewModel viewModel) => IndexedStack(
        index: viewModel.currentPage,
        children: _buildScreens(),
      );

  List<Widget> _buildScreens() => [
        _buildFirstWelcome(),
        _buildSecondWelcome(),
        _buildThirdWelcome(),
        _buildFullNameForm(),
        _buildEducationalBackgroundForm(),
        _buildAgeGroupForm(),
        _buildOccupationForm(),
        _buildCountryRegionForm(),
        _buildLearningGoalForm(),
        _buildLearningReasonForm(),
        _buildChallengeForm(),
        _buildTopicForm(),
        _buildAssessmentIntro(),
        _buildFirstAssessmentForm(),
        _buildSecondAssessment(),
        _buildThirdAssessment(),
        _buildFourthAssessment(),
        _buildAssessmentFailure(),
        _buildRetakeAssessment(),
        _buildResultAnalysis(),
        _buildAssessmentCompletion(),
        _buildAssessmentResult(),
        _buildStartLesson(),
        _buildLanguageSelector()
      ];

  Widget _buildFirstWelcome() => const FirstWelcome();

  Widget _buildSecondWelcome() => const SecondWelcome();

  Widget _buildThirdWelcome() => const ThirdWelcome();

  Widget _buildFullNameForm() =>
      FullNameForm(fullNameController: fullNameController);

  Widget _buildEducationalBackgroundForm() => const EducationalBackgroundForm();

  Widget _buildAgeGroupForm() => const AgeGroupForm();

  Widget _buildOccupationForm() =>
      OccupationForm(occupationController: occupationController);

  Widget _buildCountryRegionForm() => const CountryRegionForm();

  Widget _buildLearningGoalForm() => const LearningGoalForm();

  Widget _buildLearningReasonForm() =>
      LearningReasonForm(learningReasonController: learningReasonController);

  Widget _buildChallengeForm() =>
      ChallengeForm(challengeController: challengeController);

  Widget _buildTopicForm() => TopicForm(topicController: topicController);

  Widget _buildAssessmentIntro() => const AssessmentIntro();

  Widget _buildFirstAssessmentForm() =>
      FirstAssessmentForm(answerController: answerController);

  Widget _buildSecondAssessment() => const SecondAssessmentForm();

  Widget _buildThirdAssessment() => const ThirdAssessmentForm();

  Widget _buildFourthAssessment() => const FourthAssessmentForm();

  Widget _buildAssessmentFailure() => const AssessmentFailure();

  Widget _buildRetakeAssessment() => const RetakeAssessment();

  Widget _buildResultAnalysis() => const ResultAnalysis();

  Widget _buildAssessmentCompletion() => const AssessmentCompletion();

  Widget _buildAssessmentResult() => const AssessmentResult();

  Widget _buildStartLesson() => const StartLesson();

  Widget _buildLanguageSelector() => const LanguageSelector();

  @override
  void onViewModelReady(OnboardingViewModel viewModel) {
    syncFormWithViewModel(viewModel);
    super.onViewModelReady(viewModel);
  }

  @override
  OnboardingViewModel viewModelBuilder(
    BuildContext context,
  ) =>
      OnboardingViewModel();
}
