import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_community/bloc/community_dialog_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_community/bloc/community_dialog_event.dart';
import 'package:syncrow_web/pages/spaces_management/create_community/bloc/community_dialog_state.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart';

class CreateCommunityDialog extends StatelessWidget {
  final Function(String name, String description) onCreateCommunity;
  final List<String> existingCommunityNames;
  final bool isEditMode;
  final String? initialName;

  const CreateCommunityDialog({
    super.key,
    required this.onCreateCommunity,
    required this.existingCommunityNames,
    required this.isEditMode,
    this.initialName,
  });

  @override
  Widget build(BuildContext context) {
    final nameController =
        TextEditingController(text: isEditMode ? initialName : '');

    return BlocProvider(
      create: (_) => CommunityDialogBloc(existingCommunityNames),
      child: Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        backgroundColor: ColorsManager.transparentColor,
        child: Stack(
          children: [
            Container(
              width: MediaQuery.of(context).size.width * 0.3,
              padding: const EdgeInsets.all(20),
              decoration: BoxDecoration(
                color: ColorsManager.whiteColors,
                borderRadius: BorderRadius.circular(20),
                boxShadow: [
                  BoxShadow(
                    color: ColorsManager.blackColor.withOpacity(0.25),
                    blurRadius: 20,
                    spreadRadius: 5,
                    offset: const Offset(0, 5),
                  ),
                ],
              ),
              child: SingleChildScrollView(
                child: BlocBuilder<CommunityDialogBloc, CommunityDialogState>(
                  builder: (context, state) {
                    bool isNameValid = true;
                    bool isNameEmpty = false;

                    if (state is CommunityNameValidationState) {
                      isNameValid = state.isNameValid;
                      isNameEmpty = state.isNameEmpty;
                    }

                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          isEditMode ? 'Edit Community Name' : 'Community Name',
                          style: Theme.of(context).textTheme.headlineMedium,
                        ),
                        const SizedBox(height: 18),
                        TextField(
                          controller: nameController,
                          onChanged: (value) {
                            context
                                .read<CommunityDialogBloc>()
                                .add(ValidateCommunityNameEvent(value));
                          },
                          style: Theme.of(context).textTheme.bodyMedium,
                          decoration: InputDecoration(
                            hintText: 'Please enter the community name',
                            filled: true,
                            fillColor: ColorsManager.boxColor,
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                color: isNameValid && !isNameEmpty
                                    ? ColorsManager.boxColor
                                    : ColorsManager.red,
                                width: 1,
                              ),
                              borderRadius: BorderRadius.circular(10),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                              borderSide: const BorderSide(
                                color: ColorsManager.boxColor,
                                width: 1.5,
                              ),
                            ),
                          ),
                        ),
                        if (!isNameValid)
                          Padding(
                            padding: const EdgeInsets.only(top: 8.0),
                            child: Text(
                              '*Name already exists.',
                              style: Theme.of(context)
                                  .textTheme
                                  .bodySmall
                                  ?.copyWith(color: ColorsManager.red),
                            ),
                          ),
                        if (isNameEmpty)
                          Padding(
                            padding: const EdgeInsets.only(top: 8.0),
                            child: Text(
                              '*Name should not be empty.',
                              style: Theme.of(context)
                                  .textTheme
                                  .bodySmall
                                  ?.copyWith(color: ColorsManager.red),
                            ),
                          ),
                        const SizedBox(height: 24),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              child: CancelButton(
                                label: 'Cancel',
                                onPressed: () => Navigator.of(context).pop(),
                              ),
                            ),
                            const SizedBox(width: 16),
                            Expanded(
                              child: DefaultButton(
                                onPressed: () {
                                  if (isNameValid && !isNameEmpty) {
                                    onCreateCommunity(
                                      nameController.text.trim(),
                                      "",
                                    );
                                    Navigator.of(context).pop();
                                  }
                                },
                                backgroundColor: isNameValid && !isNameEmpty
                                    ? ColorsManager.secondaryColor
                                    : ColorsManager.lightGrayColor,
                                borderRadius: 10,
                                foregroundColor: ColorsManager.whiteColors,
                                child: const Text('OK'),
                              ),
                            ),
                          ],
                        ),
                      ],
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
