import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/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/offline_one_time_password_page.dart';
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_dialog.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/resource_manager/color_manager.dart';

class OnetimePasswordPage extends StatelessWidget {
  final String? deviceId;
  const OnetimePasswordPage({
    super.key,
    this.deviceId,
  });

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (BuildContext context) =>
            SmartDoorBloc(deviceId: deviceId!)..add(InitialOneTimePassword()),
        child: BlocConsumer<SmartDoorBloc, SmartDoorState>(listener: (context, state) {
          if (state is FailedState) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(state.errorMessage),
                backgroundColor: Colors.red,
              ),
            );
          }
        }, builder: (context, state) {
          final smartDoorBloc = BlocProvider.of<SmartDoorBloc>(context);
          return DefaultScaffold(
              title: 'Passwords',
              actions: [
                IconButton(
                    onPressed: () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(
                              builder: (context) => OfflineOneTimePasswordPage(
                                    deviceId: deviceId,
                                  )))
                          .then((result) {
                        if (result != null) {
                          smartDoorBloc.add(InitialOneTimePassword());
                          smartDoorBloc.add(InitialOneTimePassword());
                        }
                      });
                    },
                    icon: const Icon(Icons.add))
              ],
              child: Builder(
                builder: (context) {
                  return state is LoadingInitialState
                      ? const Center(child: CircularProgressIndicator())
                      : Center(
                          child: smartDoorBloc.oneTimePasswords!.isNotEmpty
                              ? ListView.builder(
                                  itemCount: smartDoorBloc.oneTimePasswords!.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.oneTimePasswords![index].pwdName}',
                                            fontWeight: FontWeight.normal,
                                          ),
                                          onTap: () async {
                                            await showDialog(
                                              context: context,
                                              builder: (context) {
                                                return DoorDialog(
                                                  title: 'Password Information',
                                                  offline: smartDoorBloc.oneTimePasswords![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')
                                  ],
                                ),
                        );
                },
              ));
        }));
  }
}
