import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_event.dart';
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_state.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/offline_timeLimit_password_page.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.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/generated/assets.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'door_dialog.dart';

class TimeLimitedPasswordPage extends StatelessWidget {
  final String? deviceId;
  const TimeLimitedPasswordPage({super.key, this.deviceId});
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (BuildContext context) =>
            SmartDoorBloc(deviceId: deviceId!)..add(InitialTimeLimitPassword()),
        child: BlocConsumer<SmartDoorBloc, SmartDoorState>(listener: (context, state) {
          if (state is FailedState) {
            CustomSnackBar.displaySnackBar(state.errorMessage);
          }
        }, builder: (context, state) {
          final smartDoorBloc = BlocProvider.of<SmartDoorBloc>(context);
          return DefaultScaffold(
              title: 'Passwords',
              actions: [
                IconButton(
                    onPressed: () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(
                              builder: (context) => CreateOfflineTimeLimitPasswordPage(
                                    deviceId: deviceId,
                                  )))
                          .then((result) {
                        smartDoorBloc.add(InitialTimeLimitPassword());
                        smartDoorBloc.add(InitialTimeLimitPassword());
                      });
                    },
                    icon: const Icon(Icons.add))
              ],
              child: Builder(
                builder: (context) {
                  return state is LoadingInitialState
                      ? const Center(child: CircularProgressIndicator())
                      : Center(
                          child: smartDoorBloc.timeLimitPasswords!.isNotEmpty
                              ? ListView.builder(
                                  itemCount: smartDoorBloc.timeLimitPasswords!.length,
                                  itemBuilder: (context, index) {
                                    return Padding(
                                      padding: const EdgeInsets.all(5.0),
                                      child: DefaultContainer(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 15, vertical: 10),
                                        child: ListTile(
                                          contentPadding: EdgeInsets.zero,
                                          leading: SvgPicture.asset(Assets.timeLimitedPasswordIcon),
                                          title: BodyMedium(
                                            text:
                                                'Password Name: ${smartDoorBloc.timeLimitPasswords![index].pwdName}',
                                            fontWeight: FontWeight.normal,
                                          ),
                                          onTap: () async {
                                            await showDialog(
                                              context: context,
                                              builder: (context) {
                                                return DoorDialog(
                                                  title: 'Password Information',
                                                  offline: smartDoorBloc.timeLimitPasswords![index],
                                                );
                                              },
                                            );
                                          },
                                          trailing: const Icon(
                                            Icons.arrow_forward_ios,
                                            color: ColorsManager.greyColor,
                                            size: 15,
                                          ),
                                        ),
                                      ),
                                    );
                                  },
                                )
                              : Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    SvgPicture.asset(Assets.noValidPasswords),
                                    const SizedBox(
                                      height: 10,
                                    ),
                                    const BodyMedium(text: 'No Valid Passwords')
                                  ],
                                ),
                        );
                },
              ));
        }));
  }
}
