import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_delivery/common/otpscreen.dart';
import 'package:healthcare_delivery/common/settings.dart';


class Login extends StatefulWidget {
  const Login({super.key});

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {

  bool isObscureText=true;
  TextEditingController mobileNumberController = TextEditingController();
  TextEditingController passwordController = TextEditingController();


  @override
  void initState() {
    isObscureText=true;
    super.initState();
  }

  Future<bool> onWillPop() async {
    final shouldPop = await showDialog<bool>(context: context, builder: (context) {
      return AlertDialog(
        title: const Text('Do you want to exit app?',
            style: TextStyle(
              color: primaryColor,
              fontSize: 20,
            )),
        actionsAlignment: MainAxisAlignment.spaceBetween,
        actions: [
          TextButton(
            onPressed: () {
              SystemNavigator.pop();
            },
            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) {
    String lastInputValue="";

    return WillPopScope(
      onWillPop: () async => onWillPop(),
      child: Scaffold(
          backgroundColor: primaryColor,
          body: Stack(
            children: <Widget>[
              /*Container(
                decoration:  const BoxDecoration(
                  image:  DecorationImage(image:AssetImage("images/backgroundimage.png"), fit: BoxFit.cover,),
                ),
              ),*/
              GestureDetector(
                onTap: () {
                  FocusManager.instance.primaryFocus?.unfocus();
                },
                child: SafeArea(
                    child: SingleChildScrollView(
                        child: Padding(
                          padding: const EdgeInsets.all(10),
                          child: Column(
                            children: <Widget>[
                              SizedBox(
                                height:MediaQuery.of(context).size.height * .05,
                              ),
                              Container(
                                //width: double.infinity,
                                  child: Image(
                                    image: const AssetImage('images/logo.png'),
                                    height: MediaQuery.of(context).size.height * .25,
                                  )),
                              SizedBox(
                                height:MediaQuery.of(context).size.height * .05,
                              ),
                              Container(
                                padding: const EdgeInsets.all(0),
                                child: TextFormField(
                                  cursorColor: Colors.white,
                                  controller: mobileNumberController,
                                  keyboardType: TextInputType.number,
                                  style: TextStyle(color: Colors.white),
                                  decoration: textFormFieldDecoration(Icons.phone,'Enter MobileNumber'),
                                ),
                              ),
                              SizedBox(
                                height:MediaQuery.of(context).size.height * .02,
                              ),

                              Container(
                                  width:double.infinity,
                                  height: MediaQuery.of(context).size.height * .05,
                                  child: ElevatedButton(
                                    style: ElevatedButton.styleFrom(
                                      primary: buttonColors, // background
                                      onPrimary: Colors.white, // foreground
                                    ),
                                    onPressed: () async{

                                      if (mobileNumberController.text != '') {
                                        AppSettings.preLoaderDialog(context);

                                        bool isOnline = await AppSettings.internetConnectivity();

                                        if(isOnline){
                                          var payload = new Map<String, dynamic>();
                                          payload["mobileNumbers"] = mobileNumberController.text.toString();
                                          bool signinStatus = await AppSettings.loginOtp(payload);

                                          try{
                                            if (signinStatus) {
                                              Navigator.of(context,rootNavigator: true).pop();

                                              await Navigator.push(
                                                context,
                                                MaterialPageRoute(
                                                    builder: (context) =>  OtpScreen(myObject:mobileNumberController.text)),
                                              );
                                              AppSettings.longSuccessToast("OTP sent Successfully");
                                              mobileNumberController.text='';
                                              passwordController.text='';

                                            } else {
                                              Navigator.of(context,rootNavigator: true).pop();
                                              AppSettings.longFailedToast("Please enter valid phone number");
                                            }
                                          }
                                          catch(exception){
                                            Navigator.of(context,rootNavigator: true).pop();
                                            print(exception);
                                          }
                                        }
                                        else{
                                          Navigator.of(context,rootNavigator: true).pop();
                                          AppSettings.longFailedToast("Please Check internet");
                                        }
                                      }
                                      else{

                                        AppSettings.longFailedToast("Please enter valid details");
                                      }


                                    },
                                    child: const Text('Login'),
                                  )),

                            ],
                          ),
                        )
                    )),
              ),
            ],
          )
      ),
    );
  }
}
