import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/create_subspace/bloc/subspace_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace/bloc/subspace_event.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace/bloc/subspace_state.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

import 'widgets/ok_cancel_sub_space_widget.dart';
import 'widgets/textfield_sub_space_dialog_widget.dart';

class CreateSubSpaceDialog extends StatefulWidget {
  final String dialogTitle;
  final List<SubspaceModel>? existingSubSpaces;
  final String? spaceName;
  final List<ProductModel>? products;
  final void Function(
      List<SubspaceModel>?, List<UpdateSubspaceModel> updatedSubSpaces)? onSave;

  const CreateSubSpaceDialog({
    required this.dialogTitle,
    required this.spaceName,
    required this.products,
    required this.onSave,
    this.existingSubSpaces,
    super.key,
  });

  @override
  State<CreateSubSpaceDialog> createState() => _CreateSubSpaceDialogState();
}

class _CreateSubSpaceDialogState extends State<CreateSubSpaceDialog> {
  final TextEditingController _subspaceNameController = TextEditingController();
  @override
  void dispose() {
    _subspaceNameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) {
        final bloc = SubSpaceBloc();
        if (widget.existingSubSpaces != null) {
          for (final subSpace in widget.existingSubSpaces ?? []) {
            bloc.add(AddSubSpace(subSpace));
          }
        }
        return bloc;
      },
      child: Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        child: BlocBuilder<SubSpaceBloc, SubSpaceState>(
          builder: (context, state) {
            return Container(
              width: context.screenWidth * 0.35,
              color: ColorsManager.whiteColors,
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    widget.dialogTitle,
                    style: context.textTheme.headlineLarge?.copyWith(
                      color: ColorsManager.blackColor,
                    ),
                  ),
                  Row(
                    children: [
                      Text(
                        'press Enter to Save',
                        style: context.textTheme.headlineLarge?.copyWith(
                          fontWeight: FontWeight.w500,
                          fontSize: 10,
                          color: ColorsManager.grayColor,
                        ),
                      ),
                      const SizedBox(
                        width: 5,
                      ),
                      const Icon(Icons.save_as_sharp, size: 10),
                    ],
                  ),
                  const SizedBox(height: 16),
                  TextFieldSubSpaceDialogWidget(
                    subspaceNameController: _subspaceNameController,
                    subSpaces: state.subSpaces,
                  ),
                  if (state.errorMessage.isNotEmpty)
                    Padding(
                      padding: const EdgeInsets.only(top: 8.0),
                      child: Text(
                        state.errorMessage,
                        style: context.textTheme.bodySmall?.copyWith(
                          color: ColorsManager.warningRed,
                        ),
                      ),
                    ),
                  const SizedBox(height: 16),
                  OkCancelSubSpaceWidget(
                    subspaceNameController: _subspaceNameController,
                    errorMessage: state.errorMessage,
                    onSave: widget.onSave,
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
