import 'package:flutter/material.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/utilities/extensions.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';
import 'package:provider/provider.dart';
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';

class KioskQrScannerScreen extends StatelessWidget {
  KioskQrScannerScreen({super.key});

  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

  @override
  Widget build(BuildContext context) {
    final screenConfigViewModel = context.read<ScreenConfigViewModel>();
    final size = MediaQuery.of(context).size;
    final scanArea = (size.width < size.height ? size.width : size.height) * 0.6;
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: [
            Column(
              children: <Widget>[
                Expanded(
                  flex: 9,
                  child: QRView(
                      key: qrKey,
                      overlay: QrScannerOverlayShape(
                        borderColor: AppColors.redColor,
                        borderRadius: 10,
                        borderLength: 30,
                        borderWidth: 10,
                        cutOutSize: scanArea,
                      ),
                      onQRViewCreated: (controller) {
                        screenConfigViewModel.onQRViewCreated(
                          controller: controller,
                          onSuccess: () => context.popScreen(),
                          onFailure: () {},
                        );
                      }),
                ),
              ],
            ),
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Container(
                color: Colors.black.withOpacity(0.4),
                padding: const EdgeInsets.all(16),
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      GestureDetector(
                        onTap: () => context.popScreen(),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Icon(
                              Icons.arrow_back,
                              color: AppColors.whiteColor,
                              size: SizeConfig.getHeightMultiplier() * 0.3,
                            ),
                            const SizedBox(width: 8),
                            Text(
                              'Back',
                              style: TextStyle(
                                color: AppColors.whiteColor,
                                fontSize: SizeConfig.getHeightMultiplier() * 0.3,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ],
                        ),
                      ),
                      GestureDetector(
                        onTap: () => screenConfigViewModel.flipCamera(),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Icon(
                              Icons.flip_camera_ios_outlined,
                              color: AppColors.whiteColor,
                              size: SizeConfig.getHeightMultiplier() * 0.4,
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Positioned(
              bottom: SizeConfig.getHeightMultiplier() * 0.5,
              left: 0,
              right: 0,
              child: Center(
                child: Consumer<ScreenConfigViewModel>(
                  builder: (BuildContext context, ScreenConfigViewModel screenConfigViewModel, _) {
                    String text = "";

                    if (screenConfigViewModel.qrCodeResult != null) {
                      final code = screenConfigViewModel.qrCodeResult?.code;

                      int? patientId = int.tryParse(code ?? '');

                      if (patientId != null && patientId > 0) {
                        text = "Patient ID: $patientId\nGenerating Ticket...";
                      } else {
                        text = "Invalid QR Code. Please scan a valid patient ID.";
                      }
                    } else {
                      text = "Please scan your Patient QR from Sulaiman Al Habib Mobile App";
                    }

                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        text,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: AppColors.whiteColor,
                          fontSize: SizeConfig.getHeightMultiplier() * 0.3,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
