import 'package:comwell_key_app/common/components/comwell_app_bar.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/up_sales/components/up_sales_bottom_button.dart';
import 'package:comwell_key_app/up_sales/cubit/up_sales_cubit.dart';
import 'package:comwell_key_app/up_sales/cubit/up_sales_state.dart';
import 'package:comwell_key_app/up_sales/models/room_upgrade_list.dart';
import 'package:comwell_key_app/up_sales/components/upsale_header_media.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

class ServicesUpgradePage extends StatelessWidget {
  final RoomUpgradeList roomUpgradeList;
  const ServicesUpgradePage({
    super.key,
    required this.roomUpgradeList,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    final isSelected = roomUpgradeList.isRoomUpgradeSelected;

    return BlocBuilder<UpSalesCubit, UpSalesState>(
      builder: (context, state) {
        final cubit = context.read<UpSalesCubit>();

        return Scaffold(
          backgroundColor: Theme.of(context).colorScheme.surface,
          appBar: ComwellAppBar(
            onBackPressed: () {
              context.pop(true);
            },
          ),
          extendBodyBehindAppBar: true,
          body: Column(
            children: [
              Stack(
                children: [
                  ClipRRect(
                    borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(10),
                      topRight: Radius.circular(10),
                    ),
                    child: UpsaleHeaderMedia(
                      height: height,
                      animationJson: roomUpgradeList.addOnUpgrade?.animationJson,
                      backgroundImageUrl: roomUpgradeList.addOnUpgrade?.backgroundImageUrl,
                      images: roomUpgradeList.addOnUpgrade?.images.toList(),
                    ),
                  ),
                  Container(
                    decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(12)),
                      gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                          Colors.black26,
                          Colors.black54,
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              Container(
                width: double.infinity,
                color: Theme.of(context).colorScheme.surface,
                padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          roomUpgradeList.addOnUpgrade?.name ?? '',
                          style: theme.textTheme.headlineLarge,
                        ),
                        Text(
                          context.strings.total_charge_value(
                            roomUpgradeList.addOnUpgrade?.price.toString() ?? '0',
                          ),
                          style: theme.textTheme.headlineLarge,
                        ),
                      ],
                    ),
                    const SizedBox(height: 12),
                    SizedBox(
                      width: width * 0.8,
                      child: Text(
                        roomUpgradeList.addOnUpgrade?.description ?? '',
                        style: theme.textTheme.bodySmall?.copyWith(
                          color: Theme.of(context).colorScheme.onSurface,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          bottomNavigationBar: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Divider(color: colorDivider),
              UpSalesBottomButton(
                isSinglePurchase: roomUpgradeList.isSinglePurchase,
                onSinglePurchaseContinue: () async {
                  if (roomUpgradeList.isSinglePurchase) {
                    cubit.toggleSelectedUpgrade(roomUpgradeList.addOnUpgrade!);

                    final result = await context.push(
                      AppRoutes.upSaleConfirmation,
                      extra: [
                        cubit.selectedAddOnUpgrades,
                        cubit.extrasTotalPrice,
                        roomUpgradeList.roomUpgrade,
                      ],
                    );

                    if (result == true) {
                      if (context.mounted) {
                        context.pop();
                      }
                    }
                  }
                },
                onContinue: () {
                  context.pop([roomUpgradeList.addOnUpgrade, isSelected]);
                },
                extrasTotalPrice: cubit.extrasTotalPrice,
                selectedRoomUpgrade: state.selectedRoomUpgrade,
                children: [
                  isSelected
                      ? Text(
                          context.strings.remove_from_booking,
                          style: theme.textTheme.headlineSmall?.copyWith(color: Colors.white),
                        )
                      : Text(
                          context.strings.add_to_booking,
                          style: theme.textTheme.headlineSmall?.copyWith(color: Colors.white),
                        ),
                  isSelected
                      ? Text(
                          "-${context.strings.total_charge_value(roomUpgradeList.addOnUpgrade?.price.toString() ?? '0')}",
                          style: theme.textTheme.headlineSmall?.copyWith(color: Colors.white),
                        )
                      : Text(
                          "+${context.strings.total_charge_value(roomUpgradeList.addOnUpgrade?.price.toString() ?? '0')}",
                          style: theme.textTheme.headlineSmall?.copyWith(color: Colors.white),
                        ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }
}