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/view/create_scene_view.dart';
import 'package:syncrow_app/features/scene/widgets/if_then_containers/then_added_tasks.dart';
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
import 'package:syncrow_app/features/scene/widgets/bottom_sheet_widget.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.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/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/custom_page_route.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

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

  final String sceneId;

  @override
  Widget build(BuildContext context) {
    return DefaultContainer(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SceneListTile(
            leadingWidget: BodyLarge(
              text: 'Then',
              style: context.bodyLarge.copyWith(
                fontWeight: FontWeight.bold,
                color: ColorsManager.primaryTextColor,
              ),
            ),
            trailingWidget: BlocBuilder<CreateSceneBloc, CreateSceneState>(
              builder: (context, state) {
                bool isClickable = false;
                if (state is AddSceneTask) {
                  isClickable = state.tasksList.isNotEmpty;
                }
                return GestureDetector(
                  onTap: isClickable
                      ? () => context.customBottomSheet(
                            child: CustomBottomSheetWidget(sceneId: sceneId),
                          )
                      : null,
                  child: SvgPicture.asset(
                    Assets.addIcon,
                    colorFilter: ColorFilter.mode(
                      isClickable
                          ? ColorsManager.primaryColor
                          : ColorsManager.greyColor,
                      BlendMode.srcIn,
                    ),
                  ),
                );
              },
            ),
            padding: EdgeInsets.zero,
          ),
          const LightDivider(),
          BlocBuilder<CreateSceneBloc, CreateSceneState>(
            builder: (context, state) {
              if (state is CreateSceneLoading) {
                return const Center(child: LinearProgressIndicator());
              }

              if (state is AddSceneTask) {
                final taskLists = state.tasksList;
                if (taskLists.isNotEmpty) {
                  return ListView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: taskLists.length,
                    reverse: true,
                    itemBuilder: (context, index) {
                      return ThenAddedTasksContainer(
                        taskItem: taskLists[index],
                      );
                    },
                  );
                }
              }
              return SceneListTile(
                titleString: '+ Add Task',
                textAlign: TextAlign.center,
                onPressed: () {
                  final sceneType = context.read<CreateSceneBloc>().sceneType;
                  if (sceneType.name == CreateSceneEnum.none.name) {
                    Navigator.push(
                        context,
                        CustomPageRoute(
                            builder: (context) => const CreateSceneView()));
                  } else {
                    context.customBottomSheet(
                      child: CustomBottomSheetWidget(
                        sceneId: sceneId,
                      ),
                    );
                  }
                },
              );
            },
          )
        ],
      ),
    );
  }
}
