import 'package:be_happy/presentation/event/subscription_list_event.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../../core/app_colors.dart';
import '../components/custom_app_bar.dart';
import '../components/subscription_card.dart';
import '../state/subscription_list_state.dart';
import '../viewmodel/subscription_list_bloc.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          gradient: AppColors.phoneScreenBg,
        ),
        child: SafeArea(
          child: Column(
            children: [
              const SizedBox(height: 16),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: CustomAppBar(title: 'Абонементы'),
              ),

              Expanded(
                child: Stack(
                  children: [
                    Positioned(
                      bottom: 0,
                      left: 0,
                      right: 0,
                      child: Opacity(
                        opacity: 0.5,
                        child: Image.asset('assets/wave.png'),
                      ),
                    ),
                    BlocBuilder<SubscriptionListBloc, SubscriptionState>(
                      builder: (context, state) {
                        if (state is SubscriptionsLoading) {
                          return const Center(child: CircularProgressIndicator());
                        } else if (state is SubscriptionsLoaded) {
                          final clientSubsMap = {
                            for (var sub in state.activeSubscriptions) sub.subscriptionId: sub
                          };

                          return ListView.builder(
                            padding: const EdgeInsets.only(top: 20),
                            itemCount: state.subscriptions.length,
                            itemBuilder: (context, index) {
                              final subscription = state.subscriptions[index];

                              final bool isPurchased = subscription.isCurrent;

                              final clientSub = clientSubsMap[subscription.id];
                              final DateTime? expirationDate = isPurchased ? clientSub?.expiredAt : null;

                              return SizedBox(
                                height: 230, //
                                child: SubscriptionCard(
                                  subscription: subscription,
                                  isActive: isPurchased,
                                  expiredAt: expirationDate,
                                  onRefresh: () {
                                    context.read<SubscriptionListBloc>().add(LoadSubscriptionsEvent());
                                  },
                                ),
                              );
                            },
                          );
                        } else if (state is SubscriptionsError) {
                          return Center(
                            child: Text(
                              state.message,
                              style: const TextStyle(color: Colors.red),
                            ),
                          );
                        }
                        return const SizedBox();
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}