import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:yimaru_app/ui/common/app_colors.dart';
import 'package:yimaru_app/ui/common/ui_helpers.dart';
import 'package:yimaru_app/ui/widgets/custom_elevated_button.dart';
import 'package:yimaru_app/ui/widgets/custom_small_radio_button.dart';
import 'package:yimaru_app/ui/widgets/large_app_bar.dart';

import '../assessment_viewmodel.dart';
import 'assessment_loading_screen.dart';

class AssessmentQuestionsScreen extends ViewModelWidget<AssessmentViewModel> {
  const AssessmentQuestionsScreen({super.key});

  @override
  Widget build(BuildContext context, AssessmentViewModel viewModel) =>
      _buildAssessmentScreens(viewModel);

  Widget _buildAssessmentScreens(AssessmentViewModel viewModel) =>
      viewModel.isBusy || viewModel.assessments.isEmpty
          ? _buildPageLoadingIndicator(viewModel)
          : _buildAssessmentScreensWrapper(viewModel);

  Widget _buildPageLoadingIndicator(AssessmentViewModel viewModel) =>
      AssessmentLoadingScreen(
        isLoading: viewModel.isBusy,
        isEmpty: viewModel.assessments.isEmpty,
        onTap: () async => await viewModel.getAssessments(),
        onPop: viewModel.assessments.isEmpty ? viewModel.pop : null,
      );

  Widget _buildAssessmentScreensWrapper(AssessmentViewModel viewModel) =>
      PopScope(
          canPop: false,
          onPopInvokedWithResult: (value, data) => viewModel.previousQuestion(),
          child: _buildScaffoldWrapper(viewModel));

  Widget _buildScaffoldWrapper(AssessmentViewModel viewModel) => Scaffold(
        backgroundColor: kcBackgroundColor,
        body: _buildScaffold(viewModel),
      );

  Widget _buildScaffold(AssessmentViewModel viewModel) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _buildScaffoldChildren(viewModel),
      );

  List<Widget> _buildScaffoldChildren(AssessmentViewModel viewModel) =>
      [_buildAppBar(viewModel), _buildExpandedBody(viewModel)];

  Widget _buildAppBar(AssessmentViewModel viewModel) => LargeAppBar(
        onClose: viewModel.abort,
        showLanguageSelection: false,
        onPop: viewModel.previousQuestion,
        showBackButton: viewModel.currentQuestion == 0 ? false : true,
      );

  Widget _buildExpandedBody(AssessmentViewModel viewModel) =>
      Expanded(child: _buildBodyWrapper(viewModel));

  Widget _buildBodyWrapper(AssessmentViewModel viewModel) => Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: _buildAssessment(viewModel),
      );

  Widget _buildAssessment(AssessmentViewModel viewModel) => PageView.builder(
        controller: viewModel.pageController,
        itemCount: viewModel.assessments.length,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cotext, index) =>
            _buildBodyScroller(index: index, viewModel: viewModel),
      );

  Widget _buildBodyScroller(
          {required int index, required AssessmentViewModel viewModel}) =>
      SingleChildScrollView(
        child: _buildBody(index: index, viewModel: viewModel),
      );

  Widget _buildBody(
          {required int index, required AssessmentViewModel viewModel}) =>
      Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _buildBodyChildren(viewModel: viewModel, index: index),
      );

  List<Widget> _buildBodyChildren(
          {required int index, required AssessmentViewModel viewModel}) =>
      [
        verticalSpaceMedium,
        _buildTitle(index: index, viewModel: viewModel),
        verticalSpaceMedium,
        _buildAnswers(index: index, viewModel: viewModel),
        _buildContinueButtonWrapper(viewModel: viewModel, question: index + 1)
      ];

  Widget _buildTitle(
          {required int index, required AssessmentViewModel viewModel}) =>
      Text(
        'Q${index + 1}. ${viewModel.assessments[index].questionText} ',
        style: style16DG600,
      );

  Widget _buildAnswers(
          {required int index, required AssessmentViewModel viewModel}) =>
      ListView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: viewModel.assessments[index].options?.length,
        itemBuilder: (context, inner) => _buildAnswer(
          onTap: () => viewModel.setSelectedAnswer(
              question: index + 1,
              option: viewModel.assessments[index].options?[inner]),
          title: viewModel.assessments[index].options?[inner].optionText ?? '',
          selected: viewModel.isSelectedAnswer(
              question: index + 1,
              answer: viewModel.assessments[index].options?[inner].optionText ??
                  ''),
        ),
      );

  Widget _buildAnswer(
          {required String title,
          required bool selected,
          required GestureTapCallback onTap}) =>
      CustomSmallRadioButton(
        title: title,
        onTap: onTap,
        selected: selected,
      );

  Widget _buildContinueButtonWrapper(
          {required int question, required AssessmentViewModel viewModel}) =>
      Padding(
        padding: const EdgeInsets.only(bottom: 50),
        child: _buildContinueButton(viewModel: viewModel, question: question),
      );

  Widget _buildContinueButton(
          {required int question, required AssessmentViewModel viewModel}) =>
      CustomElevatedButton(
        height: 55,
        borderRadius: 12,
        foregroundColor: kcWhite,
        backgroundColor:
            viewModel.selectedAnswers.containsKey(question.toString())
                ? kcPrimaryColor
                : kcPrimaryColor.withOpacity(0.1),
        onTap: viewModel.selectedAnswers.containsKey(question.toString())
            ? () => viewModel.nextQuestion()
            : null,
        text: viewModel.currentQuestion == viewModel.assessments.length - 1
            ? 'Finish'
            : 'Continue',
      );
}
