import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/enum/create_scene_enum.dart';
import 'package:syncrow_app/features/scene/model/scene_settings_route_arguments.dart';
import 'package:syncrow_app/features/scene/widgets/create_scene_save_button.dart';
import 'package:syncrow_app/features/scene/widgets/if_then_containers/if_container.dart';
import 'package:syncrow_app/features/scene/widgets/if_then_containers/then_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/navigation/navigate_to_route.dart';
import 'package:syncrow_app/navigation/routing_constants.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/strings_manager.dart';

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

  @override
  Widget build(BuildContext context) {
    final sceneSettings = ModalRoute.of(context)!.settings.arguments as SceneSettingsRouteArguments;

    final isAutomation = sceneSettings.sceneType == CreateSceneEnum.deviceStatusChanges.name;
    // context.read<CreateSceneBloc>().add(SceneTypeEvent(isAutomation
    //     ? CreateSceneEnum.deviceStatusChanges
    //     : CreateSceneEnum.tabToRun));

    return DefaultScaffold(
      title:
          sceneSettings.sceneName.isNotEmpty ? sceneSettings.sceneName : StringsManager.createScene,
      padding: EdgeInsets.zero,
      leading: IconButton(
          onPressed: () {
            navigateToRoute(context, Routes.homeRoute);
          },
          icon: const Icon(
            Icons.arrow_back_ios,
          )),
      actions: [
        BlocBuilder<CreateSceneBloc, CreateSceneState>(
          builder: (context, state) {
            final sceneType = context.read<CreateSceneBloc>().sceneType;
            return Visibility(
              visible: sceneType != CreateSceneEnum.none,
              child: SizedBox(
                width: 40,
                child: GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(
                      context,
                      Routes.sceneAutoSettingsRoute,
                      arguments: {
                        "sceneId": sceneSettings.sceneId,
                        "isAutomation": isAutomation,
                        "sceneName": sceneSettings.sceneName,
                      },
                    );
                  },
                  child: SvgPicture.asset(
                    Assets.assetsIconsSettings,
                    colorFilter: const ColorFilter.mode(
                      ColorsManager.textPrimaryColor,
                      BlendMode.srcIn,
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ],
      child: Stack(
        children: [
          SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(
                  height: 24,
                ),
                // IF
                const IFDefaultContainer(),
                const SizedBox(
                  height: 8,
                ),
                // THEN
                ThenDefaultContainer(sceneId: sceneSettings.sceneId),
                const SizedBox(
                  height: 100,
                ),
              ],
            ),
          ),
          Positioned(
            bottom: 16,
            right: 40,
            left: 40,
            child: SizedBox(
              width: context.width * 0.8,
              child: CreateSceneSaveButton(
                sceneName: sceneSettings.sceneName,
                sceneId: sceneSettings.sceneId,
              ),
            ),
          )
        ],
      ),
    );
  }
}
