import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hub/features/backend/index.dart';
import 'package:hub/features/storage/index.dart';

import 'package:hub/shared/utils/validator_util.dart';

import '/flutter_flow/flutter_flow_util.dart';
import '/flutter_flow/form_field_controller.dart';
import 'regisiter_vistor_template_component_widget.dart';

class RegisiterVistorTemplateComponentModel
    extends FlutterFlowModel<RegisiterVistorTemplateComponentWidget> {
  Timer? _debounceTimer;
  late final String devUUID;
  late final String userUUID;
  late final String cliUUID;

  final unfocusNode = FocusNode();
  bool isDataUploading = false;
  FFUploadedFile uploadedLocalFile =
      FFUploadedFile(bytes: Uint8List.fromList([]));

  void debounce(Function() fn, Duration time) {
    if (_debounceTimer != null) {
      _debounceTimer!.cancel();
    }
    _debounceTimer = Timer(time, fn);
  }

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode1;
  TextEditingController? textController1;
  String? Function(BuildContext, String?)? textController1Validator;
  String? _textController1Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }

    return null;
  }

  Future<bool> getVisitanteByDocument(
      String document, BuildContext context) async {
    final response = await PhpGroup.getVisitorByDocCall.call(
      documento: document,
    );

    if (response.jsonBody['visitante']['VTE_ID'] != '0' &&
        response.jsonBody['error'] != 'false') {
      return true;
    }
    return false;
  }

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode2;
  TextEditingController? textController2;
  String? Function(BuildContext, String?)? textController2Validator;
  String? _textController2Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }

    return null;
  }

  // State field(s) for DropDown widget.
  String? dropDownValue;
  FormFieldController<String>? dropDownValueController;
  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode3;
  TextEditingController? textController3;
  String? Function(BuildContext, String?)? textController3Validator;
  String? _textController3Validator(BuildContext context, String? val) {
    if (val == null || val.isEmpty) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'This field is required',
        ptText: 'Este campo é obrigatório',
      );
    }
    return null;
  }

  // State field(s) for TextField widget.
  FocusNode? textFieldFocusNode4;
  TextEditingController? textController4;
  String? Function(BuildContext, String?)? textController4Validator;
  String? _textController4Validator(BuildContext context, String? val) {
    if (val != null &&
        val.isNotEmpty &&
        ValidatorUtil.isValidEmail(val) == false) {
      return FFLocalizations.of(context).getVariableText(
        enText: 'Invalid email',
        ptText: 'Email inválido',
      );
    }
    return null;
  }

  // Stores action output result for [Custom Action - convertImageFileToBase64] action in Button widget.
  String? imgBase64;
  // Stores action output result for [Backend Call - API (postScheduleVisitor)] action in Button widget.
  ApiCallResponse? scheduleVisitor;

  @override
  void initState(BuildContext context) {
    textFieldFocusNode1 = FocusNode();
    textController1 = TextEditingController();
    textController1Validator = _textController1Validator;

    textFieldFocusNode2 = FocusNode();
    textController2 = TextEditingController();
    textController2Validator = _textController2Validator;

    dropDownValue = '';
    // dropDownValueController = FormFieldController<String>(initialValue: dropDownValue);

    textFieldFocusNode3 = FocusNode();
    textController3 = TextEditingController();
    textController3Validator = _textController3Validator;

    textFieldFocusNode4 = FocusNode();
    textController4 = TextEditingController();
    textController4Validator = _textController4Validator;

    initializeDatabase();
  }

  Future<void> initializeDatabase() async {
    devUUID = (await StorageHelper().get(ProfileStorageKey.devUUID.key)) ?? '';
    userUUID =
        (await StorageHelper().get(ProfileStorageKey.userUUID.key)) ?? '';
    cliUUID =
        (await StorageHelper().get(ProfileStorageKey.clientUUID.key)) ?? '';
    ;
  }

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

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

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

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