import 'dart:io';

import 'package:hmg_patient_app/widgets/buttons/custom_text_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:flutter_svg/flutter_svg.dart';

void showNfcReader(BuildContext context, {required Function onNcfScan, required VoidCallback onCancel}) {
  showModalBottomSheet(
      context: context,
      enableDrag: false,
      isDismissible: true,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12)),
      ),
      backgroundColor: Colors.white,
      builder: (context) {
        return NfcLayout(
          onNcfScan: onNcfScan,
          onCancel: onCancel,
        );
      });
}

class NfcLayout extends StatefulWidget {
  Function? onNcfScan;
  VoidCallback? onCancel;

  NfcLayout({this.onNcfScan, this.onCancel});

  @override
  _NfcLayoutState createState() => _NfcLayoutState();
}

class _NfcLayoutState extends State<NfcLayout> {
  bool _reading = false;
   Widget? mainWidget;
  late String nfcId;

  @override
  void initState() {
    super.initState();
    readNFC();
  }

  void readNFC() async {
    FlutterNfcKit.finish();
    FlutterNfcKit.poll(timeout: Duration(seconds: 10), androidPlatformSound: true, androidCheckNDEF: false, iosMultipleTagMessage: "Multiple tags found!").then((value) async {
      setState(() {
        _reading = true;
        mainWidget = doneNfc();
      });
      Future.delayed(const Duration(milliseconds: 500), () async {
        await FlutterNfcKit.finish();
        widget.onNcfScan!(nfcId);
        Navigator.pop(context);
      });
      nfcId = value.id;
    }).catchError((err) {
      print(err);
      Navigator.of(context).pop();
    });
  }

  @override
  Widget build(BuildContext context) {
    // return SizedBox();
    (mainWidget == null && !_reading) ? mainWidget = scanNfc() : mainWidget = doneNfc();
    return Platform.isAndroid ? AnimatedSwitcher(duration: Duration(milliseconds: 500), child: mainWidget) : SizedBox.shrink();
  }

  Widget scanNfc() {
    return Container(
      key: ValueKey(1),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          SizedBox(
            height: 30,
          ),
          Text(
            "Ready To Scan",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 24,
            ),
          ),
          SizedBox(
            height: 30,
          ),
          SvgPicture.asset(
            "assets/images/nfc/contactless.svg",
            height: MediaQuery.of(context).size.width / 3,
          ),
          SizedBox(
            height: 30,
          ),
          Text(
            "Approach an NFC Tag",
            style: TextStyle(
              fontSize: 18,
            ),
          ),
          SizedBox(
            height: 30,
          ),
          ButtonTheme(
            minWidth: MediaQuery.of(context).size.width / 1.2,
            height: 45.0,
            buttonColor: Colors.grey[300],
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6),
            ),
            child: CustomTextButton(
              elevation: 0,
              onPressed: () {
                widget.onCancel!();
                Navigator.pop(context);
              },
              child: Text("CANCEL"),
            ),
          ),
          SizedBox(
            height: 30,
          ),
        ],
      ),
    );
  }

  Widget doneNfc() {
    return Container(
      key: ValueKey(2),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          SizedBox(
            height: 30,
          ),
          Text(
            "Successfully Scanned",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 24,
            ),
          ),
          SizedBox(
            height: 30,
          ),
          Image.asset(
            "assets/images/nfc/ic_done.png",
            height: MediaQuery.of(context).size.width / 3,
          ),
          SizedBox(
            height: 30,
          ),
          Text(
            "Approach an NFC Tag",
            style: TextStyle(
              fontSize: 18,
            ),
          ),
          SizedBox(
            height: 30,
          ),
          ButtonTheme(
            minWidth: MediaQuery.of(context).size.width / 1.2,
            height: 45.0,
            buttonColor: Colors.grey[300],
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6),
            ),
            child: CustomTextButton(
              elevation: 0,
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text("DONE"),
            ),
          ),
          SizedBox(
            height: 30,
          ),
        ],
      ),
    );
  }
}
