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/entity/staff_contact_entity.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/scroll_layout_helper.dart';
import 'package:krow/core/presentation/widgets/staff_position_details_widget.dart';
import 'package:krow/core/presentation/widgets/ui_kit/check_box.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_dropdown.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/rate_staff/domain/bloc/rating_staff_bloc.dart';
import 'package:krow/features/rate_staff/presentation/widgets/rating_widget.dart';

@RoutePage()
class RateStaffScreen extends StatelessWidget implements AutoRouteWrapper {
  final StaffContact staff;
  final TextEditingController _reasonController = TextEditingController();

  RateStaffScreen({super.key, required this.staff});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        titleText: 'Rate Staff',
      ),
      body: BlocConsumer<RatingStaffBloc, RatingStaffState>(
        listener: (context, state) {
          if (state.error.isNotEmpty) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(state.error),
                backgroundColor: AppColors.statusError,
              ),
            );
          }

          if (state.success) {
            context.router.maybePop();
          }
        },
        builder: (context, state) {
          return ScrollLayoutHelper(
            padding: const EdgeInsets.symmetric(horizontal: 0),
            upperWidget: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Gap(16),
                StaffPositionAvatar(
                    imageUrl: staff.photoUrl,
                    userName: '${staff.firstName} ${staff.lastName}'),
                const Gap(16),
                StaffContactsWidget(staff: staff),
                const Gap(12),
                StaffPositionDetailsWidget(staff: staff),
                const Gap(12),
                const RatingWidget(),
                if (state.rating < 3) ..._blackListCheckbox(context, state),
                const Gap(24),
              ],
            ),
            lowerWidget: Padding(
              padding: const EdgeInsets.only(bottom: 36, left: 16, right: 16),
              child: KwButton.primary(
                  disabled: state.rating < 3 && state.reason == null,
                  onPressed: () {
                    BlocProvider.of<RatingStaffBloc>(context).add(
                      RatingSubmitEvent(),
                    );
                  },
                  label: 'Submit Review'),
            ),
          );
        },
      ),
    );
  }

  List<Widget> _blackListCheckbox(
      BuildContext context, RatingStaffState state) {
    return [
      const Gap(12),
      GestureDetector(
        onTap: () {
          BlocProvider.of<RatingStaffBloc>(context)
              .add(StaffBlacklistToggleEvent());
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            const Gap(32),
            KWCheckBox(
              value: state.blackListed,
              style: CheckBoxStyle.black,
            ),
            const Gap(6),
            Text(
              'Don\'t assign to my Events anymore',
              style: AppTextStyles.bodyMediumReg
                  .copyWith(color: AppColors.blackGray),
            ),
            const Gap(16),
          ],
        ),
      ),
      Padding(
        padding: const EdgeInsets.only(top: 24, left: 16, right: 16),
        child: KwDropdown<String>(
          title: 'Reason',
          items: ['Lack of responsibility', 'Unpreparedness', 'Poor communication','Unprofessional behavior','Ignoring Responsibilities']
              .map((e) => KwDropDownItem(data: e, title: e))
              .toList(),
          horizontalPadding: 16,
          hintText: 'Select a reason',
          selectedItem: state.reason != null
              ? KwDropDownItem(data: state.reason!, title: state.reason!)
              : null,
          onSelected: (String item) {
            BlocProvider.of<RatingStaffBloc>(context)
                .add(StaffReasonSelectedEvent(item));
          },
        ),
      ),
      const Gap(8),
      Padding(
        padding: const EdgeInsets.only(top: 8.0, left: 16, right: 16),
        child: KwTextInput(
          onChanged: (String value) {
            BlocProvider.of<RatingStaffBloc>(context)
                .add(StaffReasonDetailsChangeEvent(value));
          },
          minHeight: 144,
          maxLength: 300,
          showCounter: true,
          radius: 12,
          title: 'Additional Details',
          hintText: 'Enter your reason here...',
          controller: _reasonController,
        ),
      ),
    ];
  }

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider(
      create: (context) => RatingStaffBloc(staff),
      child: this,
    );
  }
}
