import 'package:flutter/material.dart';
import 'package:capture/common/login.dart';
import 'package:capture/common/settings.dart';
import 'package:flutter/services.dart';
import 'dart:io' show File, Platform;




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

  @override
  State<SignUp> createState() => _SignUpState();
}

class _SignUpState extends State<SignUp> {
  bool isPwdObscureText=true;
  bool isConfirmPwdObscureText = true;
  TextEditingController nameController = TextEditingController();
  TextEditingController custmerid = TextEditingController();
  TextEditingController buildingNameController = TextEditingController();
    TextEditingController emailController = TextEditingController();
  TextEditingController mobileNumberController = TextEditingController();
  TextEditingController userAddressDescriptionController = TextEditingController();
  TextEditingController userAddressCapturingController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  TextEditingController confirmPasswordController = TextEditingController();
  var genderUnitItems = [
    'Male',
    'Female',
    'Other',
  ];
  var genderUnit = 'Male';


  String _currentAddress ='';
  String address1 = '';
  String address2 = '';
  String city = '';
  String state = '';
  String zip = '';
  String country = '';
  double lat=0;
  double lng=0;





  @override
  void initState() {
    isPwdObscureText = true;
    isConfirmPwdObscureText = 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) {
    return WillPopScope(
      onWillPop:()async=>onWillPop(),
      child: Scaffold(
        backgroundColor: primaryColor,
          body: Stack(children: <Widget>[
           /* Container(
              decoration:  const BoxDecoration(
                image:  DecorationImage(image:  AssetImage("images/background.png"), fit: BoxFit.cover,),
              ),
            ),*/
            GestureDetector(
              onTap: () {
                FocusManager.instance.primaryFocus?.unfocus();
              },
              child: SafeArea(
                  child: SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.all(10),
                      child: Column(
                        children: <Widget>[

                          SizedBox(
                            height: 20,
                          ),
                          Container(
                            //width: double.infinity,
                              child: Image(
                                image: const AssetImage('images/logo.png'),
                                height: MediaQuery.of(context).size.height * .18,
                              )),

                          SizedBox(
                            height:MediaQuery.of(context).size.height * .05,
                          ),
                          Container(
                            child: TextFormField(
                              cursorColor: Colors.white,
                              style: TextStyle(color: Colors.white),
                              controller: nameController,
                              textCapitalization: TextCapitalization.words,
                              decoration: textFormFieldDecoration(Icons.person,'Enter Name'),

                            ),
                          ), //name

                          SizedBox(height:MediaQuery.of(context).size.height * .02,),
                          Container(
                            child: TextFormField(
                              cursorColor: Colors.white,
                              style: TextStyle(color: Colors.white),
                              controller: mobileNumberController,
                              keyboardType: TextInputType.phone,
                              maxLength: 10,
                              decoration: textFormFieldDecoration(Icons.phone_android,'Enter Mobile Number'),
                            ),
                          ), //mobile
                          SizedBox(height:MediaQuery.of(context).size.height * .02,),
                          Container(
                            child: TextFormField(
                              cursorColor: Colors.white,
                              style: TextStyle(color: Colors.white),
                              controller: emailController,
                              keyboardType: TextInputType.emailAddress,
                              decoration: textFormFieldDecoration(Icons.plagiarism_outlined,'Enter email'),

                            ),
                          ),//address description
                          SizedBox(height:MediaQuery.of(context).size.height * .02,),
                          Container(
                            child: TextFormField(
                              cursorColor: Colors.white,
                              style: TextStyle(color: Colors.white),
                              obscureText: isPwdObscureText,
                              controller: passwordController,
                              decoration: InputDecoration(
                                fillColor: primaryColor,
                                filled: true,
                                labelText: 'Create Password',
                                prefixIcon: const Icon(
                                  Icons.lock,
                                  color: Colors.white,
                                ),
                                labelStyle: const TextStyle(
                                  color: Colors.white, //<-- SEE HERE
                                ),
                                border: const OutlineInputBorder(
                                    borderSide: BorderSide(color: Colors.white)),
                                focusedBorder: const OutlineInputBorder(
                                  borderSide: BorderSide(color: Colors.white),
                                ),
                                enabledBorder: const OutlineInputBorder(
                                  borderSide: BorderSide(color: Colors.white),
                                ),
                                suffixIcon: IconButton(
                                  icon:  Icon(
                                    Icons.visibility_off_outlined,
                                    color: isPwdObscureText?buttonColors:Colors.white,
                                  ),
                                  onPressed: () {
                                    setState(() {
                                      isPwdObscureText = !isPwdObscureText;
                                    });
                                  },
                                ),
                              ),
                            ),
                          ), //pwd
                          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.black, // foreground
                                ),
                                onPressed: () async {
                                  if (nameController.text != '' &&
                                      passwordController.text != '' &&
                                      mobileNumberController.text != '') {
                                    AppSettings.preLoaderDialog(context);

                                    var payload = new Map<String, dynamic>();
                                    payload["name"] = nameController.text.toString();
                                    payload["password"] =
                                        passwordController.text.toString();
                                    payload["phone"] =
                                        mobileNumberController.text.toString();
                                    payload["emails"] = [
                                      {"email": emailController.text.toString()}
                                    ];

                                   /* payload["gender"] = genderUnit.toString();*/

                                    bool signUpStatus = await AppSettings.signUp(payload);

                                    try {
                                      if (signUpStatus) {
                                        Navigator.of(context, rootNavigator: true).pop();
                                        await Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) => const Login()),
                                        );
                                      }
                                      else {
                                        Navigator.of(context, rootNavigator: true).pop();
                                        AppSettings.longFailedToast(
                                            "Mobile number already exists");
                                      }
                                    } catch (exception) {
                                      print(exception);
                                      Navigator.of(context, rootNavigator: true).pop();
                                      AppSettings.longFailedToast("Please enter valid details");
                                    }
                                  }
                                  else {
                                    AppSettings.longFailedToast("details should not be empty");
                                  }
                                },
                                child: Text('Sign Up'),
                              )),//login button
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              const Text(
                                'Are you a Member?',
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                              TextButton(
                                child: const Text(
                                  'Login?',
                                  style: TextStyle(
                                    fontSize: 20,
                                    color: Colors.white,
                                    decoration: TextDecoration.underline,
                                  ),
                                ),
                                onPressed: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => const Login()),
                                  );
                                },
                              )
                            ],
                          ),
                        ],
                      ),
                    )
                  )),
            ),
          ])), );
  }


  showUserSignUSuccessDialog() async {

    custmerid.text=AppSettings.customerIdsign;

    return showDialog(
      context: context,
      barrierDismissible: false,
      useSafeArea: true,
      builder: (BuildContext context) {
        return StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return AlertDialog(
                title: const Text('User SignUp Successfully!!'),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Container(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                        child: TextFormField(
                          cursorColor: greyColor,
                          readOnly: true,
                          controller: nameController,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(
                              Icons.person,
                              color: greyColor,
                            ),
                            border: OutlineInputBorder(
                                borderSide: BorderSide(color: greyColor)),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            labelText: 'User Name',
                            labelStyle: TextStyle(
                              color: greyColor, //<-- SEE HERE
                            ),
                          ),
                        ),
                      ),


                      Container(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                        child: TextFormField(
                          cursorColor: greyColor,
                          readOnly: true,
                          controller:custmerid,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(
                              Icons.person,
                              color: greyColor,
                            ),
                            border: OutlineInputBorder(
                                borderSide: BorderSide(color: greyColor)),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            labelText: 'User Id',
                            labelStyle: TextStyle(
                              color: greyColor, //<-- SEE HERE
                            ),
                          ),
                        ),
                      ),

                      // AppSettings.saveAvailableReportAndLocationsInMemorysignup('customerId');
                      const SizedBox(
                        height:10,
                      ),
                      Container(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                        child: TextFormField(
                          cursorColor: greyColor,
                          readOnly: true,
                          controller: buildingNameController,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(
                              Icons.home_filled,
                              color: greyColor,
                            ),
                            border: OutlineInputBorder(
                                borderSide: BorderSide(color: greyColor)),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            labelText: 'Building Name',
                            labelStyle: TextStyle(
                              color: greyColor, //<-- SEE HERE
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(
                        height:10,
                      ),
                      Container(
                        padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                        child: TextFormField(
                          cursorColor: greyColor,
                          readOnly: true,
                          controller: emailController,
                          decoration: const InputDecoration(
                            prefixIcon: Icon(
                              Icons.email,
                              color: greyColor,
                            ),
                            border: OutlineInputBorder(
                                borderSide: BorderSide(color: greyColor)),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide: BorderSide(color: greyColor),
                            ),
                            labelText: 'Email',
                            labelStyle: TextStyle(
                              color: greyColor, //<-- SEE HERE
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                actions: <Widget>[

                  Container(
                      width: 400,
                      height: 50,
                      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          primary: primaryColor, // background
                          onPrimary: Colors.white, // foreground
                        ),
                        onPressed: () async{
                          await Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => const Login()),
                          );

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


                  /* Padding(
                  //  padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16  ),
                    padding: EdgeInsets.only(left:75, bottom: 20, right: 70, top:10),
                    child: TextFormField(
                      cursorColor: greyColor,
                      controller: nameController,
                      decoration: const InputDecoration(
                        border: UnderlineInputBorder(),
                        labelText: 'Enter your username',
                      ),
                    ),
                  ),*/


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


}
