import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/smart_scene/smart_scene_select_dart_bloc.dart';
import 'package:syncrow_app/features/scene/model/scenes_model.dart';
import 'package:syncrow_app/features/scene/model/smart_scene_enable.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.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';

class SmartSceneSelectTabToRunList extends StatefulWidget {
  const SmartSceneSelectTabToRunList({
    super.key,
    required this.scenes,
  });
  final List<ScenesModel> scenes;

  @override
  State<SmartSceneSelectTabToRunList> createState() => _SmartSceneSelectTabToRunListState();
}

class _SmartSceneSelectTabToRunListState extends State<SmartSceneSelectTabToRunList> {
  String groupValue = '';

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            // Expanded(
            //   child: TextButton(
            //       onPressed: () => Navigator.pop(context),
            //       child: BodySmall(
            //         text: 'Save',
            //         style: context.bodySmall
            //             .copyWith(color: ColorsManager.primaryColorWithOpacity),
            //       )),
            // ),
            const Spacer(),
            Expanded(
              flex: 2,
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: BodyMedium(
                  text: 'Tab To Run',
                  textAlign: TextAlign.center,
                  style: context.bodyMedium.copyWith(
                      color: ColorsManager.primaryColorWithOpacity, fontWeight: FontWeight.bold),
                ),
              ),
            ),
            const Spacer(),
          ],
        ),
        const Divider(
          color: ColorsManager.dividerColor,
        ),
        Expanded(
          child: ListView.builder(
              shrinkWrap: true,
              itemCount: widget.scenes.length,
              itemBuilder: (context, index) {
                final scene = widget.scenes[index];
                final selectedScene = context.read<SmartSceneSelectBloc>().smartSceneEnable;
                if (selectedScene != null) {
                  if (scene.id == selectedScene.entityId) {
                    groupValue = scene.id;
                  }
                }
                return SceneListTile(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  leadingWidget: Image.asset(
                    height: 32,
                    width: 32,
                    Assets.assetsIconsLogo,
                    fit: BoxFit.fill,
                  ),
                  titleString: scene.name,
                  trailingWidget: Radio(
                      value: scene.id,
                      groupValue: groupValue,
                      onChanged: (String? value) {
                        if (value != null) {
                          setState(() {
                            groupValue = value;
                          });

                          context
                              .read<SmartSceneSelectBloc>()
                              .add(SmartSceneEnableEvent(SmartSceneEnable(
                                entityId: scene.id,
                                actionExecutor: 'rule_trigger',
                                sceneORAutomationName: scene.name,
                                type: scene.type,
                                isAutomation: false,
                              )));
                          context
                              .read<SmartSceneSelectBloc>()
                              .add(const SmartSceneConfirmSelectionEvent());
                        }
                      }),
                  onPressed: () {
                    setState(() {
                      groupValue = scene.id;
                    });
                    context.read<SmartSceneSelectBloc>().add(SmartSceneEnableEvent(SmartSceneEnable(
                          entityId: scene.id,
                          actionExecutor: 'rule_trigger',
                          sceneORAutomationName: scene.name,
                          type: scene.type,
                          isAutomation: false,
                        )));
                    context
                        .read<SmartSceneSelectBloc>()
                        .add(const SmartSceneConfirmSelectionEvent());
                  },
                );
              }),
        ),
      ],
    );
  }
}
