import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/scene/bloc/create_scene/create_scene_bloc.dart';
import 'package:syncrow_app/features/scene/enum/operation_dialog_type.dart';
import 'package:syncrow_app/features/scene/model/scene_static_function.dart';
import 'package:syncrow_app/features/scene/widgets/alert_dialogs/alert_dialog_countdown.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
import 'package:syncrow_app/features/shared_widgets/light_divider.dart';

import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/generated/assets.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';

class CustomBottomSheetWidget extends StatelessWidget {
  const CustomBottomSheetWidget({
    super.key,
    required this.sceneId,
  });
  final String sceneId;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 16, bottom: 8),
            child: BodyLarge(
              text: 'Add Task',
              style: context.bodyLarge.copyWith(
                fontWeight: FontWeight.w700,
                color: ColorsManager.primaryColorWithOpacity,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          SizedBox(
            width: context.width * 0.7,
            child: const LightDivider(),
          ),
          SceneListTile(
            assetPath: Assets.assetsIconsLight,
            assetHeight: 24,
            minLeadingWidth: 30,
            titleString: 'Control Single Device',
            padding: const EdgeInsets.symmetric(horizontal: 20),
            trailingWidget: const Icon(
              Icons.arrow_forward_ios_rounded,
              size: 16,
              color: ColorsManager.greyColor,
            ),
            onPressed: () {
              Navigator.pushNamed(context, Routes.sceneControlDevicesRoute);
            },
          ),
          SceneListTile(
            assetPath: Assets.player,
            titleString: 'Select Smart Scene',
            minLeadingWidth: 30,
            assetHeight: 24,
            padding: const EdgeInsets.symmetric(horizontal: 20),
            trailingWidget: const Icon(
              Icons.arrow_forward_ios_rounded,
              size: 16,
              color: ColorsManager.greyColor,
            ),
            onPressed: () {
              Navigator.pushNamed(context, Routes.smartAutomationSelectRoute,
                  arguments: sceneId);
            },
          ),
          SceneListTile(
            assetPath: Assets.delay,
            titleString: 'Delay The Action',
            assetHeight: 24,
            minLeadingWidth: 30,
            padding: const EdgeInsets.symmetric(horizontal: 20),
            trailingWidget: const Icon(
              Icons.arrow_forward_ios_rounded,
              size: 16,
              color: ColorsManager.greyColor,
            ),
            onPressed: () => _onDelayActionPressed(context),
          ),
        ],
      ),
    );
  }

  void _onDelayActionPressed(BuildContext context) {
    final functionValues =
        context.read<CreateSceneBloc>().selectedValues['delay'];
    final functions = [
      SceneStaticFunction(
        deviceId: 'delay',
        deviceName: 'Delay The Action',
        icon: '',
        operationName: 'Delay The Action',
        code: '',
        functionValue: 0,
        operationDialogType: OperationDialogType.delay,
        operationalValues: [
          SceneOperationalValue(icon: '', value: 0),
        ],
      ),
    ];
    context.customAlertDialog(
      alertBody: AlertDialogCountdown(
        durationValue: functions[0].operationalValues.first.value,
        function: functions[0],
        functionValue: functionValues,
      ),
      title: functions[0].operationName,
      onConfirm: () {
        final selectedValue =
            context.read<CreateSceneBloc>().selectedValues['delay'];
        context.read<CreateSceneBloc>().add(TempHoldSceneTasksEvent(
              deviceType: '',
              deviceControlModel: DeviceControlModel(
                deviceId: '',
                code: '',
                value: selectedValue,
              ),
              deviceId: 'delay',
              operation: functions[0].operationName,
              icon: Assets.delay,
              deviceName: 'Delay The Action',
              uniqueId: functions[0].uniqueCustomId!,
              operationType: functions[0].operationDialogType,
            ));
        context.read<CreateSceneBloc>().add(const AddTaskEvent());
        Navigator.pop(context);
        Navigator.pop(context);
      },
      onDismiss: () {
        final tempTaskList = context.read<CreateSceneBloc>().tempTasksList;

        for (var element in tempTaskList) {
          if (element.code == functions[0].code) {
            context
                .read<CreateSceneBloc>()
                .add(RemoveTempTaskByIdEvent(code: functions[0].code));
            context
                .read<CreateSceneBloc>()
                .add(RemoveFromSelectedValueById(code: functions[0].code));
          }
        }
        //  }
        Navigator.pop(context);
      },
    );
  }
}
