import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/model/ceiling_sensor_model.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';

class PresenceSpaceTypeDialog extends StatefulWidget {
  final String description;
  final SpaceTypes selectedSpaceType;
  final void Function(SpaceTypes spaceType) onSpaceTypeSelected;

  const PresenceSpaceTypeDialog({
    super.key,
    required this.description,
    required this.selectedSpaceType,
    required this.onSpaceTypeSelected,
  });

  @override
  _PresenceSpaceTypeDialogState createState() =>
      _PresenceSpaceTypeDialogState();
}

class _PresenceSpaceTypeDialogState extends State<PresenceSpaceTypeDialog> {
  late SpaceTypes _selectedSpaceType;

  @override
  void initState() {
    super.initState();
    _selectedSpaceType = widget.selectedSpaceType;
  }

  @override
  Widget build(BuildContext context) {
    final Map<SpaceTypes, String> spaceTypeIcons = {
      SpaceTypes.none: Assets.office,
      SpaceTypes.parlour: Assets.parlour,
      SpaceTypes.area: Assets.dyi,
      SpaceTypes.toilet: Assets.bathroom,
      SpaceTypes.bedroom: Assets.bedroom,
    };

    final Map<SpaceTypes, String> spaceTypeTitles = {
      SpaceTypes.none: 'None',
      SpaceTypes.parlour: 'Parlour',
      SpaceTypes.area: 'Area',
      SpaceTypes.toilet: 'Toilet',
      SpaceTypes.bedroom: 'Bedroom',
    };

    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),
      ),
      child: Container(
        // padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                widget.description,
                style: context.bodyMedium.copyWith(
                    color: ColorsManager.primaryColorWithOpacity,
                    fontWeight: FontsManager.extraBold,
                    fontSize: 16),
              ),
            ),
            Container(
              height: 1,
              width: double.infinity,
              color: ColorsManager.greyColor,
            ),
            const SizedBox(height: 16),
            Wrap(
              runSpacing: 12,
              spacing: 16,
              children: spaceTypeIcons.entries.map((entry) {
                final spaceType = entry.key;
                final icon = entry.value;
                final title = spaceTypeTitles[spaceType]!;
                return Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(25),
                    color: _selectedSpaceType == spaceType
                        ? ColorsManager.primaryColorWithOpacity
                        : ColorsManager.textGray,
                  ),
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        _selectedSpaceType = spaceType;
                      });
                    },
                    child: Padding(
                      padding: const EdgeInsets.only(left: 10, right: 10),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: const EdgeInsets.all(10),
                            child: SvgPicture.asset(
                              icon,
                            ),
                          ),
                          const SizedBox(height: 4),
                          Text(
                            title == "None" ? "Office" : title,
                            style: Theme.of(context)
                                .textTheme
                                .bodySmall
                                ?.copyWith(
                                    color: _selectedSpaceType == spaceType
                                        ? ColorsManager.onPrimaryColor
                                        : ColorsManager.blackColor,
                                    fontWeight: FontWeight.w400,
                                    fontSize: 15),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }).toList(),
            ),
            const SizedBox(height: 20),

            Container(
              height: 1,
              width: double.infinity,
              color: ColorsManager.greyColor,
            ),

            /// Cancel / Confirm
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                InkWell(
                  onTap: () => Navigator.pop(context),
                  child: Center(
                    child: BodyMedium(
                      text: 'Cancel',
                      style: context.bodyMedium.copyWith(
                        color: ColorsManager.greyColor,
                      ),
                    ),
                  ),
                ),
                Container(
                  height: 50,
                  width: 1,
                  color: ColorsManager.greyColor,
                ),
                InkWell(
                  onTap: () {
                    widget.onSpaceTypeSelected(_selectedSpaceType);
                    Navigator.pop(context, _selectedSpaceType);
                  },
                  child: Center(
                    child: BodyMedium(
                      text: 'Confirm',
                      style: context.bodyMedium.copyWith(
                        color: ColorsManager.primaryColorWithOpacity,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
