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/views/onboarding/onboarding_viewmodel.dart';
import 'package:yimaru_app/ui/widgets/onboarding_app_bar.dart';

import '../../onboarding_view.form.dart';

class FirstAssessmentForm extends ViewModelWidget<OnboardingViewModel> {
  final TextEditingController answerController;

  const FirstAssessmentForm({super.key, required this.answerController});

  @override
  Widget build(BuildContext context, OnboardingViewModel viewModel) =>
      _buildScaffoldWrapper(viewModel);

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

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

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

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

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

  Widget _buildBody(OnboardingViewModel viewModel) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: _buildBodyChildren(viewModel),
      );

  List<Widget> _buildBodyChildren(OnboardingViewModel viewModel) =>
      [_buildUpperColumn(viewModel), _buildContinueButtonWrapper(viewModel)];

  Widget _buildUpperColumn(OnboardingViewModel viewModel) => Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _buildUpperColumnChildren(viewModel),
      );

  List<Widget> _buildUpperColumnChildren(OnboardingViewModel viewModel) => [
        verticalSpaceMedium,
        _buildTitle(),
        verticalSpaceLarge,
        _buildFirstAssessmentFormField(viewModel),
        if (viewModel.hasAnswerValidationMessage &&
            viewModel.focusFirstAssessment)
          verticalSpaceTiny,
        if (viewModel.hasAnswerValidationMessage &&
            viewModel.focusFirstAssessment)
          _buildFirstAssessmentValidatorWrapper(viewModel)
      ];

  Widget _buildAppBar() => const OnboardingAppBar(
        showBackButton: false,
        showLanguageSelection: false,
      );

  Widget _buildTitle() => const Text(
        '1. What is the plural of “book”?',
        style: TextStyle(
          fontSize: 16,
          color: kcDarkGreyColor,
          fontWeight: FontWeight.w600,
        ),
      );

  Widget _buildFirstAssessmentFormField(OnboardingViewModel viewModel) =>
      TextFormField(
        controller: answerController,
        onTap: viewModel.setFirstAssessmentFocus,
        decoration: inputDecoration(focus: viewModel.focusFirstAssessment),
      );

  Widget _buildFirstAssessmentValidatorWrapper(OnboardingViewModel viewModel) =>
      viewModel.hasAnswerValidationMessage
          ? _buildFirstAssessmentValidator(viewModel)
          : Container();

  Widget _buildFirstAssessmentValidator(OnboardingViewModel viewModel) => Text(
        viewModel.answerValidationMessage!,
        style: const TextStyle(
          fontSize: 12,
          color: Colors.red,
          fontWeight: FontWeight.w700,
        ),
      );

  Widget _buildContinueButtonWrapper(OnboardingViewModel viewModel) => Padding(
        padding: const EdgeInsets.only(bottom: 50),
        child: _buildContinueButton(viewModel),
      );

  Widget _buildContinueButton(OnboardingViewModel viewModel) =>
      CustomElevatedButton(
        height: 55,
        text: 'Continue',
        borderRadius: 12,
        foregroundColor: kcWhiteColor,
        backgroundColor:
            viewModel.focusFirstAssessment && answerController.text.isNotEmpty
                ? kcPrimaryColor
                : kcPrimaryColor.withOpacity(0.2),
        onTap:
            viewModel.focusFirstAssessment && answerController.text.isNotEmpty
                ? () => viewModel.next()
                : null,
      );
}
