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/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';
import 'create_temporary_password.dart';
import 'door_dialog.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (BuildContext context) => SmartDoorBloc(deviceId: deviceId!)..add(InitialPasswordsPage( )),
        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) =>
                                  CreateTemporaryPassword(deviceId: deviceId, type: type))).then((result) {
                            if (result != null && result) {
                              smartDoorBloc.add(InitialPasswordsPage());
                              smartDoorBloc.add(InitialPasswordsPage());
                            }
                          });
                        },
                        icon: const Icon(Icons.add)
                    )
                  ],
                  child: Builder(
                    builder: (context) {
                      return state is LoadingInitialState
                          ? const Center(child: CircularProgressIndicator())
                          : Center(
                        child: smartDoorBloc.temporaryPasswords!.isNotEmpty
                            ? ListView.builder(
                          itemCount: smartDoorBloc.temporaryPasswords!.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.temporaryPasswords![index].name}',
                                    fontWeight: FontWeight.normal,
                                  ),
                                  onTap: () async {
                                    final result = await showDialog(
                                      context: context,
                                      builder: (context) {
                                        return DoorDialog(
                                          title: 'Password Information',
                                          temporaryPassword: smartDoorBloc.temporaryPasswords![index],
                                        );
                                      },
                                    );
                                    if(result=='delete'){
                                      smartDoorBloc.add(DeletePasswordEvent(passwordId: smartDoorBloc.temporaryPasswords![index].id.toString()));
                                    }
                                  },
                                  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')
                          ],
                        ),
                      );
                    },
                  ));
            })
    );
  }
}
