import 'package:comwell_key_app/common/components/round_icon_button.dart';
import 'package:comwell_key_app/key/bloc/key_bloc.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:focus_detector/focus_detector.dart';
import 'package:go_router/go_router.dart';
import 'package:lottie/lottie.dart';

class KeyPage extends StatelessWidget {
  final String roomNumber;
  
  const KeyPage({super.key, required this.roomNumber});

  @override
  Widget build(BuildContext context) {
    context.read<KeyBloc>().add(const StartScanning());
    return BlocBuilder<KeyBloc, KeyState>(builder: (context, state) {
      
      //if (state.status == KeyStatus.scanning || state.status != KeyStatus.openClosestReaderSuccess) {
      return FocusDetector(
        child:  Scaffold(
            backgroundColor: colorTertiary,
            body: Padding(
              padding: const EdgeInsets.fromLTRB(8, 60, 10, 32),
              child: Column(
                children: [
                  Stack(
                    children: [
                      Center(
                        heightFactor: 2,
                        child: Text(
                          context.strings.open_room,
                          style: Theme.of(context)
                              .textTheme
                              .headlineMedium
                              ?.copyWith(color: Colors.white),
                        ),
                      ),
                      Align(
                        alignment: AlignmentDirectional.centerEnd,
                        child: RoundIconButton(
                          color: Colors.white,
                          onPressed: () {
                            context.pop();
                          },
                          icon: "assets/icons/close-icon.svg",
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 8),
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        Image.asset(
                          'assets/images/room_key.png',
                          fit: BoxFit.cover,
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 6),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.white, width: 2),
                            borderRadius: BorderRadius.circular(96),
                          ),
                          child: Text(
                            context.strings.key_page_room_prefix(roomNumber),
                            style: Theme.of(context).textTheme.displayLarge?.copyWith(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 36,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 20),
                  Lottie.asset(
                    'assets/animations/scan.json',
                    height: 64,
                    width: 64,
                    fit: BoxFit.cover,
                    animate: true,
                  ),
                  const SizedBox(height: 10),
                  _buildStatusText(state.status, context),
                  const Spacer(),
                  Image.asset(
                    'assets/images/seos_by_assa_logo.png',
                    height: 42,
                    fit: BoxFit.cover,
                  ),
                  const SizedBox(height: 40),
                ],
              ),
            ),
          ),
        
        onFocusGained: () {
          context.read<KeyBloc>().add(SetRootOpeningTrigger());
        },
        onFocusLost: () {},
      );
    } /* else if(state.status == KeyStatus.openClosestReaderSuccess) {
           context.pop(); // Go back to the previous page and stop scanning
        }else if (state.status == KeyStatus.searchForKeysError) {
          return Container( 
            color: Theme.of(context).colorScheme.primary,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text('error_opening_door'.tr(), style: Theme.of(context).textTheme.displayLarge, textAlign: TextAlign.center),
              const SizedBox(height: 100),
              ElevatedButton(
                onPressed: () {
                  context.read<KeyBloc>().add(StartScanning(state.key!));
                },
                child: Text('try_again'.tr(), style: Theme.of(context).textTheme.headlineSmall),
              ),
              const SizedBox(height: 300),
            ],
          ));
        } 
        return const SizedBox(); */
        // },
        );
  }

  Widget _buildStatusText(KeyStatus status, BuildContext context) {
    if (status == KeyStatus.scanning) {
      return Text(context.strings.hold_phone_to_door,
          style: Theme.of(context)
              .textTheme
              .headlineMedium
              ?.copyWith(color: Colors.white));
    } else if (status == KeyStatus.scanningError) {
      return Text(context.strings.open_room_error,
          style: Theme.of(context)
              .textTheme
              .headlineMedium
              ?.copyWith(color: Colors.white));
    } else if (status == KeyStatus.openClosestReaderSuccess) {
      return Text(context.strings.open_room_success,
          style: Theme.of(context)
              .textTheme
              .headlineMedium
              ?.copyWith(color: Colors.white));
    } else {
      return Text(context.strings.hold_phone_to_door,
          style: Theme.of(context)
              .textTheme
              .headlineMedium
              ?.copyWith(color: Colors.white));
    }
  }
}