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 TimeZoneScreenPage extends StatelessWidget {
  const TimeZoneScreenPage({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
     create: (BuildContext context) => ProfileBloc()..add(TimeZoneInitialEvent()),
      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 timeZoneList = state is TimeZoneLoadedState ?state.timezone : [];
     return DefaultScaffold(
       padding: const EdgeInsets.all(0),
       title: 'Time Zone',
       child: state is LoadingInitialState
       ? const Center(child: CircularProgressIndicator())
        : Column(
       children: [
        TextFormField(
          controller:profileBloc.searchController ,
          onChanged: (value) {
            profileBloc.add(SearchTimeZoneEvent(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: timeZoneList.length,
                   itemBuilder: (context, index) {
                     return   InkWell(
                       onTap: () {
                         profileBloc.add(SelectTimeZoneEvent(val: timeZoneList[index].id, context: context));
                       },
                       child: Column(
                         crossAxisAlignment: CrossAxisAlignment.center,
                         mainAxisAlignment: MainAxisAlignment.center,
                         children: [
                           Padding(
                             padding: const EdgeInsets.symmetric(horizontal: 10,),
                             child: SizedBox(
                               height: 45,
                               child: ListTile(
                                 contentPadding: EdgeInsets.zero,
                                 trailing: BodyMedium(
                                   text: timeZoneList[index].offset,
                                   fontSize: 13,
                                   fontColor: ColorsManager.textGray,),
                                 leading: BodyMedium(
                                   fontSize: 13,
                                   text: timeZoneList[index].name,),),
                             ),
                           ),
                           const Divider(color: ColorsManager.textGray), // Divider between items
                         ],
                       ),
                     );
                     },
                 )),
               ),
             ),
           ],
         ),
       );
    })
    );
  }
}
