import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.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_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_bloc.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_event.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_state.dart';
import 'package:krow/features/shifts/presentation/dialogs/complete_dialog/shift_complete_dialog.dart';
import 'package:krow/features/shifts/presentation/dialogs/complete_dialog/widgets/break_time_picker.dart';
import 'package:krow/features/shifts/presentation/dialogs/complete_dialog/widgets/complete_reason_input.dart';

class CompleteDialogView extends StatelessWidget {
  CompleteDialogView({super.key, this.canSkip = true, required this.eventName});

  final _textEditingController = TextEditingController();
  final bool canSkip;
  final String eventName;

  String _title(BreakStatus state) => state == BreakStatus.negative
      ? 'help_us_understand'.tr()
      : 'did_you_take_a_break'.tr();

  String _message(BreakStatus state) => state == BreakStatus.negative
      ? 'taking_breaks_essential'.tr()
      : 'taking_regular_breaks'.tr(namedArgs: {'eventName': eventName});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CompleteDialogBloc, CompleteDialogState>(
      builder: (context, state) {
        return Center(
          child: AnimatedSize(
            duration: const Duration(milliseconds: 300),
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
              decoration: BoxDecoration(
                color: AppColors.grayWhite,
                borderRadius: BorderRadius.circular(24),
              ),
              child: SingleChildScrollView(
                padding:
                    const EdgeInsets.symmetric(horizontal: 24, vertical: 56),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      _title(state.status),
                      style: AppTextStyles.headingH1.copyWith(height: 1),
                      textAlign: TextAlign.center,
                    ),
                    const Gap(8),
                    Text(
                      _message(state.status),
                      style: AppTextStyles.bodyMediumReg
                          .copyWith(color: AppColors.blackGray),
                      textAlign: TextAlign.center,
                    ),
                    const Gap(8),
                    if (state.status == BreakStatus.positive)
                      BreakTimePicker(state: state),
                    if (state.status == BreakStatus.negative)
                      CompleteReasonInput(selectedReason: state.selectedReason),
                    if (state.status == BreakStatus.negative) ...[
                      const Gap(8),
                      KwTextInput(
                        controller: _textEditingController,
                        minHeight: 144,
                        maxLength: 300,
                        showCounter: true,
                        radius: 12,
                        title: 'additional_reasons'.tr(),
                        hintText: 'enter_main_text'.tr(),
                      ),
                    ],
                    const Gap(24),
                    _buttonGroup(context, state),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buttonGroup(BuildContext context, CompleteDialogState state) {
    var cancelButton = canSkip
        ? Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: KwButton.outlinedPrimary(
              label: 'cancel'.tr(),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          )
        : const SizedBox.shrink();
    return Column(
      children: [
        if (state.status == BreakStatus.neutral) ...[
          KwButton.primary(
            label: 'yes_i_took_a_break'.tr(),
            onPressed: () => context
                .read<CompleteDialogBloc>()
                .add(SelectBreakStatus(BreakStatus.positive)),
          ),
          const Gap(8),
          KwButton.outlinedPrimary(
            label: 'no_i_didnt_take_a_break'.tr(),
            onPressed: () => context
                .read<CompleteDialogBloc>()
                .add(SelectBreakStatus(BreakStatus.negative)),
          ),
        ],
        if (state.status == BreakStatus.positive) ...[
          KwButton.primary(
            disabled: state.breakTimeInputError != null,
            label: 'submit_break_time'.tr(),
            onPressed: () {
              Navigator.pop(context, <String, dynamic>{
                'result': true,
                'details': ClockOutDetails.positive(
                  state.startTime,
                  state.endTime,
                )
              });
            },
          ),
          cancelButton,
        ],
        if (state.status == BreakStatus.negative) ...[
          KwButton.primary(
            disabled: state.selectedReason == null,
            label: 'submit_reason'.tr(),
            onPressed: () {
              Navigator.pop(context, {
                'result': false,
                'details': ClockOutDetails.negative(
                    state.selectedReason!, _textEditingController.text)
              });
            },
          ),
          cancelButton,
        ]
      ],
    );
  }
}
