import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:intl/intl.dart';
import 'package:krow/core/application/common/str_extensions.dart';
import 'package:krow/core/data/models/event/addon_model.dart';
import 'package:krow/core/entity/event_entity.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.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/icon_row_info_widget.dart';
import 'package:krow/features/events/presentation/event_details/widgets/event_button_group_widget.dart';

class EventInfoCardWidget extends StatelessWidget {
  final EventEntity item;

  final bool isPreview;

  const EventInfoCardWidget(
      {required this.item, super.key, this.isPreview = false});

  Color getIconColor(EventStatus? status) {
    return switch (status) {
      EventStatus.active || EventStatus.finished => AppColors.statusSuccess,
      EventStatus.pending ||
      EventStatus.assigned ||
      EventStatus.confirmed =>
        AppColors.primaryBlue,
      _ => AppColors.statusWarning
    };
  }

  Color getIconBgColor(EventStatus? status) {
    switch (status) {
      case EventStatus.active:
      case EventStatus.finished:
        return AppColors.tintGreen;
      case EventStatus.pending:
      case EventStatus.assigned:
      case EventStatus.confirmed:
        return AppColors.tintBlue;
      default:
        return AppColors.tintOrange;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: KwBoxDecorations.white12,
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 24),
      margin: const EdgeInsets.only(bottom: 12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          _buildStatusRow(),
          const Gap(24),
          Text(item.name, style: AppTextStyles.headingH1),
          const Gap(24),
          IconRowInfoWidget(
            icon: Assets.images.icons.calendar.svg(),
            title: 'Date',
            value: DateFormat('MM.dd.yyyy')
                .format(item.startDate ?? DateTime.now()),
          ),
          const Gap(12),
          IconRowInfoWidget(
            icon: Assets.images.icons.location.svg(),
            title: 'Location',
            value: item.hub?.name ?? 'Hub Name',
          ),
          const Gap(12),
          ValueListenableBuilder(
              valueListenable: item.totalCost,
              builder: (context, value, child) {
                return IconRowInfoWidget(
                  icon: Assets.images.icons.dollarSquare.svg(),
                  title: 'Value',
                  value: '\$${value.toStringAsFixed(2)}',
                );
              }),
          const Gap(24),
          const Divider(
            color: AppColors.grayTintStroke,
            thickness: 1,
            height: 0,
          ),
          _buildAddons(item.addons),
          ..._buildAdditionalInfo(),
          if (!isPreview) const Gap(24),
          EventButtonGroupWidget(item: item, isPreview: isPreview),
        ],
      ),
    );
  }

  List<Widget> _buildAdditionalInfo() {
    return [
      const Gap(12),
      Text('Additional Information',
          style:
              AppTextStyles.bodySmallReg.copyWith(color: AppColors.blackGray)),
      const Gap(2),
      Text(
          (item.additionalInfo == null || item.additionalInfo!.trim().isEmpty)
              ? 'No additional information'
              : item.additionalInfo!,
          style: AppTextStyles.bodyMediumMed),
    ];
  }

  Widget _buildAddons(List<AddonModel>? selectedAddons) {
    if (item.addons == null || item.addons!.isEmpty) {
      return const SizedBox.shrink();
    }

    return Column(
      children: [
        ...selectedAddons?.map((addon) {
              var textStyle = AppTextStyles.bodyMediumMed.copyWith(height: 1);
              return Padding(
                padding: const EdgeInsets.only(top: 12),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(addon.name ?? '', style: textStyle),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Assets.images.icons.addonInclude.svg(),
                        const Gap(7),
                        Text('Included', style: textStyle)
                      ],
                    )
                  ],
                ),
              );
            }) ??
            [],
      ],
    );
  }

  Row _buildStatusRow() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          height: 48,
          width: 48,
          decoration: BoxDecoration(
            color: getIconBgColor(item.status),
            shape: BoxShape.circle,
          ),
          child: Center(
            child: Assets.images.icons.navigation.confetti.svg(
              colorFilter:
                  ColorFilter.mode(getIconColor(item.status), BlendMode.srcIn),
            ),
          ),
        ),
        if (!isPreview)
          Container(
              height: 28,
              padding: const EdgeInsets.symmetric(horizontal: 8),
              decoration: BoxDecoration(
                color: item.status?.color,
                borderRadius: BorderRadius.circular(14),
              ),
              child: Center(
                child: Padding(
                  padding: const EdgeInsets.only(bottom: 2.0),
                  child: Text(
                    item.status?.name.capitalize() ?? '',
                    style: AppTextStyles.bodySmallMed.copyWith(
                        color: item.status == EventStatus.draft
                            ? null
                            : AppColors.grayWhite),
                  ),
                ),
              ))
      ],
    );
  }
}

extension on EventStatus {
  Color get color {
    switch (this) {
      case EventStatus.active:
      case EventStatus.finished:
        return AppColors.statusSuccess;
      case EventStatus.pending:
        return AppColors.blackGray;
      case EventStatus.assigned:
        return AppColors.statusWarning;
      case EventStatus.confirmed:
        return AppColors.primaryBlue;
      case EventStatus.completed:
        return AppColors.statusSuccess;
      case EventStatus.closed:
        return AppColors.bgColorDark;
      case EventStatus.canceled:
        return AppColors.statusError;
      case EventStatus.draft:
        return AppColors.primaryYolk;
    }
  }
}
