import 'dart:io';

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';

class QRPage extends StatefulWidget {
  @override
  _QRPageState createState() => _QRPageState();
}

class _QRPageState extends State<QRPage> {
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  final GlobalKey<ScaffoldState> _scaffoldkey = GlobalKey<ScaffoldState>();
  Barcode? result;
  QRViewController? controller;
  bool _flash = false;

  // In order to get hot reload to work we need to pause the camera if the platform
  // is android, or resume the camera if the platform is iOS.
  @override
  void reassemble() {
    super.reassemble();
    if (Platform.isAndroid) {
      controller!.pauseCamera();
    } else if (Platform.isIOS) {
      controller!.resumeCamera();
    }
  }

  @override
  Widget build(BuildContext context) {
    final _screenSize = MediaQuery.of(context).size;
    return Scaffold(
      key: _scaffoldkey,
      body: Column(
        children: [
          Expanded(
            child: Stack(
              children: [
                QRView(
                  key: qrKey,
                  onQRViewCreated: _onQRViewCreated,
                  overlay: QrScannerOverlayShape(
                    borderColor: Colors.deepPurpleAccent,
                    borderRadius: 8.0,
                    borderLength: 32.0,
                    borderWidth: 8.0,
                    cutOutSize: _screenSize.width < 400 ? 250.0 : 350.0
                  ),
                ),
                Positioned(
                  left: 8.0,
                  child: SafeArea(
                    child: TextButton(
                      child: Text(
                        "Cancelar",
                        style: GoogleFonts.montserrat(
                          fontSize: 14.0,
                          fontWeight: FontWeight.bold,
                          color: Colors.deepPurpleAccent,
                        ),
                      ),
                      onPressed: () => Navigator.pop(context, ""),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(
              horizontal: 16.0,
              vertical: 16.0
            ),
            width: _screenSize.width,
            child: Column(
              children: [
                Row(
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        child: SizedBox(
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                _flash ? "Apagar flash" : "Encender flash",
                                style: GoogleFonts.montserrat(
                                  color: Colors.deepPurpleAccent,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14.0,
                                ),
                              ),
                              const SizedBox(
                                width: 4.0,
                              ),
                              Icon(
                                _flash ? Icons.flash_off : Icons.flash_on,
                                color: Colors.deepPurpleAccent,
                              )
                            ],
                          ),
                        ),
                        onPressed: () async{
                          await controller!.toggleFlash();
                          setState(() => _flash = !_flash);
                        },
                        style: ElevatedButton.styleFrom(
                          primary: Colors.white54,
                          padding: EdgeInsets.zero
                        ),
                      ),
                    ),
                    const SizedBox(
                      width: 16.0,
                    ),
                    Expanded(
                      child: ElevatedButton(
                        child: SizedBox(
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                "Cambiar cámara",
                                style: GoogleFonts.montserrat(
                                  color: Colors.deepPurpleAccent,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14.0,
                                ),
                              ),
                              const SizedBox(
                                width: 4.0,
                              ),
                              const Icon(
                                Icons.switch_camera,
                                color: Colors.deepPurpleAccent,
                              )
                            ],
                          ),
                        ),
                        onPressed: () async => await controller!.flipCamera(),
                        style: ElevatedButton.styleFrom(
                          primary: Colors.white54,
                          padding: EdgeInsets.zero
                        ),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      if(scanData.code != null) Navigator.pop(_scaffoldkey.currentContext!, scanData.code!);
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}
