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

import '../../../../../../../Controller/Dashboard/Pickup/Tabs/Tenantcustomers/Customeraddress/Addcustomer/Addcustomercontroller.dart';
import '../../../../../../../Helper/Constants/Colorconstants.dart';
import '../../../../../../../Helper/custombutton.dart';
import '../../../../../../../Helper/customederaction.dart';



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

  AddPickupCustomerController addPickupCustomerController = Get.put(AddPickupCustomerController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AddPickupCustomerController>(
        initState: (_){
          addPickupCustomerController.getLocation();
        },
        builder: (controller) {
          return Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(
              backgroundColor: ColorConstants.primaryColor,
              elevation: 0.3,
              leading: InkWell(
                  onTap: (){
                    Get.back();
                  },
                  child: Icon(Icons.reply,size: 30,)),
              title: Row(
                children: [
                  Text('Create customer',),
                ],
              ),
              titleSpacing: 0,
            ),
            body: Column(
              children: [
                Expanded(
                  flex: 9,
                  child: Container(
                      height: Get.height,
                      width: Get.width,
                      color: Colors.white,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SizedBox(
                            height: 15,
                          ),
                          Expanded(
                            // height: Get.height,
                            child: ListView(
                              children: [
                                Container(
                                  height: Get.height * 1.1,
                                  child: Padding(
                                    padding: const EdgeInsets.all(15.0),
                                    child: Column(
                                      children: [
                                        Column(
                                          children: [
                                            customerFirstName(),
                                            SizedBox(
                                              height: 15,
                                            ),
                                            // CustomerLastName(),
                                            // SizedBox(
                                            //   height: 15,
                                            // ),
                                            Container(
                                              height: 55,
                                              child: contactNo(Get.context),
                                            ),
                                            SizedBox(
                                              height: 15,
                                            ),
                                            Column(
                                              children: [
                                                Row(
                                                  children: [
                                                    Icon(Icons.location_on,size: 20,color:  ColorConstants.primaryColor,),
                                                    SizedBox(width: 5,),
                                                    Text("Address",style: TextStyle(color: Colors.grey[600],fontSize: 15),),
                                                    // Spacer(),
                                                    // InkWell(
                                                    //     onTap: () async{
                                                    //       if (controller.resultPosition != null) {
                                                    //         var locationResult = await
                                                    //         Get.to(CustomerMap(lat: controller.resultPosition!.latitude, long: controller.resultPosition!.longitude));
                                                    //         if (locationResult != null) {
                                                    //           controller.setLocationValues(locationResult);
                                                    //         }
                                                    //       }
                                                    //     },
                                                    //     child: Icon(
                                                    //       Icons.location_on_outlined,
                                                    //       color: Colors.red,
                                                    //       size: 32,
                                                    //     )),

                                                  ],
                                                ),
                                                SizedBox(height: 8,),
                                                address(),
                                              ],
                                            ),
                                            SizedBox(
                                              height: 15,
                                            ),
                                            doorNo(),
                                            SizedBox(
                                              height: 15,
                                            ),
                                            landmark(),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      )),
                ),
              ],
            ),
            bottomNavigationBar: Container(
              height: Get.height*0.08,
              child: submit(),
            ),
          );
        }
    );
  }

   customerFirstName(){
     return Container(
       decoration: BoxDecoration(
         color: ColorConstants.secondaryColor,
       ),
       child: TextFormField(
         textCapitalization: TextCapitalization.sentences,
         inputFormatters: <TextInputFormatter>[
           UpperCaseTextFormatter()
         ],
         decoration: CustomDecoration.textFieldStyle(
             labelTextStr: 'Name',
             prefixIcon: Icon(
               Icons.person,
               color: ColorConstants.primaryColor,
             )),
         controller: addPickupCustomerController.customerFirstNameController,
         onEditingComplete: () {
           FocusScope.of(Get.context!).requestFocus(FocusNode());
         },
       ),
     );

   }

   customerLastName(){
     return Container(
       decoration: BoxDecoration(
         color: ColorConstants.secondaryColor,
       ),
       child: TextFormField(
         decoration: CustomDecoration.textFieldStyle(
             labelTextStr: 'LastName',
             prefixIcon: Icon(
               Icons.person,
               color: ColorConstants.primaryColor,
             )),
         controller: addPickupCustomerController.customerLastNameController,
         onEditingComplete: () {
           FocusScope.of(Get.context!).requestFocus(FocusNode());
         },
       ),
     );

   }

   contactNo(context) {
     return GetBuilder<AddPickupCustomerController>(
         builder: (controller) {
           return TextFormField(
             enabled: true,
             maxLength: 10,

             keyboardType:
             TextInputType.numberWithOptions(signed: true, decimal: true),
             controller: controller.customerPhoneController,
             onChanged: (value){
               if(value.length==10){
                 FocusScope.of(context).unfocus();

               }
             },
             onTap: () async {
             },
             decoration: CustomDecoration.textFieldStyle(
               labelTextStr: 'Mobile no',
               prefixIcon: Column(
                 mainAxisAlignment: MainAxisAlignment.center,
                 children: [
                   Container(
                     width: Get.width * 0.3,
                     height: Get.height * 0.04,
                     child: Row(
                       children: [
                         Spacer(flex: 1,),
                         Flexible(
                           child: Icon(
                             Icons.call,
                             color:  ColorConstants.primaryColor,
                           ),
                         ),
                         Spacer(flex: 1,),
                         Flexible(flex: 3,child: Text("+${controller.selectedDialogCountry.phoneCode}",style: TextStyle(color:  ColorConstants.primaryColor,fontWeight: FontWeight.bold))),
                       ],
                     ),
                   ),
                 ],
               ),),

           );
         }
     );
   }

   address() {
     return Column(
       children: [
         Container(
           decoration: BoxDecoration(
             color: ColorConstants.secondaryColor,
           ),
           child: TextFormField(
             // onTap: () async{
             //   if (addCustomerController.resultPosition != null) {
             //     var locationResult = await  Get.to(CustomerMap(lat: addCustomerController.resultPosition!.latitude, long: addCustomerController.resultPosition!.longitude));
             //     if (locationResult != null) {
             //       addCustomerController.setLocationValues(locationResult);
             //     }
             //   }
             // },

             onChanged: (text) => addPickupCustomerController.onSearchTextChanged(text),

             decoration: CustomDecoration.textFieldStyle(),

             maxLines: 2,
             controller: addPickupCustomerController.addressController,
             onEditingComplete: () {
               FocusScope.of(Get.context!).requestFocus(FocusNode());
             },
           ),
         ),
         SizedBox(height: 8,),

         addPickupCustomerController.predictions.isNotEmpty?Container(
           height: Get.height*0.20,
           width: Get.width*0.90,
           decoration: BoxDecoration(color: Colors.grey[100],borderRadius: BorderRadius.circular(10)),
           child: Obx(() {
             return ListView.builder(
               itemCount: addPickupCustomerController.predictions.length,
               itemBuilder: (context, index) {
                 final prediction =
                 addPickupCustomerController.predictions[index]['description'];
                 return ListTile(
                   title: Text(prediction),
                   onTap: () {
                     final placeId =
                     addPickupCustomerController.predictions[index]['place_id'];
                     addPickupCustomerController.getPlaceDetails(placeId,prediction);
                     addPickupCustomerController.predictions.clear();
                     FocusScope.of(context).unfocus();
                   },
                 );
               },
             );

           }),):SizedBox()

       ],
     );

   }

   doorNo(){
     return Container(
       decoration: BoxDecoration(
         color: ColorConstants.secondaryColor,
       ),
       child: TextFormField(
         // keyboardType: TextInputType.numberWithOptions(signed: true, decimal: true),
         decoration: CustomDecoration.textFieldStyle(
             labelTextStr: 'Door / Flat number',
             prefixIcon: Icon(
               Icons.door_front_door_outlined,
               color: ColorConstants.primaryColor,
             )),
         controller: addPickupCustomerController.doorNoController,
         onEditingComplete: () {
           FocusScope.of(Get.context!).requestFocus(FocusNode());
         },
       ),
     );

   }

   landmark(){
     return Container(
       decoration: BoxDecoration(
         color: ColorConstants.secondaryColor,
       ),
       child: TextFormField(
         decoration: CustomDecoration.textFieldStyle(
             labelTextStr: 'Landmark',
             prefixIcon: Icon(
               Icons.location_on_rounded,
               color: ColorConstants.primaryColor,
             )),
         controller: addPickupCustomerController.landMarkController,
         onEditingComplete: () {
           FocusScope.of(Get.context!).requestFocus(FocusNode());
         },
       ),
     );

   }

   submit() {
     return Padding(
       padding: EdgeInsets.only(top: 0,bottom: 5,left: 20,right: 20),
       child: Center(
           child:  RoundedButton(
             onPressed: () {
               addPickupCustomerController.describeValidation();

             },
             color:ColorConstants.primaryColor!,
             title: 'Create',
             controller: addPickupCustomerController.btnController,
             textStyle: const TextStyle(
                 fontSize: 18,
                 fontFamily: "Lato",
                 color: Colors.white,
                 fontWeight: FontWeight.bold),
             height: Get.height*0.06,
             width: Get.width*0.8,
           )
       ),
     );
   }
}

class UpperCaseTextFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    return TextEditingValue(
      text: capitalize(newValue.text),
      selection: newValue.selection,
    );
  }
}
String capitalize(String value) {
  if(value.trim().isEmpty) return "";
  return "${value[0].toUpperCase()}${value.substring(1)}";
}