import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_event.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_state.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class RegionPage extends StatelessWidget {
  const RegionPage({super.key});
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (BuildContext context) => ProfileBloc()..add(RegionInitialEvent()),
        child: BlocConsumer<ProfileBloc, ProfileState>(listener: (context, state) {
          if (state is FailedState) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(state.errorMessage),
                backgroundColor: Colors.red,
              ),
            );
          }
          if (state is SaveState) {
            Navigator.of(context).pop(true);
          }
        },
        builder: (context, state) {
          final profileBloc = BlocProvider.of<ProfileBloc>(context);
          final regionList = state is RegionsLoadedState ? state.regions : [];
          return DefaultScaffold(
            padding: const EdgeInsets.all(0),
            title: 'Region',
            child: state is LoadingInitialState
                ? const Center(child: CircularProgressIndicator())
                : Column(
              children: [
                TextFormField(
                  controller:profileBloc.searchController ,
                  onChanged: (value) {
                    profileBloc.add(SearchRegionEvent(query: value));
                  },
                  decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.search),
                    hintText: 'Search',
                    fillColor: ColorsManager.textGray,
                  ),
                ),
                const SizedBox(height: 10),
                Expanded(
                  child: Container(
                    decoration: const BoxDecoration(
                      color: ColorsManager.onPrimaryColor,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20),
                        topRight: Radius.circular(20),
                      ),
                    ),
                    child:Padding(
                      padding: const EdgeInsets.only(bottom: 10.0,top: 10.0,left: 15,right: 15),
                      child:  ListView.builder(
                      itemCount: regionList.length,
                      itemBuilder: (context, index) {
                        return   InkWell(
                          onTap: () {
                            profileBloc.add(SelectRegionEvent(val: regionList[index].id, context: context));
                          },
                          child:Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.symmetric(horizontal: 10,),
                                  child: SizedBox(
                                    child: ListTile(
                                      contentPadding: EdgeInsets.zero,
                                      leading: BodyMedium(
                                        fontSize: 15,
                                        text: regionList[index].name,
                                      ),
                                    ),
                                  ),
                                ),
                                const Divider(color: ColorsManager.textGray), // Divider between items
                              ],
                            ),
                        );
                      },
                    )),
                  ),
                ),
              ],
            ),
          );
        }));
  }
}
