import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/subspace_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/tag.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/utils/color_manager.dart';

import '../../../add_device_type/views/add_device_type_widget.dart';
import '../../../helper/tag_helper.dart';

class SaveAddDeviceRowWidget extends StatelessWidget {
  const SaveAddDeviceRowWidget({
    super.key,
    required this.subspaces,
    required this.products,
    required this.projectTags,
    required this.spaceName,
    required this.onSave,
    required this.allTags,
    required this.tags,
    required this.isSaveEnabled,
  });
  final List<Tag> tags;
  final List<SubspaceModel>? subspaces;
  final List<ProductModel>? products;
  final List<Tag> projectTags;
  final String spaceName;
  final Function(List<Tag> p1, List<SubspaceModel>? p2)? onSave;
  final List<String>? allTags;
  final bool isSaveEnabled;
  @override
  Widget build(BuildContext context) {
    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(tags);
                final result = TagHelper.processTags(updatedTags, subspaces);

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

                Navigator.of(context).pop();

                await showDialog(
                  context: context,
                  builder: (context) => AddDeviceTypeWidget(
                    products: products,
                    subspaces: processedSubspaces,
                    projectTags: projectTags,
                    initialSelectedProducts:
                        TagHelper.createInitialSelectedProductsForTags(
                            processedTags, processedSubspaces),
                    spaceName: spaceName,
                    spaceTags: processedTags,
                    isCreate: false,
                    onSave: onSave,
                    allTags: allTags,
                  ),
                );
              },
            ),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: DefaultButton(
            borderRadius: 10,
            backgroundColor: ColorsManager.secondaryColor,
            foregroundColor: isSaveEnabled
                ? ColorsManager.whiteColors
                : ColorsManager.whiteColorsWithOpacity,
            onPressed: isSaveEnabled
                ? () async {
                    final updatedTags = List<Tag>.from(tags);
                    final result =
                        TagHelper.processTags(updatedTags, subspaces);

                    final processedTags = result['updatedTags'] as List<Tag>;
                    final processedSubspaces = List<SubspaceModel>.from(
                        result['subspaces'] as List<dynamic>);
                    onSave?.call(processedTags, processedSubspaces);
                    Navigator.of(context).pop();
                  }
                : null,
            child: const Text('Save'),
          ),
        ),
        const SizedBox(width: 10),
      ],
    );
  }
}
