import 'package:siro_rider/constant/style.dart';
import 'package:siro_rider/controller/auth/register_controller.dart';
import 'package:siro_rider/views/widgets/elevated_btn.dart';
import 'package:siro_rider/views/widgets/my_scafold.dart';
import 'package:siro_rider/views/widgets/my_textField.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../controller/local/phone_intel/intl_phone_field.dart';
import '../../print.dart';
import '../widgets/mycircular.dart';
import '../../controller/functions/country_logic.dart';
import '../../../constant/box_name.dart';
import '../../../main.dart';
// import 'package:intl_phone_field/intl_phone_field.dart';

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

  @override
  Widget build(BuildContext context) {
    Get.put(RegisterController());
    return MyScafolld(
      title: "Phone Number Check".tr,
      body: [
        GetBuilder<RegisterController>(builder: (registerController) {
          return ListView(
            children: [
              // Logo at the top
              Padding(
                padding: const EdgeInsets.only(bottom: 20.0),
                child: Image.asset(
                  'assets/images/logo.png', // Make sure you have a logo image in your assets folder
                  height: 100,
                ),
              ),
              // Message to the driver
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
                child: Text(
                  'We need your phone number to contact you and to help you.'
                      .tr,
                  textAlign: TextAlign.center,
                  style: AppStyle.title,
                ),
              ),
              // Phone number input field with country code dropdown
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: IntlPhoneField(
                  decoration: InputDecoration(
                    labelText: 'Phone Number'.tr,
                    border: const OutlineInputBorder(
                      borderSide: BorderSide(),
                    ),
                  ),
                  initialCountryCode: CountryLogic.getCountryPrefix(box.read(BoxName.countryCode) ?? 'Syria'),
                  onChanged: (phone) {
                    // Properly concatenate country code and number
                    registerController.phoneController.text =
                        phone.completeNumber.toString();
                    Log.print(' phone.number: ${phone.number}');
                    Log.print(
                        "Formatted phone number: ${registerController.phoneController.text}");
                  },
                  validator: (phone) {
                    // Check if the phone number is not null and is valid
                    if (phone == null || phone.completeNumber.isEmpty) {
                      return 'Please enter your phone number';
                    }

                    // Extract the phone number (excluding the country code)
                    final number = phone.completeNumber.toString();

                    // Check if the number length is exactly 11 digits
                    if (number.length != 13) {
                      return 'Phone number must be exactly 11 digits long';
                    }

                    // If all validations pass, return null
                    return null;
                  },
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              if (registerController.isSent)
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Form(
                    key: registerController.formKey3,
                    child: MyTextForm(
                        controller: registerController.verifyCode,
                        label: '3 digit'.tr,
                        hint: '3 digit'.tr,
                        type: TextInputType.number),
                  ),
                ),
              // Submit button
              registerController.isLoading
                  ? const MyCircularProgressIndicator()
                  : Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: MyElevatedButton(
                        onPressed: () async {
                          !registerController.isSent
                              ? await registerController.sendOtpMessage()
                              : await registerController.verifySMSCode();
                        },
                        title: 'Submit'.tr,
                      ),
                    ),
            ],
          );
        }),
      ],
      isleading: false,
    );
  }
}
