import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/pages/spaces_management/create_subspace_model/bloc/subspace_model_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class CreateSubspaceModelFooterButtons extends StatelessWidget {
  const CreateSubspaceModelFooterButtons({
    required this.onUpdate,
    required this.errorMessage,
    super.key,
  });

  final void Function(List<SubspaceTemplateModel> newSubspaces)? onUpdate;
  final String errorMessage;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: CancelButton(
            label: 'Cancel',
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: DefaultButton(
            onPressed: errorMessage.isEmpty
                ? () {
                    Navigator.of(context).pop();
                    if (onUpdate != null) {
                      final subSpaces =
                          context.read<SubSpaceModelBloc>().state.subSpaces;
                      onUpdate!(subSpaces);
                    }
                  }
                : null,
            backgroundColor: ColorsManager.secondaryColor,
            borderRadius: 10,
            foregroundColor: errorMessage.isNotEmpty
                ? ColorsManager.whiteColorsWithOpacity
                : ColorsManager.whiteColors,
            child: const Text('OK'),
          ),
        ),
      ],
    );
  }
}
