import 'package:comwell_key_app/choose_share_room/components/share_room_confirm_dialog.dart';
import 'package:comwell_key_app/choose_share_room/cubit/choose_share_room_cubit.dart';
import 'package:comwell_key_app/choose_share_room/cubit/choose_share_room_state.dart';
import 'package:comwell_key_app/common/components/comwell_app_bar.dart';
import 'package:comwell_key_app/common/const.dart';
import 'package:comwell_key_app/overview/models/room.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/overview/models/booking.dart';
import 'package:comwell_key_app/up_sales/models/facility_type.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:comwell_key_app/up_sales/components/facility_icon_text.dart';
import 'package:comwell_key_app/up_sales/components/tags.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:comwell_key_app/common/components/shimmer_loader/room_info_shimmer_loader.dart';
import 'package:go_router/go_router.dart';

class RoomInfoPage extends StatefulWidget {
  final Booking booking;
  final bool isAssigned;

  const RoomInfoPage({super.key, required this.booking, required this.isAssigned});

  @override
  State<RoomInfoPage> createState() => _RoomInfoPageState();
}

class _RoomInfoPageState extends State<RoomInfoPage> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final images = [widget.booking.image];
    final room = Room.empty();
    final facilities = <FacilityType>[];
    final hasFacilities = facilities.isNotEmpty;
    const description = '';
    final height = MediaQuery.of(context).size.height;

    return BlocBuilder<ChooseShareRoomCubit, ChooseShareRoomState>(
      builder: (context, state) {
        if (state.isLoading) {
          return const RoomInfoShimmerLoader();
        }

        return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: const ComwellAppBar(),
          backgroundColor: Colors.white,
          body: SingleChildScrollView(
            physics: const AlwaysScrollableScrollPhysics(),
            child: ConstrainedBox(
              constraints: BoxConstraints(
                minHeight: height - kComwellAppBarHeight - 80,
              ),
              child: Column(
                children: [
                  // RoomImageCarousel(images: images.expand((x) => x).toList()),
                  Container(
                    width: double.infinity,
                    decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(24),
                        topRight: Radius.circular(24),
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                room.name,
                                style: theme.textTheme.headlineLarge,
                              ),
                              if (room.tags.isNotEmpty)
                                TagWidget(text: '${room.tags.first} M2', textColor: sandColor),
                            ],
                          ),
                          const SizedBox(height: 12),
                          Text(
                            description,
                            style: theme.textTheme.bodySmall,
                            maxLines: _isExpanded ? null : 3,
                            overflow: _isExpanded ? null : TextOverflow.ellipsis,
                          ),
                          const SizedBox(height: 4),
                          if (description.length > 200) ...[
                            GestureDetector(
                              onTap: () {
                                setState(() {
                                  _isExpanded = !_isExpanded;
                                });
                              },
                              child: Text(
                                _isExpanded ? context.strings.read_less : context.strings.read_more,
                                style: theme.textTheme.bodySmall?.copyWith(
                                  color: sandColor,
                                  decoration: TextDecoration.underline,
                                  decorationColor: sandColor,
                                ),
                              ),
                            ),
                          ],
                          const SizedBox(height: 24),
                          if (hasFacilities) ...[
                            Wrap(
                              spacing: 8,
                              runSpacing: 8,
                              children: [
                                ...facilities.map(
                                  (f) => FacilityIconText(facility: f, showDivider: true),
                                ),
                                GestureDetector(
                                  onTap: () => {},
                                  // _showFacilitiesSheet(context, facilities),
                                  child: Padding(
                                    padding: const EdgeInsets.symmetric(horizontal: 4.0),
                                    child: Text(
                                      context.strings.see_all_facilities,
                                      style: theme.textTheme.bodySmall?.copyWith(
                                        color: sandColor,
                                        decoration: TextDecoration.underline,
                                        decorationColor: sandColor,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(height: 32),
                          ],
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          bottomNavigationBar: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Divider(color: colorDivider),
              Padding(
                padding: const EdgeInsets.only(
                  left: 16.0,
                  right: 16.0,
                  top: 16.0,
                  bottom: 40,
                ),
                child: ElevatedButton(
                  onPressed: () async {
                    if (widget.isAssigned) {
                      context.push(AppRoutes.shareRoom, extra: widget.booking);
                      return;
                    }
                    await showDialog<bool>(
                      context: context,
                      builder: (context) {
                        return const ShareRoomConfirmDialog();
                      },
                    );

                    if (context.mounted) {
                      context.pop(true);
                    }
                  },
                  child: Text(
                    widget.isAssigned ? context.strings.share_room : context.strings.choose_room,
                    style: theme.textTheme.headlineSmall?.copyWith(color: Colors.white),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}