import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/community_structure_header_action_button.dart';
import 'package:syncrow_web/pages/spaces_management/create_community/view/create_community_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';

class CommunityStructureHeader extends StatefulWidget {
  final String? communityName;
  final bool isEditingName;
  final bool isSave;
  final TextEditingController nameController;
  final VoidCallback onSave;
  final VoidCallback onDelete;
    final VoidCallback onEdit;
  final VoidCallback onDuplicate;

  final VoidCallback onEditName;
  final ValueChanged<String> onNameSubmitted;
  final List<CommunityModel> communities;
  final CommunityModel? community;
  final SpaceModel? selectedSpace;

  const CommunityStructureHeader(
      {super.key,
      required this.communityName,
      required this.isSave,
      required this.isEditingName,
      required this.nameController,
      required this.onSave,
      required this.onDelete,
      required this.onEditName,
      required this.onNameSubmitted,
      this.community,
      required this.communities,
      this.selectedSpace,
      required this.onDuplicate,
      required this.onEdit});

  @override
  State<CommunityStructureHeader> createState() =>
      _CommunityStructureHeaderState();
}

class _CommunityStructureHeaderState extends State<CommunityStructureHeader> {
  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final screenWidth = MediaQuery.of(context).size.width;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
      decoration: BoxDecoration(
        color: ColorsManager.whiteColors,
        boxShadow: [
          BoxShadow(
            color: ColorsManager.shadowBlackColor,
            blurRadius: 8,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: _buildCommunityInfo(theme, screenWidth),
              ),
              const SizedBox(width: 16),
            ],
          ),
        ],
      ),
    );
  }

  void _showCreateCommunityDialog(BuildContext parentContext) {
    showDialog(
      context: parentContext,
      builder: (context) => CreateCommunityDialog(
        isEditMode: true,
        existingCommunityNames:
            widget.communities.map((community) => community.name).toList(),
        initialName: widget.community?.name ?? '',
        onCreateCommunity: (String communityName, String description) {
          widget.onNameSubmitted(communityName);
        },
      ),
    );
  }

  Widget _buildCommunityInfo(ThemeData theme, double screenWidth) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Community Structure',
          style: theme.textTheme.headlineLarge
              ?.copyWith(color: ColorsManager.blackColor),
        ),
        if (widget.communityName != null)
          Row(
            children: [
              Expanded(
                child: Row(
                  children: [
                    if (!widget.isEditingName)
                      Flexible(
                        child: Text(
                          widget.communityName!,
                          style: theme.textTheme.bodyLarge
                              ?.copyWith(color: ColorsManager.blackColor),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      ),
                    if (widget.isEditingName)
                      SizedBox(
                        width: screenWidth * 0.1,
                        child: TextField(
                          controller: widget.nameController,
                          decoration: const InputDecoration(
                            border: InputBorder.none,
                            isDense: true,
                          ),
                          style: theme.textTheme.bodyLarge
                              ?.copyWith(color: ColorsManager.blackColor),
                          onSubmitted: widget.onNameSubmitted,
                        ),
                      ),
                    const SizedBox(width: 2),
                    GestureDetector(
                      onTap: () => _showCreateCommunityDialog(context),
                      child: SvgPicture.asset(
                        Assets.iconEdit,
                        width: 16,
                        height: 16,
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(width: 8),
              CommunityStructureHeaderActionButtons(
                theme: theme,
                isSave: widget.isSave,
                onSave: widget.onSave,
                onDelete: widget.onDelete,
                onDuplicate: widget.onDuplicate,
                onEdit: widget.onEdit,
                selectedSpace: widget.selectedSpace,
              ),
            ],
          ),
      ],
    );
  }
}
