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/application/common/int_extensions.dart';
import 'package:krow/core/application/common/str_extensions.dart';
import 'package:krow/core/data/enums/staff_skill_enums.dart';
import 'package:krow/core/data/models/staff_role.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_button.dart';
import 'package:krow/features/profile/role/domain/bloc/role_bloc.dart';
import 'package:krow/features/profile/role/domain/bloc/role_event.dart';

class RoleCard extends StatelessWidget {
  final StaffRole role;
  final int index;

  final bool canDelete;

  const RoleCard({
    super.key,
    required this.role,
    required this.index,
    required this.canDelete,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '${(index + 1).toOrdinal()} ${'role_details'.tr()}',
              style: AppTextStyles.bodyMediumMed,
            ),
            if (role.status != null) _buildStatus(),
          ],
        ),
        const SizedBox(height: 24),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildRoleTextInfo(context, '${'role'.tr()}:', role.skill?.name),
            _buildRoleTextInfo(
                context, '${'experience'.tr()}:', '${role.experience} ${'years'.tr()}'),
            _buildRoleTextInfo(context, '${'level'.tr()}:', role.level?.name),
          ],
        ),
        const SizedBox(height: 24),
        Row(
          children: [
            Expanded(
              child: KwButton.outlinedPrimary(
                label: 'edit_role'.tr(),
                onPressed: () {
                  BlocProvider.of<RoleBloc>(context).add(
                    RoleSelectEvent(role),
                  );
                },
                leftIcon: Assets.images.icons.edit,
              ),
            ),
            if (canDelete) ...[
              const Gap(8),
              KwButton.outlinedPrimary(
                      fit: KwButtonFit.circular,
                      onPressed: () {
                        BlocProvider.of<RoleBloc>(context).add(
                          RoleDeleteEvent(role),
                        );
                      },
                      leftIcon: Assets.images.icons.delete)
                  .copyWith(color: AppColors.statusError),
            ],
          ],
        ),
        const Gap(12),
      ],
    );
  }

  Container _buildStatus() {
    Color color;
    switch (role.status) {
      case StaffSkillStatus.pending:
        color = AppColors.primaryBlue;
        break;
      case StaffSkillStatus.verified:
        color = AppColors.statusSuccess;
        break;
      case StaffSkillStatus.deactivated:
        color = AppColors.statusError;
        break;
      default:
        color = AppColors.bgColorDark;
    }
    return Container(
      height: 24,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(12),
      ),
      padding: const EdgeInsets.only(left: 8, right: 8),
      child: Center(
          child: Text(
        role.status?.name.capitalize() ?? '',
        style: AppTextStyles.bodySmallMed.copyWith(color: Colors.white),
      )),
    );
  }

  Column _buildRoleTextInfo(BuildContext context, String title, String? value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: AppTextStyles.captionReg.copyWith(
            color: AppColors.blackGray,
          ),
        ),
        const Gap(4),
        Text(value ?? '', style: AppTextStyles.bodyMediumMed)
      ],
    );
  }
}
