import 'package:doctor/common/settings.dart';
import 'package:doctor/patient_dashboard/patient_dshboard_details.dart';
import 'package:flutter/material.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
import 'package:flutter/services.dart';

class PatientDashboard extends StatefulWidget {
  const PatientDashboard({Key? key}) : super(key: key);

  @override
  State<PatientDashboard> createState() => _PatientDashboardState();
}

class _PatientDashboardState extends State<PatientDashboard> {

  bool isScannerVisible=true;
  TextEditingController mobileNumberController = TextEditingController();
  final List<String> images = [
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
    'https://via.placeholder.com/150',
  ];


  Future<void> scanQR() async {
    String barcodeScanRes;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
          '#ff6666', 'Cancel', true, ScanMode.QR);
      print(barcodeScanRes);
      setState(() {

      });
    } on PlatformException {
      barcodeScanRes = 'Failed to get platform version.';
    }}


  Future<bool> onWillPop() async {
    final shouldPop = await showDialog<bool>(context: context, builder: (context) {
      return AlertDialog(
        title: const Text('Do you want to leave this page',
            style: TextStyle(
              color: primaryColor,
              fontSize: 20,
            )),
        actionsAlignment: MainAxisAlignment.spaceBetween,
        actions: [
          TextButton(
            onPressed: () {
              Navigator.of(context).pop(true);
            },
            child: const Text('Yes',
                style: TextStyle(
                  color: primaryColor,
                  fontSize: 20,
                )),
          ),
          TextButton(
            onPressed: () {
              Navigator.of(context).pop(false);
            },
            child: const Text('No',
                style: TextStyle(
                  color: primaryColor,
                  fontSize: 20,
                )),
          ),
        ],
      );
    },
    );
    return shouldPop!;
  }
  @override
  Widget build(BuildContext context) {
    return   WillPopScope(
        onWillPop: () async => onWillPop(),
      child: Scaffold(
          appBar: AppSettings.appBar('Patient Dashboard'),
          body: SafeArea(
              child: SingleChildScrollView(
                  child: Padding(
                      padding: EdgeInsets.all(10),
                      child: Column(
                          children: <Widget>[
                            SizedBox(
                              height:MediaQuery.of(context).size.height * .02,
                            ),
                            Visibility(
                              visible: isScannerVisible,
                              child:Center(
                                child:  Container(

                                    child: Column(
                                      children: [
                                        IconButton(
                                          onPressed: (){
                                            scanQR();
                                          },
                                          icon: Icon(
                                            Icons.qr_code_scanner,
                                            color: primaryColor,
                                          ),
                                        ),
                                        Text('Scan Qr Code',style: TextStyle(fontSize: 12,color: primaryColor),)
                                      ],
                                    )
                                ),
                              ),
                            ),
                            SizedBox(height:MediaQuery.of(context).size.height * .02,),
                            Container(
                              child: TextFormField(
                                cursorColor: primaryColor,
                                controller: mobileNumberController,
                                keyboardType: TextInputType.number,
                                textCapitalization: TextCapitalization.characters,
                                decoration: textFormFieldDecorationInsideApp(Icons.phone,'Enter mobile number'),
                              ),
                            ),
                            SizedBox(height:MediaQuery.of(context).size.height * .02,),
                            Container(
                                width:double.infinity,
                                height: MediaQuery.of(context).size.height * .06,
                                child: ElevatedButton(
                                  style: ElevatedButton.styleFrom(
                                    primary: buttonColors, // background
                                    onPrimary: Colors.white, // foreground
                                  ),
                                  onPressed: () async {
                                    if(mobileNumberController.text!=''){
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            builder: (context) =>  PatientDashboardDetails(mobileNumber: mobileNumberController.text.toString(),)),
                                      );
                                    }
                                    else{
                                      AppSettings.longFailedToast('Please enter details');
                                    }
                                  },
                                  child: Text('Go to details'),
                                )),


                          ]
                      ))) )   ),
    );

  }
}
