import 'dart:developer';

import 'package:hub/backend/api_requests/api_manager.dart';

import 'package:flutter/material.dart';
import 'package:hub/components/organism_components/schedule_visit_detail/schedule_visit_detail_widget.dart';
import 'package:hub/flutter_flow/flutter_flow_model.dart';
import 'package:intl/intl.dart';

class ScheduleVisitDetailModel
    extends FlutterFlowModel<ScheduleVisitDetailWidget> {
  ///  State fields for stateful widgets in this component.

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode1;
  TextEditingController? textController1;
  String? Function(BuildContext, String?)? textController1Validator;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode2;
  TextEditingController? textController2;
  String? Function(BuildContext, String?)? textController2Validator;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode3;
  TextEditingController? textController3;
  String? Function(BuildContext, String?)? textController3Validator;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode4;
  TextEditingController? textController4;
  String? Function(BuildContext, String?)? textController4Validator;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode5;
  TextEditingController? textController5;
  String? Function(BuildContext, String?)? textController5Validator;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode6;
  TextEditingController? textController6;
  String? Function(BuildContext, String?)? textController6Validator;
  // Stores action output result for [Backend Call - API (postScheduleVisit)] action in Icon widget.
  ApiCallResponse? postScheduleVisit;

  String convertDateFormat(String dateStr) {
    try {
      log('Received date string: $dateStr');

      // Formato original
      DateFormat originalFormat = DateFormat('d/M/y H:mm:ss');
      // Novo formato
      DateFormat newFormat = DateFormat('y-M-d H:mm:ss');

      // Validate the input string format
      if (!RegExp(r'^\d{1,2}/\d{1,2}/\d{4} \d{1,2}:\d{2}:\d{2}$')
          .hasMatch(dateStr)) {
        log('Invalid date format: $dateStr');
        return 'Invalid date format';
      }

      // Converte a string para DateTime
      DateTime dateTime = originalFormat.parse(dateStr);

      // Converte DateTime para a nova string formatada
      String formattedDate = newFormat.format(dateTime);
      log('Formatted date: $formattedDate');
      return formattedDate;
    } catch (e) {
      // Handle the exception by returning an error message or a default value
      log('Error parsing date: $e');
      return 'Invalid date format';
    }
  }

  @override
  void initState(BuildContext context) {}

  @override
  void dispose() {
    textFieldFocusNode1?.dispose();
    textController1?.dispose();

    textFieldFocusNode2?.dispose();
    textController2?.dispose();

    textFieldFocusNode3?.dispose();
    textController3?.dispose();

    textFieldFocusNode4?.dispose();
    textController4?.dispose();

    textFieldFocusNode5?.dispose();
    textController5?.dispose();

    textFieldFocusNode6?.dispose();
    textController6?.dispose();
  }
}
