import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/bloc/subspace_model_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/bloc/subspace_model_event.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/bloc/subspace_model_state.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/widgets/create_subspace_model_chips_box.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/widgets/create_subspace_model_footer_buttons.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class CreateSubSpaceModelDialog extends StatelessWidget {
  final bool isEdit;
  final String dialogTitle;
  final List<SubspaceTemplateModel>? existingSubSpaces;
  final void Function(List<SubspaceTemplateModel> newSubspaces)? onUpdate;

  const CreateSubSpaceModelDialog({
    required this.isEdit,
    required this.dialogTitle,
    this.existingSubSpaces,
    this.onUpdate,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;

    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),
      ),
      child: BlocProvider(
        create: (context) {
          final bloc = SubSpaceModelBloc();
          if (existingSubSpaces != null) {
            for (final subSpace in existingSubSpaces ?? []) {
              bloc.add(AddSubSpaceModel(subSpace));
            }
          }
          return bloc;
        },
        child: BlocBuilder<SubSpaceModelBloc, SubSpaceModelState>(
          builder: (context, state) => Container(
            color: ColorsManager.whiteColors,
            width: screenWidth * 0.3,
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  dialogTitle,
                  style: context.textTheme.headlineLarge?.copyWith(
                    color: ColorsManager.blackColor,
                  ),
                ),
                const SizedBox(height: 16),
                CreateSubspaceModelChipsBox(subSpaces: state.subSpaces),
                if (state.errorMessage.isNotEmpty)
                  Padding(
                    padding: const EdgeInsets.only(bottom: 16),
                    child: Text(
                      state.errorMessage,
                      style: context.textTheme.bodySmall?.copyWith(
                        color: ColorsManager.red,
                      ),
                    ),
                  ),
                const SizedBox(height: 16),
                CreateSubspaceModelFooterButtons(
                  onUpdate: onUpdate,
                  errorMessage: state.errorMessage,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
