import 'dart:math';

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/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/assigned_staff_item_widget.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/features/events/domain/blocs/details/event_details_bloc.dart';

class AssignedStaff extends StatelessWidget {
  final PositionState roleState;

  const AssignedStaff({super.key, required this.roleState});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _buildRoleHeader(context),
        if (roleState.isStaffExpanded)
          AnimatedContainer(
              duration: const Duration(milliseconds: 500),
              height: roleState.isStaffExpanded ? null : 0,
              child: Column(children: [
                ListView.builder(
                    shrinkWrap: true,
                    primary: false,
                    itemCount: min(3, roleState.position.staffContacts.length),
                    itemBuilder: (context, index) {
                      return AssignedStaffItemWidget(
                        staffContact: roleState.position.staffContacts[index],
                        department: roleState.position.department?.name ?? '',
                      );
                    }),
                KwButton.outlinedPrimary(
                    label: 'View All',
                    onPressed: () {
                      context.pushRoute(AssignedStaffRoute(
                        staffContacts: roleState.position.staffContacts,
                        department: roleState.position.department?.name ?? '',
                      ));
                    }),
                const Gap(8),
              ])),
      ],
    );
  }

  Widget _buildRoleHeader(context) {
    return Padding(
      padding: const EdgeInsets.only(top: 16, bottom: 16),
      child: GestureDetector(
        onTap: () {
          BlocProvider.of<EventDetailsBloc>(context)
              .add(OnAssignedStaffHeaderTapEvent(roleState));
        },
        child: Container(
          color: Colors.transparent,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text('Assigned Staff', style: AppTextStyles.bodyLargeMed),
              Container(
                height: 16,
                width: 16,
                color: Colors.transparent,
                child: AnimatedRotation(
                  duration: const Duration(milliseconds: 200),
                  turns: roleState.isStaffExpanded ? 0.5 : 0,
                  child: Center(
                    child: Assets.images.icons.chevronDown.svg(
                      colorFilter: ColorFilter.mode(
                        roleState.isStaffExpanded
                            ? AppColors.blackBlack
                            : AppColors.blackCaptionText,
                        BlendMode.srcIn,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
