import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/data/enums/state_status.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/profile/wages_forms/domain/bloc/wages_form_bloc.dart';
import 'package:krow/features/profile/wages_forms/domain/entities/offer_letter_entity.dart';
import 'package:krow/features/profile/wages_forms/presentation/mixins/form_updater_mixin.dart';
import 'package:krow/features/profile/wages_forms/presentation/widgets/form_sign_holder_widget.dart';

@RoutePage()
class OfferLetterFormScreen extends StatefulWidget {
  const OfferLetterFormScreen({super.key});

  @override
  State<OfferLetterFormScreen> createState() => _OfferLetterFormScreenState();
}

class _OfferLetterFormScreenState extends State<OfferLetterFormScreen>
    with FormUpdaterMixin<OfferLetterFormScreen, OfferLetterEntity> {
  @override
  void initState() {
    super.initState();

    final activeForm = context.read<WagesFormBloc>().state.activeForm;
    form = activeForm is OfferLetterEntity
        ? activeForm
        : const OfferLetterEntity.empty();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        titleText: 'Offer Letter & Agreement',
        showNotification: true,
      ),
      body: BlocConsumer<WagesFormBloc, WagesFormState>(
        listenWhen: (previous, current) =>
            current.status == StateStatus.success,
        buildWhen: (previous, current) => current.status == StateStatus.error,
        listener: (context, state) {
          context.pushRoute(
            const SignedFormRoute(),
          );
          updateCurrentForm(form.copyWith(isSigned: true));
        },
        builder: (context, state) {
          final formErrors = state.formErrors;

          return ListView(
            primary: false,
            padding: const EdgeInsets.all(16),
            children: [
              const Text(
                'Fill the form below:',
                style: AppTextStyles.bodySmallRegBlackGrey,
              ),
              const Gap(24),
              KwTextInput(
                initialValue: form.fullName,
                onChanged: (fullName) {
                  updateCurrentForm(form.copyWith(fullName: fullName));
                },
                title: 'Full Name',
                hintText: 'Enter your full name',
                textInputAction: TextInputAction.next,
                textCapitalization: TextCapitalization.sentences,
                keyboardType: TextInputType.name,
                borderColor: AppColors.grayStroke,
                showError: formErrors.containsKey(WagesFormFields.fullName),
                helperText: formErrors[WagesFormFields.fullName],
              ),
              const Gap(8),
              KwTextInput(
                initialValue: form.city,
                onChanged: (city) {
                  updateCurrentForm(form.copyWith(city: city));
                },
                title: 'City',
                hintText: 'Enter your City',
                textInputAction: TextInputAction.next,
                textCapitalization: TextCapitalization.sentences,
                keyboardType: TextInputType.name,
                borderColor: AppColors.grayStroke,
                showError: formErrors.containsKey(WagesFormFields.city),
                helperText: formErrors[WagesFormFields.city],
              ),
              const Gap(24),
              const Text(
                'By signing below, you agree you have read and '
                'understood the following documents:',
                style: AppTextStyles.bodySmallMed,
              ),
              const Gap(12),
              Text(
                ' • Advantage Workforce Services (“AWS”) Offer Letter',
                style: AppTextStyles.bodySmallMed.copyWith(
                  color: AppColors.primaryBlue,
                ),
              ),
              const Gap(12),
              Text(
                ' • Arbitration Agreements',
                style: AppTextStyles.bodySmallMed.copyWith(
                  color: AppColors.primaryBlue,
                ),
              ),
              const Gap(12),
              Text(
                ' • Employee Policies',
                style: AppTextStyles.bodySmallMed.copyWith(
                  color: AppColors.primaryBlue,
                ),
              ),
              const Gap(24),
              const FormSignHolderWidget(
                signingPageTitle: 'Offer Letter & Agreement',
              ),
              const Gap(24),
              KwButton.outlinedPrimary(
                label: 'Document Preview',
                leftIcon: Assets.images.icons.eye,
                onPressed: () {
                  context.pushRoute(const FormPreviewRoute());
                },
              ),
            ],
          );
        },
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: KwButton.primary(label: 'Submit', onPressed: handleSubmit),
        ),
      ),
    );
  }
}
