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/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/ui_kit/kw_popup_menu.dart';
import 'package:krow/features/earning/domain/bloc/earnings_bloc.dart';
import 'package:krow/features/earning/presentation/widget/earnings_charts_card.dart';

class EarningsHistoryHeader extends StatelessWidget {
  const EarningsHistoryHeader({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
            padding: const EdgeInsets.all(12),
            margin: const EdgeInsets.only(bottom: 24, left: 12, right: 12),
            decoration: KwBoxDecorations.primaryDark,
            child: BlocBuilder<EarningsBloc, EarningsState>(
              builder: (context, state) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    _buildPeriodRow(),
                    const Gap(12),
                    const EarningsCharts(),
                    const Gap(8),
                    _buildMinMaxRow(
                      state.earnings.minEarningInPeriod,
                      state.earnings.maxEarningInPeriod,
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ],
    );
  }



  _buildMinMaxRow(int min, int max) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration:
          KwBoxDecorations.primaryDark.copyWith(color: AppColors.darkBgStroke),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildInfoRow('max_earning'.tr(), '\$$max'),
          const Gap(12),
          _buildInfoRow('min_earning'.tr(), '\$$min'),
        ],
      ),
    );
  }

  Row _buildInfoRow(String title, String? value) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style:
              AppTextStyles.bodyMediumMed.copyWith(color: AppColors.grayWhite),
        ),
        Text(
          value ?? '',
          style:
              AppTextStyles.bodyMediumMed.copyWith(color: AppColors.grayWhite),
        ),
      ],
    );
  }

  Row _buildPeriodRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          'earnings_history'.tr(),
          textAlign: TextAlign.center,
          style:
              AppTextStyles.bodyMediumMed.copyWith(color: AppColors.grayWhite),
        ),
        KwPopupMenu(
            customButtonBuilder: (context, isOpen) {
              return Row(
                children: [
                  Assets.images.icons.calendar.svg(
                      width: 12,
                      height: 12,
                      colorFilter: const ColorFilter.mode(
                          AppColors.grayWhite, BlendMode.srcIn)),
                  const Gap(2),
                  Text(
                    'period'.tr(),
                    style: AppTextStyles.bodyMediumMed
                        .copyWith(color: AppColors.grayWhite),
                  ),
                  const Gap(12),
                  AnimatedRotation(
                    duration: const Duration(milliseconds: 150),
                    turns: isOpen ? -0.5 : 0,
                    child: Assets.images.icons.caretDown.svg(
                      width: 16,
                      height: 16,
                      colorFilter: const ColorFilter.mode(
                          AppColors.darkBgInactive, BlendMode.srcIn),
                    ),
                  ),
                ],
              );
            },
            menuItems: [
              KwPopupMenuItem(title: 'week'.tr(), onTap: () {}),
              KwPopupMenuItem(title: 'month'.tr(), onTap: () {}),
              KwPopupMenuItem(title: 'range'.tr(), onTap: () {}),
            ])
      ],
    );
  }
}
