import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../utils/color_manager.dart';
import '../../../../common/buttons/cancel_button.dart';
import '../../../../common/buttons/default_button.dart';
import '../../../all_spaces/model/product_model.dart';
import '../../../all_spaces/model/tag.dart';
import '../../../helper/tag_helper.dart';
import '../../../space_model/models/space_template_model.dart';
import '../../../space_model/models/subspace_template_model.dart';
import '../../../space_model/widgets/dialog/create_space_model_dialog.dart';
import '../../../tag_model/views/add_device_type_model_widget.dart';
import '../../bloc/assign_tag_model_bloc.dart';
import '../../bloc/assign_tag_model_state.dart';

class RowOfSaveCancelWidget extends StatelessWidget {
  const RowOfSaveCancelWidget({
    super.key,
    required this.subspaces,
    required this.products,
    required this.allTags,
    required this.spaceName,
    required this.otherSpaceModels,
    required this.pageContext,
    required this.projectTags,
    required this.spaceModel,
    required this.allSpaceModels,
  });

  final List<SubspaceTemplateModel>? subspaces;
  final List<ProductModel>? products;
  final List<String>? allTags;
  final String spaceName;
  final List<String>? otherSpaceModels;
  final BuildContext? pageContext;
  final List<Tag> projectTags;
  final SpaceTemplateModel? spaceModel;
  final List<SpaceTemplateModel>? allSpaceModels;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AssignTagModelBloc, AssignTagModelState>(
      builder: (context, state) {
        if (state is AssignTagModelLoaded) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              const SizedBox(width: 10),
              Expanded(
                child: Builder(
                  builder: (buttonContext) => CancelButton(
                    label: 'Add New Device',
                    onPressed: () async {
                      final updatedTags = List<Tag>.from(state.tags);
                      final result = TagHelper.updateSubspaceTagModels(
                          updatedTags, subspaces);

                      final processedTags = result['updatedTags'] as List<Tag>;
                      final processedSubspaces =
                          List<SubspaceTemplateModel>.from(
                              result['subspaces'] as List<dynamic>);

                      if (context.mounted) {
                        Navigator.of(context).pop();

                        await showDialog<bool>(
                          barrierDismissible: false,
                          context: context,
                          builder: (dialogContext) => AddDeviceTypeModelWidget(
                              products: products,
                              subspaces: processedSubspaces,
                              isCreate: false,
                              initialSelectedProducts:
                                  TagHelper.createInitialSelectedProducts(
                                      processedTags, processedSubspaces),
                              allTags: allTags,
                              spaceName: spaceName,
                              otherSpaceModels: otherSpaceModels,
                              spaceTagModels: processedTags,
                              pageContext: pageContext,
                              projectTags: projectTags,
                              spaceModel: SpaceTemplateModel(
                                  modelName: spaceName,
                                  tags: updatedTags,
                                  uuid: spaceModel?.uuid,
                                  internalId: spaceModel?.internalId,
                                  subspaceModels: processedSubspaces)),
                        );
                      }
                    },
                  ),
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: DefaultButton(
                  borderRadius: 10,
                  backgroundColor: ColorsManager.secondaryColor,
                  foregroundColor: state.isSaveEnabled
                      ? ColorsManager.whiteColors
                      : ColorsManager.whiteColorsWithOpacity,
                  onPressed: state.isSaveEnabled
                      ? () async {
                          final updatedTags = List<Tag>.from(state.tags);

                          final result = TagHelper.updateSubspaceTagModels(
                              updatedTags, subspaces);

                          final processedTags =
                              result['updatedTags'] as List<Tag>;
                          final processedSubspaces =
                              List<SubspaceTemplateModel>.from(
                                  result['subspaces'] as List<dynamic>);

                          Navigator.of(context)
                              .popUntil((route) => route.isFirst);

                          await showDialog(
                            context: context,
                            builder: (BuildContext dialogContext) {
                              return CreateSpaceModelDialog(
                                products: products,
                                allSpaceModels: allSpaceModels,
                                allTags: allTags,
                                projectTags: projectTags,
                                pageContext: pageContext,
                                otherSpaceModels: otherSpaceModels,
                                spaceModel: SpaceTemplateModel(
                                    modelName: spaceName,
                                    tags: processedTags,
                                    uuid: spaceModel?.uuid,
                                    internalId: spaceModel?.internalId,
                                    subspaceModels: processedSubspaces),
                              );
                            },
                          );
                        }
                      : null,
                  child: const Text('Save'),
                ),
              ),
              const SizedBox(width: 10),
            ],
          );
        } else {
          return const SizedBox();
        }
      },
    );
  }
}
