import 'package:country_currency_pickers/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'package:rounded_loading_button_plus/rounded_loading_button.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../Controller/Authentication/Authcontroller.dart';
import '../../Globalwidgets/textwidget.dart';
import '../../Globalwidgets/web_view.dart';
import '../../Helper/Constants/Assetconstants.dart';
import '../../Helper/Constants/Colorconstants.dart';
import '../../Helper/custombutton.dart';
import '../More/Faqview/privacy.dart';

class LoginView extends StatelessWidget {
   LoginView({super.key});

  final AuthController authController =  Get.put(AuthController());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        SystemNavigator.pop();
        return Future.value(true);
      },
      child: GetBuilder<AuthController>(
        initState: (_) {
          authController.getHintPhoneNumber(context);
        },
        builder: (controller) {
          return SafeArea(
            top: false,
            child: Scaffold(
              backgroundColor: Colors.white,
              body: Padding(
                padding: const EdgeInsets.only(left: 15, right: 15),
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Center(
                        child: Image.asset(
                            AssetConstants.nearledailyImage,
                            height: 300,
                            width: 300,
                        ),
                      ),
                      Center(
                        child: TextWidget(
                            text: "Enter Your Phone Number",
                            color: ColorConstants.primaryColor,
                            fontWeight: FontWeight.w700,
                            fontSize: 16,
                        ),
                      ),
                      const SizedBox(height: 10),
                      Center(
                        child: TextWidget(
                          text: "We will send you the 6 digit verification code",
                          fontSize: 14,
                        ),
                      ),
                      const SizedBox(height: 25),
                      TextWidget(
                        text: "Let's Get Started",
                        color: ColorConstants.primaryColor,
                        fontWeight: FontWeight.w700,
                        fontSize: 18,
                      ),
                      const SizedBox(height: 10),
                      contactNo(context),
                      const SizedBox(height: 20),
                      // const SizedBox(height: 0),
                      // SizedBox(
                      //   height: 100,
                      //   child: submit(context),
                      // ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          InkWell(
                            onTap: () {
                              terms();
                            },
                            child: Container(
                              height: 20,
                              width: 20,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(3),
                                color: controller.termsChecking
                                    ? Colors.green
                                    : Colors.white,
                                border: Border.all(
                                  color: controller.termsChecking
                                      ? Colors.green
                                      : Colors.grey[400]!,
                                ),
                              ),
                              child: controller.termsChecking
                                  ? const Icon(
                                Icons.check,
                                color: Colors.white,
                                size: 19,
                              )
                                  : const SizedBox(),
                            ),
                          ),
                          const SizedBox(width: 8),
                          Text(
                            "I agree to the Terms and &",
                            style: TextStyle(
                              color: ColorConstants.darkGreyColor,
                              fontSize: 14,
                            ),
                          ),
                          const SizedBox(width: 3),
                          InkWell(
                            onTap: () {
                              Get.to(() => const WebViewApp(
                                url: 'https://nearle.in/privacy',
                                appBarText: 'Privacy Policy',
                              ));
                            },
                            child: Text(
                              "Privacy Policy",
                              style: TextStyle(
                                color: ColorConstants.darkGreyColor,
                                fontSize: 14,
                                decoration: TextDecoration.underline,
                              ),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 80), // Space so button doesn't overlap
                    ],
                  ),
                ),
              ),
              bottomNavigationBar: Padding(
                padding: const EdgeInsets.all(15.0),
                child: SizedBox(
                  height: 100,
                  child: submit(context),
                ),
              ),
            ),
          );
        },
      ),
    );
  }


   Widget contactNo(context) {
     return TextFormField(
       buildCounter: (BuildContext context,
           {int? currentLength, int? maxLength, bool? isFocused}) =>
       null,
       maxLength: 10,
       keyboardType:
       const TextInputType.numberWithOptions(signed: true, decimal: true),
       controller: authController.loginPhoneNumberController,
       onChanged: (value){
         if(value.length==10){
           FocusScope.of(context).unfocus();

         }
       },
       onTap: () async {
         SharedPreferences prefs = await SharedPreferences.getInstance();
         prefs.setString('Code_Currency',
             authController.selectedDialogCountry.currencyCode!);
         prefs.setString('Currency_Code',
             authController.selectedDialogCountry.currencyCode!);
         prefs.setString('Country_Code',
             authController.selectedDialogCountry.isoCode!);
         prefs.setString('dialCode',
             '+${authController.selectedDialogCountry.phoneCode!}');
         prefs.setString('location_Country',
             authController.selectedDialogCountry.isoCode!);
         authController.currency();
       },
       decoration: InputDecoration(
         labelText: 'Mobile Number',
         labelStyle: const TextStyle(color: Colors.grey),
         prefixIcon: Column(
           mainAxisAlignment: MainAxisAlignment.center,
           children: [
             SizedBox(
               width: Get.width * 0.3,
               height: Get.height * 0.04,
               child: Row(
                 children: [
                   const SizedBox(
                     width: 25,
                   ),
                   InkWell(
                     child: CountryPickerUtils.getDefaultFlagImage(
                         authController.selectedDialogCountry),
                     // onTap: _openCountryPickerDialog,
                   ),
                   const SizedBox(
                     width: 8,
                   ),
                   Text(
                       "+${authController.selectedDialogCountry.phoneCode}"),
                 ],
               ),
             ),
           ],
         ),
         border: OutlineInputBorder(
           borderRadius: BorderRadius.circular(7.0),
           borderSide: const BorderSide(
             color: Color.fromRGBO(220, 220, 220, 0.6),
             width: 2.0,
           ),
         ),
         enabledBorder: OutlineInputBorder(
           borderRadius: BorderRadius.circular(7.0),
           borderSide: const BorderSide(
             color: Color.fromRGBO(220, 220, 220, 0.6),
             width: 2.0,
           ),
         ),
         focusedBorder: OutlineInputBorder(
           borderRadius: BorderRadius.circular(7.0),
           borderSide: const BorderSide(
             color: ColorConstants.primaryColor,
             width: 1.0,
           ),
         ),
       ),
     );
   }

   bool terms() {
     authController.update();
     authController.termsChecking = !authController.termsChecking;
     return authController.termsChecking;
   }

   Widget submit(context) {
     return GetBuilder<AuthController>(
         builder: (controller) {
           return RoundedLoadingButton(
               color: ColorConstants.primaryColor,
               width: Get.width,
               controller: controller.loginController,
               onPressed: () {
                     controller.loginTenant(context);
                   },
               child: TextWidget(
                 text: 'Next',
                 fontSize: 18,
                 color: ColorConstants.secondaryColor,
                 fontWeight: FontWeight.w700,
               )
           );
           //   RoundedButton(
           //   height: Get.height*0.06,
           //   width: Get.width,
           //   onPressed: () {
           //     controller.loginTenant(context);
           //   },
           //   color: ColorConstants.primaryColor,
           //   title: 'Next',
           //   controller: controller.btnController,
           //   textStyle: const TextStyle(
           //       fontSize: 18,
           //       fontFamily: "Lato",
           //       color: Colors.white,
           //       fontWeight: FontWeight.bold),
           // );
         });
   }
}
