import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/main/registration_form_controller.dart';
import 'package:sigap/src/utils/validators/validation.dart';

class PersonalInfoController extends GetxController {
  
  // Singleton instance
  static PersonalInfoController get instance => Get.find();

  // Controllers
  final firstNameController = TextEditingController();
  final lastNameController = TextEditingController();
  final nameController = TextEditingController(); // For combined name
  final phoneController = TextEditingController();
  final bioController = TextEditingController();
  final addressController = TextEditingController();

  // Error states
  final RxString firstNameError = ''.obs;
  final RxString lastNameError = ''.obs;
  final RxString nameError = ''.obs;
  final RxString phoneError = ''.obs;
  final RxString bioError = ''.obs;
  final RxString addressError = ''.obs;

  final RxString personalInfoError = ''.obs; // General error for personal info


  // Manual validation as fallback
  final RxBool isFormValid = RxBool(true);

  @override
  void onInit() {
    super.onInit();
    // Add listeners to first name and last name controllers to update the combined name
    firstNameController.addListener(_updateCombinedName);
    lastNameController.addListener(_updateCombinedName);
  }

  // Update the combined name when either first or last name changes
  void _updateCombinedName() {
    final firstName = firstNameController.text.trim();
    final lastName = lastNameController.text.trim();

    if (firstName.isEmpty && lastName.isEmpty) {
      nameController.text = '';
    } else if (lastName.isEmpty) {
      nameController.text = firstName;
    } else if (firstName.isEmpty) {
      nameController.text = lastName;
    } else {
      nameController.text = '$firstName $lastName';
    }
  }

  bool validate(GlobalKey<FormState> formKey) {
    clearErrors();

    if (formKey.currentState?.validate() ?? false) {
      return true;
    }


    final firstNameValidation = TValidators.validateUserInput(
      'First name',
      firstNameController.text,
      50,
    );
    if (firstNameValidation != null) {
      firstNameError.value = firstNameValidation;
      isFormValid.value = false;
    }

    final lastNameValidation = TValidators.validateUserInput(
      'Last name',
      lastNameController.text,
      50,
      required: false,
    );
    if (lastNameValidation != null) {
      lastNameError.value = lastNameValidation;
      isFormValid.value = false;
    }

    final phoneValidation = TValidators.validatePhoneNumber(
      phoneController.text,
    );
    if (phoneValidation != null) {
      phoneError.value = phoneValidation;
      isFormValid.value = false;
    }

    // Bio can be optional, so we validate with required: false
    final bioValidation = TValidators.validateUserInput(
      'Bio',
      bioController.text,
      255,
      required: false,
    );
    if (bioValidation != null) {
      bioError.value = bioValidation;
      isFormValid.value = false;
    }

    final addressValidation = TValidators.validateUserInput(
      'Address',
      addressController.text,
      255,
    );
    if (addressValidation != null) {
      addressError.value = addressValidation;
      isFormValid.value = false;
    }

    return isFormValid.value;
  }

  void handleNextStep(
    GlobalKey<FormState> formKey,
    FormRegistrationController mainController,
  ) {
    if (!validate(formKey)) {
      personalInfoError.value = 'Please fill the required information.';
      return;
    }
    mainController.nextStep();
  }
 

  void clearErrors() {
    firstNameError.value = '';
    lastNameError.value = '';
    nameError.value = '';
    phoneError.value = '';
    bioError.value = '';
    addressError.value = '';
  }

  @override
  void onClose() {
    firstNameController.removeListener(_updateCombinedName);
    lastNameController.removeListener(_updateCombinedName);

    firstNameController.dispose();
    lastNameController.dispose();
    nameController.dispose();
    phoneController.dispose();
    bioController.dispose();
    addressController.dispose();

    super.onClose();
  }
}
