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

import 'package:hub/flutter_flow/flutter_flow_model.dart';
import 'package:hub/flutter_flow/form_field_controller.dart';
import 'package:hub/flutter_flow/internationalization.dart';
import 'package:hub/flutter_flow/request_manager.dart';

import 'package:flutter/material.dart';
import 'package:hub/pages/schedule_complete_visit_page/schedule_complete_visit_page_widget.dart';
import 'package:intl/intl.dart';

class ScheduleCompleteVisitPageModel
    extends FlutterFlowModel<ScheduleCompleteVisitPageWidget> {
  final _visitHistoryManager = FutureRequestManager<ApiCallResponse>();
  Future<ApiCallResponse> visitHistory({
    String? uniqueQueryKey,
    bool? overrideCache,
    required Future<ApiCallResponse> Function() requestFn,
  }) =>
      _visitHistoryManager.performRequest(
        uniqueQueryKey: uniqueQueryKey,
        overrideCache: overrideCache,
        requestFn: requestFn,
      );
  void clearVisitHistoryCache() => _visitHistoryManager.clear();
  void clearVisitHistoryCacheKey(String? uniqueKey) =>
      _visitHistoryManager.clearRequest(uniqueKey);

  ///  Local state fields for this page.

  List<dynamic> visitorJsonList = [];
  void addToVisitorJsonList(dynamic item) => visitorJsonList.add(item);
  void removeFromVisitorJsonList(dynamic item) => visitorJsonList.remove(item);
  void removeAtIndexFromVisitorJsonList(int index) =>
      visitorJsonList.removeAt(index);
  void insertAtIndexInVisitorJsonList(int index, dynamic item) =>
      visitorJsonList.insert(index, item);
  void updateVisitorJsonListAtIndex(int index, Function(dynamic) updateFn) =>
      visitorJsonList[index] = updateFn(visitorJsonList[index]);

  String visitorStrList = '0';
  bool? dropDownValidator1;
  bool? dropDownValidator2;

  bool isValid() {
    if ((textController1!.text != '') &&
        (textController2!.text != '') &&
        (dropDownValue1 != null && dropDownValue1 != '') &&
        (dropDownValue2 != null && dropDownValue2 != '')) {
      return true;
    }
    return false;
  }

  ///  State fields for stateful widgets in this page.

  final unfocusNode = FocusNode();
  // State field(s) for TabBar widget.
  TabController? tabBarController;
  int get tabBarCurrentIndex =>
      tabBarController != null ? tabBarController!.index : 0;

  // 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.',
      );
    }
    if (val != null) {
      try {
        DateTime startDate = DateFormat('d/M/y H:mm:ss').parse(val);
        DateTime endDate =
            DateFormat('d/M/y H:mm:ss').parse(textController2!.text);
        if (startDate.isAfter(endDate)) {
          return FFLocalizations.of(context).getVariableText(
            ptText: 'A data de início deve ser anterior à data de término.',
            enText: 'Start date must be before end date.',
          );
        }
        if (startDate.isBefore(DateTime.now())) {
          return FFLocalizations.of(context).getVariableText(
            enText: 'Start date must be after current date.',
            ptText: 'A data de início deve ser posterior à data atual.',
          );
        }
      } catch (e) {
        return '$e';
      }
    }
    return null;
  }

  DateTime? datePicked1;
  // 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.',
      );
    }
    if (val != null) {
      try {
        DateTime endDate = DateFormat('d/M/y H:mm:ss').parse(val);
        DateTime startDate =
            DateFormat('d/M/y H:mm:ss').parse(textController1!.text);
        if (endDate.isBefore(startDate)) {
          return FFLocalizations.of(context).getVariableText(
            enText: 'End date must be after start date.',
            ptText: 'A data de término deve ser posterior à data de início.',
          );
        }
        if (endDate.isBefore(DateTime.now())) {
          return FFLocalizations.of(context).getVariableText(
            enText: 'End date must be after current date.',
            ptText: 'A data de término deve ser posterior à data atual.',
          );
        }
      } catch (e) {
        return '$e';
      }
    }
    return null;
  }

  DateTime? datePicked2;
  // State field(s) for DropDown widget.
  String? dropDownValue1;
  FormFieldController<String>? dropDownValueController1;

  // State field(s) for DropDown widget.
  String? dropDownValue2;
  FormFieldController<String>? dropDownValueController2;
  // State field(s) for Switch widget.
  bool? switchValue;
  // 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;
  }

  @override
  void initState(BuildContext context) {
    tabBarController = TabController(
      vsync: Navigator.of(context),
      length: 2,
    );

    textFieldFocusNode1 = FocusNode();
    textController1 = TextEditingController();
    textController1Validator = _textController1Validator;

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

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

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

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

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

    clearVisitHistoryCache();
  }

  /// Action blocks.
  Future getVisitorsActionPage(
    BuildContext context, {
    List<dynamic>? visitorsJsonList,
  }) async {
    visitorJsonList = visitorsJsonList!.toList().cast<dynamic>();
  }

  Future<bool> toggleCurrentSelectionHeader(
    BuildContext context, {
    required bool? toggleIndexValue,
  }) async {
    if (toggleIndexValue == true) {
      return true;
    }

    return false;
  }
}
