import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class AppBarHomeDropdown extends StatelessWidget {
  const AppBarHomeDropdown({super.key});

  @override
  Widget build(BuildContext context) {
    return HomeCubit.getInstance().spaces != null || HomeCubit.getInstance().spaces!.isNotEmpty
        ? BlocBuilder<HomeCubit, HomeState>(
            builder: (context, state) {
              return Padding(
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: DropdownButton(
                  icon: const Icon(
                    Icons.expand_more,
                    color: ColorsManager.textPrimaryColor,
                    size: 16,
                  ),
                  underline: const SizedBox.shrink(),
                  padding: EdgeInsets.zero,
                  borderRadius: BorderRadius.circular(20),
                  value: HomeCubit.getInstance().selectedSpace?.id ?? '',
                  items: HomeCubit.getInstance().spaces!.map((space) {
                    return DropdownMenuItem(
                      alignment: AlignmentDirectional.centerStart,
                      value: space.id,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          SvgPicture.asset(
                            Assets.assetsIconsHome,
                            width: 25,
                            height: 25,
                            colorFilter: const ColorFilter.mode(
                              ColorsManager.textPrimaryColor,
                              BlendMode.srcIn,
                            ),
                          ),
                          const SizedBox(width: 5),
                          Flexible(
                            child: BodyMedium(
                              text: space.name,
                              style: context.bodyMedium.copyWith(
                                fontSize: 15,
                                color: ColorsManager.textPrimaryColor,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ),
                          const SizedBox(width: 5),
                        ],
                      ),
                    );
                  }).toList(),
                  onChanged: (value) {
                    if (value != null) {
                      HomeCubit.getInstance().changeSelectedSpace(HomeCubit.getInstance()
                          .spaces!
                          .firstWhere((element) => element.id == value));
                    }
                  },
                ),
              );
            },
          )
        : Container();
  }
}
