import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:hub/flutter_flow/nav/nav.dart'; import 'package:hub/shared/utils/dialog_util.dart'; import 'package:hub/shared/utils/storage_util.dart'; import '../../../backend/api_requests/api_calls.dart'; import '../../../components/organism_components/bottom_arrow_linked_locals_component/bottom_arrow_linked_locals_component_widget.dart'; import '../../../flutter_flow/flutter_flow_util.dart'; import '../../utils/log_util.dart'; import '../../utils/snackbar_util.dart'; class LocalizationService { static Future checkLocals(BuildContext context) async { try { final GetLocalsCall callback = PhpGroup.getLocalsCall; var response = await callback.call(); if (response.jsonBody['error'] == false) { final List locals = response.jsonBody['locais'] ?? []; final bool isEmpty = locals.isEmpty; final bool isActive = locals .where((local) => local['CLU_STATUS'] != 'B') .toList() .isNotEmpty; final bool isEnable = !isEmpty && isActive; if (isEnable) { StorageUtil().haveLocal = true; StorageUtil().isLogged = true; await WidgetsBinding.instance.endOfFrame; StorageUtil().cliUUID = ''; StorageUtil().ownerUUID = ''; StorageUtil().context?.go('/homePage'); } } } catch (e, s) { log(e.toString(), stackTrace: s); } } static Future processLocals(BuildContext context) async{ try { final GetLocalsCall callback = PhpGroup.getLocalsCall; final ApiCallResponse response = await callback.call(); final List locals = response.jsonBody['locais'].toList() ?? []; for (var local in locals) { final String status = local['CLU_STATUS']; log('() => CLU_STATUS: $status'); } final bool isActive = locals .where((local) => local['CLU_STATUS'] == 'A') .toList() .isNotEmpty; final bool isInactived = locals .where((local) => local['CLI_ID'] != StorageUtil().cliUUID && local['CLU_STATUS'] == 'A').toList().isNotEmpty; final bool isPending = locals .where((local) => local['CLU_STATUS'] != 'B' && local['CLU_STATUS'] != 'A') .toList() .isNotEmpty; final bool isUnique = locals.length == 1; final bool isBlocked = locals[0]['CLU_STATUS'] == 'B'; final bool isEnabled = isUnique && isActive; final bool isDisabled = isUnique && isBlocked; final bool isUnselected = StorageUtil().cliUUID.isEmpty && StorageUtil().cliName.isEmpty & StorageUtil().ownerUUID.isEmpty; final bool isSelected = StorageUtil().cliUUID.isNotEmpty && StorageUtil().cliName.isNotEmpty && isInactived; final bool isUnavailable = isPending && isUnselected && isUnique; if (isDisabled) { context.go('/welcomePage'); return true; } else if (isUnavailable) { log('() => isUnavailable'); try { StorageUtil().cliUUID = locals[0]['CLI_ID']; StorageUtil().ownerUUID = locals[0]['CLU_OWNER_ID']; var response = await PhpGroup.resopndeVinculo.call(tarefa: 'A'); if (response.jsonBody['error'] == true) { StorageUtil().cliUUID = ''; StorageUtil().cliName = ''; StorageUtil().ownerUUID = ''; return false; } if (response.jsonBody['error'] == false) return await processData(context).then( (value) => value); } catch (e, s) { DialogUtil.errorDefault(context); LogUtil.requestAPIFailed( 'responderVinculo.php', '', 'Responder VĂ­nculo', e, s); return false; } } else if (isEnabled) { log('() => isEnabled'); final dynamic local = locals[0]; StorageUtil().cliName = local['CLI_NOME']; StorageUtil().userName = local['USU_NOME']; StorageUtil().cliUUID = local['CLI_ID']; StorageUtil().ownerUUID = local['CLU_OWNER_ID']; return await processData(context); } else if (isUnselected) { log('() => isUnselected'); return await selectLocal(context); } else if (isSelected) { log('() => isSelected'); return await processData(context); } else { log('() => else'); return await selectLocal(context); } } catch (e) { log('() => catch: $e'); return await selectLocal(context); } return false; } static Future processData(BuildContext context) async { try { final GetDadosCall callback = PhpGroup.getDadosCall; var response = await callback.call(); final error = response.jsonBody['error']; if (error == false) { final bool whatsapp = response.jsonBody['whatsapp'] ?? false; final bool provisional = response.jsonBody['provisional'] ?? false; final bool pets = response.jsonBody['pet'] ?? false; final String petAmountRegister = response.jsonBody['petAmountRegister'].toString().isEmpty ? '0' : response.jsonBody['petAmountRegister'].toString(); final String name = response.jsonBody['visitado']['VDO_NOME']; StorageUtil().whatsapp = whatsapp; StorageUtil().provisional = provisional; StorageUtil().pets = pets; StorageUtil().petAmountRegister = petAmountRegister; StorageUtil().userName = name; return true; } else if (error == true) { log('() => error: $error'); DialogUtil.warningDefault(context).whenComplete(() => selectLocal(context)); return false; } return false; } catch (e, s) { log('() => error: $e', stackTrace: s); DialogUtil.warningDefault(context).whenComplete(() => selectLocal(context)); return false; } } static Future selectLocal(BuildContext context) async { return await showModalBottomSheet( isScrollControlled: true, backgroundColor: Colors.transparent, enableDrag: false, isDismissible: false, showDragHandle: false, useSafeArea: true, context: context, builder: (context) => PopScope( canPop: false, child: Padding( padding: MediaQuery.viewInsetsOf(context), child: const BottomArrowLinkedLocalsComponentWidget(), ), ), ).then((_) async => await processData(context)); } static Future unlinkLocal(BuildContext context) async { String content; try { content = FFLocalizations.of(context).getVariableText( enText: 'Device unlinked successfully', ptText: 'Dispositivo desvinculado com sucesso', ); await PhpGroup.resopndeVinculo.call(tarefa: 'I').then((value) { if (value.jsonBody['error'] == false) { StorageUtil().cliName = ''; StorageUtil().cliUUID = ''; // StorageUtil().ownerUUID = ''; context.pop(); context.go( '/homePage', extra: { kTransitionInfoKey: const TransitionInfo( hasTransition: true, transitionType: PageTransitionType.scale, alignment: Alignment.bottomCenter, ), }, ); SnackBarUtil.showSnackBar(context, content); } }).catchError((err, stack) { context.pop(); content = FFLocalizations.of(context).getVariableText( enText: 'Error unlinking device', ptText: 'Erro ao desvincular dispositivo', ); SnackBarUtil.showSnackBar(context, content, isError: true); }); } catch (err, stack) { context.pop(); log(err.toString(), stackTrace: stack); content = FFLocalizations.of(context).getVariableText( enText: 'Error unlinking device', ptText: 'Erro ao desvincular dispositivo', ); SnackBarUtil.showSnackBar(context, content, isError: true); } } }